fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(false); cin.tie(NULL)
  5. #define int long long
  6.  
  7. void solve() {
  8. int n;
  9. cin >> n;
  10.  
  11. vector<int> a(n+1), pref(n+1, 0);
  12. for (int i = 1; i <= n; i++) {
  13. cin >> a[i];
  14. pref[i] = pref[i-1] + a[i];
  15. }
  16.  
  17. long long original = pref[n];
  18. long long best_gain = 0;
  19.  
  20. long long bestB = LLONG_MIN;
  21.  
  22. for (int r = 1; r <= n; r++) {
  23.  
  24. long long B_r = pref[r-1] - 1LL*r*r + r;
  25. bestB = max(bestB, B_r);
  26.  
  27. long long A_r = 1LL*r*r + r - pref[r];
  28.  
  29. long long gain = A_r + bestB;
  30.  
  31. best_gain = max(best_gain, gain);
  32. }
  33.  
  34. cout << original + best_gain << "\n";
  35. }
  36.  
  37. int32_t main() {
  38. fast_io;
  39. int t;
  40. cin >> t;
  41. while (t--) solve();
  42. }
  43.  
Success #stdin #stdout 0.01s 5292KB
stdin
4
3
2 5 1
2
4 4
4
1 3 2 1
5
3 2 0 9 10
stdout
13
8
20
32