fork download
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3. using namespace std;
  4. using namespace chrono;
  5. // "AJEET JAIN"----"JAI JINENDRA"
  6. /* "णमो अरिहंताणं",
  7.   "णमो सिद्धाणं",
  8.   "णमो आयरियाणं",
  9.   "णमो उवज्झायाणं",
  10.   "णमो लोए सव्वसाहूणं",
  11.   "",
  12.   "एसो पंच नमोक्कारो, सव्व पावप्पणासणो",
  13.   "मंगलाणं च सव्वेसिं, पडमं हवै मंगलं", */
  14.  
  15.  
  16. // Aliases to op
  17. using ll = long long;
  18. using ull = unsigned long long;
  19. using ld = double;
  20. using vll = vector<ll>;
  21.  
  22.  
  23. // Constants
  24. constexpr ll INF = 4e18;
  25. constexpr ld EPS = 1e-9;
  26. constexpr ll MOD = 1e9 + 7;
  27.  
  28.  
  29.  
  30. // Macros
  31. #define F first
  32. #define S second
  33. #define all(x) begin(x), end(x)
  34. #define allr(x) rbegin(x), rend(x)
  35. #define py cout<<"YES\n";
  36. #define pn cout<<"NO\n";
  37. #define forn(i,n) for(int i=0;i<n;i++)
  38. #define for1(i,n) for(int i=1;i<=n;i++)
  39.  
  40. // #define insert push_back
  41. #define pb push_back
  42. #define MP make_pair
  43. #define endl '\n'
  44.  
  45. /*
  46.   remove substring or subarray ---> try to think about sliding w
  47.  
  48.   */
  49.  
  50. /*
  51.  
  52.   Golden Rule
  53.  
  54.   1) problem is easy
  55.   2) proofs is easy
  56.   3) implementation is easy
  57.  
  58.   /*
  59.   ROUGH --
  60.  
  61.   // kadan_lft = prefixmaxSum of left
  62.   // kadan_rgt = PrfixMaxSum of right
  63.  
  64.   */
  65.  
  66. void AJNJ(){
  67. ll n;
  68. cin >> n;
  69.  
  70. vector<ll> v(n + 1 , 0);
  71. for(int i = 1 ; i <= n ; i++){
  72. cin >> v[i];
  73. }
  74. vector<ll> kadan_lft(n + 1 , 0);
  75. for(int i = 1 ; i <= n ; i++){
  76. kadan_lft[i] = max({ll(0) , v[i] , kadan_lft[i - 1] + v[i]});
  77. }
  78.  
  79. vector<ll> kadan_rgt(n + 1 , 0);
  80. kadan_rgt[n] = max(ll(0) , v[n]);
  81. for(int i = n - 1 ; i >= 1 ; i--){
  82. kadan_rgt[i] = max({ll(0) , v[i] , kadan_rgt[i + 1] + v[i]});
  83. }
  84.  
  85. vector<ll> max_lft_kadn(n + 1 , 0);
  86. for(int i = 1 ; i <= n ; i++){
  87. max_lft_kadn[i] = max(max_lft_kadn[i - 1] , kadan_lft[i]);
  88. }
  89.  
  90. vector<ll> max_rgt_kadn(n + 2 , 0);
  91. max_rgt_kadn[n] = max(ll(0) , kadan_rgt[n]);
  92. for(int i = n - 1 ; i >= 0 ; i--){
  93. max_rgt_kadn[i] = max(max_rgt_kadn[i + 1] , kadan_rgt[i]);
  94. }
  95.  
  96. vector<ll> answer(n + 1 , 0);
  97. for(int i = 1 ; i <= n ; i++){
  98. answer[i] = max(answer[i] , max_rgt_kadn[i + 1] + max_lft_kadn[i]);
  99. }
  100.  
  101. sort(all(answer));
  102. cout << answer[n] << endl;
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. int main(){
  110. ios::sync_with_stdio(0);
  111. cin.tie(0);
  112. cout.tie(0);
  113. int T = 1;
  114. cin>>T;
  115. auto start1 = high_resolution_clock::now();
  116. while(T--){
  117. AJNJ();
  118. }
  119. auto stop1 = high_resolution_clock::now();
  120. auto duration = duration_cast<microseconds>(stop1 - start1);
  121. cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
  122.  
  123. return 0;
  124. }
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
0
stderr
Time: 0 ms