fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. //* Kadanes Algo :
  26.  
  27. //* P[R] = max sum subarray ending at R
  28. //* P[R] = max(P[R-1] + a[R], a[R]);
  29.  
  30. //* In case of Max +ve sum subarray :
  31. //* P[R] = max( P[R-1] + a[R], a[R], 0 )
  32.  
  33. //* Prefix Dp :
  34. //* dp[R] = max(P[R] , dp[R-1] )
  35.  
  36.  
  37.  
  38. vector<int> a;
  39. void consistency(int n) {
  40.  
  41. int prev = 0;
  42.  
  43. //* Negative sum allowed here
  44. int maxi = INT32_MIN;
  45. vector<int> Prefix(n);
  46.  
  47. for(int i = 0; i < n; i++){
  48. int curr = max(a[i], prev + a[i]);
  49. prev = curr;
  50. maxi = max(maxi, curr);
  51. Prefix[i] = maxi;
  52. }
  53.  
  54. cout << maxi << endl;
  55. }
  56.  
  57. void solve() {
  58.  
  59. int n;
  60. cin >> n;
  61. a.resize(n);
  62. for(int i=0; i<n; i++) cin >> a[i];
  63.  
  64. consistency(n);
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int32_t main() {
  73. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  74.  
  75. int t = 1;
  76. cin >> t;
  77. while (t--) {
  78. solve();
  79. }
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5276KB
stdin
2
1
-1
9
-2 1 -3 4 -1 2 1 -5 4
stdout
-1
6