#  maximum-sum section due to Gries.

#  good test of dynamic allocation of arrays, pre-defined functions
#  (lb,ub,max), and some sequential stuff.

# this tiny program has a remarkable proclivity for exposing compiler bugs.

resource gries()

    op max_sum(int a[0:*]) returns int ms

    int n
    put("enter n\n")
    read(n)
    int nums[1:n]
    # read in numbers
    for [ i = lb(nums) to ub(nums) ] {
	put("?")
	read(nums[i])
    }
    write(max_sum(nums))

    proc max_sum(a) returns s {
	int k = lb(a)
	int c = 0
	s = 0
	while (k != ub(a)+1) {
	    c = max(a[k]+c,0)
	    s = max(s,c)
	    k++
	}
    }
end
