[Usaco11jan] Profit profits

Source: Internet
Author: User

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:

    • Line 1: A single integer:n

    • Lines 2..n+1:line i+1 contains a single integer:p_i

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.