Created
February 12, 2013 12:55
-
-
Save rohith2506/4762670 to your computer and use it in GitHub Desktop.
maximum sum sub sequence (dp solution)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
maximum sub sequence in an array(dp solution) | |
@author:rohit | |
*/ | |
#include <iostream> | |
#include <stdio.h> | |
#include <vector> | |
using namespace std; | |
vector<int> v; | |
int dp(int n){ | |
int Min[n]; | |
for(int i=0;i<n;i++) | |
Min[i]=0; | |
if(v[0]>Min[0]) | |
Min[0]=v[0]; | |
else | |
Min[0]=0; | |
for(int i=1;i<n;i++){ | |
if(Min[i-1]+v[i]>0) | |
Min[i]=Min[i-1]+v[i]; | |
else | |
Min[i]=0; | |
} | |
int max=0; | |
for(int i=0;i<n;i++){ | |
if(Min[i]>max) | |
max=Min[i]; | |
} | |
return max; | |
} | |
int main(){ | |
int n; | |
cin>>n; | |
for(int i=0;i<n;i++){ | |
int x; | |
cin>>x; | |
v.push_back(x); | |
} | |
cout<<dp(n)<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment