20,200,310 and the maximum sequences (dynamic programming)

Description Title
for a given sequence length N is an integer A, which defines the "sequence" of be: A stretch of consecutive non-null elements (integer). You have to complete the task, all possible sequences, find a sequence, the sequence of all the elements and is the largest (in comparison with all other sub-sequence). This procedure requires you to maximum output.

Input
The first line of the input file contains an integer N, the second line contains N integers, represents A.
Wherein
. 1 <= N <= 100000
-10000 <= A [I] <= 10000

Output
Output contains only one integer that represents you figure out the answer.
Sample input
. 5
. 3. 4 -2. 3 -5

Sample output
4

#include <bits/stdc++.h>
using namespace std;
int a[100000];
int dp[100000]; 
int main(){
	int N;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>a[i];
	int t=INT_MIN;
	dp[0]=0;
	for(int i=1;i<=N;i++){
		dp[i]=max(dp[i-1]+a[i-1],a[i-1]);
		t=max(t,dp[i]);
	}
	cout<<max(t,dp[0])<<endl;
	return 0;
}
Published 37 original articles · won praise 0 · Views 375

Guess you like

Origin blog.csdn.net/weixin_45351699/article/details/104781756