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. //* dp[R] = max sum subarray ending at R
  28. //* dp[R] = max(dp[R-1] + a[R], a[R]);
  29.  
  30.  
  31. //* Prefix Dp :
  32. //* P[R] = max(dp[R] , P[R-1] )
  33.  
  34.  
  35.  
  36. vector<int> a;
  37. int consistency(int n) {
  38.  
  39. int prev = 0;
  40.  
  41. //* Negative sum allowed here
  42. int maxi = INT32_MIN;
  43. vector<int> PrefixMax(n);
  44.  
  45. for(int i = 0; i < n; i++){
  46. int curr = max(a[i], prev + a[i]);
  47. prev = curr;
  48. maxi = max(maxi, curr);
  49. PrefixMax[i] = maxi;
  50. }
  51.  
  52. return maxi;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int practice(int n) {
  73.  
  74.  
  75. return 0;
  76. }
  77.  
  78.  
  79.  
  80.  
  81. void solve() {
  82.  
  83. int n;
  84. cin >> n;
  85. a.resize(n);
  86. for(int i=0; i<n; i++) cin >> a[i];
  87.  
  88. cout << consistency(n) << endl;
  89. // cout << consistency(n) << " -> " << practice(n) << endl;
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. int32_t main() {
  98. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  99.  
  100. int t = 1;
  101. cin >> t;
  102. while (t--) {
  103. solve();
  104. }
  105.  
  106. return 0;
  107. }
Success #stdin #stdout 0s 5316KB
stdin
2
1
-1
9
-2 1 -3 4 -1 2 1 -5 4
stdout
-1
6