Profits
InterviewTime limit1sMemory limit128 MB
Given a sequence of N daily profits, find the maximum sum over any contiguous stretch of days.
- Level
Easy3 of 10
- Topics
- Array, Dynamic programming, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
The cows have opened a new business, and Farmer John wants to see how well it is doing. The business has run for days (), and on each day the cows recorded their net profit ().
Farmer John wants to find the largest total profit earned during any single consecutive stretch of days. Such a stretch may be as short as one day or as long as all days. Write a program that computes this largest possible sum of consecutive daily profits.
Input
- Line 1: a single integer .
- Lines through : line contains a single integer .
Output
- Line 1: a single integer, the maximum sum of profits over any consecutive stretch of days.
Hint
In the sample, the maximum is obtained by summing the profits from day 2 through day 6 ().