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. //* Negative sum allowed here
  36. vector<int> a;
  37. int consistency(int n) {
  38.  
  39. int last = 0;
  40.  
  41. int maxi = INT32_MIN;
  42.  
  43. vector<int> PrefixMax(n);
  44.  
  45. for(int i = 0; i < n; i++){
  46. int curr = max(a[i], last + a[i]);
  47. last = 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. //* Negative sum allowed here
  72.  
  73. int practice(int n) {
  74.  
  75.  
  76.  
  77. return 0;
  78. }
  79.  
  80.  
  81.  
  82.  
  83. void solve() {
  84.  
  85. int n;
  86. cin >> n;
  87. a.resize(n);
  88. for(int i=0; i<n; i++) cin >> a[i];
  89.  
  90. cout << consistency(n) << endl;
  91. // cout << consistency(n) << " -> " << practice(n) << endl;
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. int32_t main() {
  100. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  101.  
  102. int t = 1;
  103. cin >> t;
  104. while (t--) {
  105. solve();
  106. }
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0s 5316KB
stdin
2
1
-1
9
-2 1 -3 4 -1 2 1 -5 4
stdout
-1
6