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);
  12. vector<int> pref(n+1, 0);
  13.  
  14. for (int i = 1; i <= n; i++) {
  15. cin >> a[i];
  16. pref[i] = pref[i-1] + a[i];
  17. }
  18.  
  19. int original_sum = pref[n];
  20. int best = 0;
  21.  
  22. long long bestVal = LLONG_MAX;
  23.  
  24. for (int r = 1; r <= n; r++) {
  25.  
  26. long long cur = pref[r-1] - (1LL * r * (r + 1));
  27. bestVal = min(bestVal, cur);
  28.  
  29. long long gain = 1LL * r * (r + 1) - pref[r] - bestVal;
  30.  
  31. best = max(best, gain);
  32. }
  33.  
  34. cout << original_sum + best << "\n";
  35. }
  36.  
  37. int32_t main() {
  38. fast_io;
  39. int t;
  40. cin >> t;
  41. while (t--) solve();
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
3
2 5 1
2
4 4
4
1 3 2 1
5
3 2 0 9 10
stdout
17
8
34
46