Title Description
The cows has opened a new business, and Farmer John wants to see how well they is doing. The business have been running for N (1 <= N <= 100,000) days, and every day I the cows recorded their net profit p_i ( -1,000 <= p_i <= 1,000).
Farmer John wants to find the largest total profit that the cows has made during any consecutive time period. (Note that a consecutive time period can range in length from one day through N days.) Help him by writing a program to calculate the largest sum of consecutive profits.
The cattle opened a new company, the company has been operating for n days, the financial statements show that the first day of profit is pi, some days of profit may be a negative. John wants to write a news report to the dairy company to brag about their performance. So he wanted to know, in which consecutive days the company had the largest sum of profits.
Input/output format
Input format:
Output format:
- Line 1: A single integer representing the value of the maximum sum of profits for any consecutive time period.
Input and Output Sample input example # #:
Sample # # of output:
Description
The maximum sum is obtained by taking the sum from the second through the sixth number (4, 9,-2, -5, 8) and 14.
Simple rules. (because the equation does not pay the full negative situation, add a l special sentence)
The code is actually:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 intn,a,b,c,l=- the, f[2][2];5 intMain () {6scanf"%d",&n);7 for(intI=1; i<=n;i++){8scanf"%d",&a);9b=i%2; C= (b +1)%2; l=Max (l,a);Tenf[b][0]=max (f[c][0],f[c][1]); Onef[b][1]=max (f[c][1]+a,a); A } - if(l>=0) printf ("%d\n", Max (f[n%2][0],f[n%2][1])); - Elseprintf"%d\n", L); the return 0; -}
Remember a number of questions?
[Usaco11jan] profit profits