fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8.  
  9. int a[n];
  10.  
  11. for(int i=0; i<n; i++)
  12. {
  13. cin>>a[i];
  14. }
  15.  
  16. int p1[n+1]= {0};
  17.  
  18. for(int i=1; i<=n; i++ )
  19. {
  20. p1[i] = max({p1[i-1]+a[i-1], a[i-1],0});
  21. }
  22.  
  23. // for(int i=0; i<=n; i++)
  24. // {
  25. // cout<<p1[i]<<" ";
  26. // }
  27. // cout<<endl;
  28.  
  29. // note that if all the ele of main array are negative then the ans will be max(a[1],.......a[n-1]);
  30. int ans = 0;
  31.  
  32. for(int i=0; i<=n; i++)
  33. {
  34. ans = max(ans,p1[i]);
  35. }
  36.  
  37. cout<<ans<<endl;
  38.  
  39. }
  40.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
2147479979