fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int sum, n, a[1000009], a1[1000009];
  4. int duyet , duyet1, maxn , maxn1 , ans;
  5. void inp(){
  6. cin >> n;
  7. for (int i = 1; i <= n; i++){
  8. cin >> a[i];
  9. a1[i] = (-1) * a[i];
  10. sum += a[i];
  11. }
  12. }
  13. int kadane(){
  14. for (int i = 1; i <= n; i++){
  15. duyet += a[i];
  16. if (duyet < 0){
  17. duyet = 0;
  18. }
  19. maxn = max (maxn, duyet);
  20. }
  21. int tong1 = maxn;
  22. for (int i = 1; i <= n; i++){
  23. duyet1 += a1[i];
  24. if (duyet1 < 0){
  25. duyet1 = 0;
  26. }
  27. maxn1 = max (maxn1, duyet1);
  28. }
  29. int tong2 = sum - ((-1)*maxn1);
  30. if (maxn1 + sum == 0){
  31. return tong1;
  32. }
  33. ans = max (tong1, tong2);
  34. return ans;
  35. }
  36. int main(){
  37. ios_base::sync_with_stdio(false);
  38. cin.tie(0); cout.tie(0);
  39. freopen("CAU3.INP", "r", stdin);
  40. freopen("CAU3.OUT", "w", stdout);
  41. inp();
  42. cout << kadane();
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty