Advertisement
volochai

CF344 E

Jul 9th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define ld long double
  5. #define all(x) (x).begin(), (x).end()
  6. #define rall(x) (x).rbegin(), (x).rend()
  7. #define watch(x) cout << (#x) << " : " << x << '\n'
  8. #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  9.  
  10. using namespace std;
  11.  
  12. #define int ll
  13.  
  14. int n;  
  15.  
  16. vector < pair<ll, ll> > slope;  
  17.  
  18. ll f(ll x, int id) {
  19.     assert(id < (int)slope.size());
  20.     return x * slope[id].first + slope[id].second;
  21. }
  22.  
  23. const ll INF = 1e18;
  24.  
  25. ld cross(pair<ll, ll> L, pair<ll, ll> R) {
  26.     ld A = (ld)R.second - L.second;
  27.     ld B = (ld)L.first - R.first;
  28.     if (B == .0)
  29.         return INF;
  30.     return A / B;
  31. }  
  32.  
  33. ll get(ll x) {
  34.     if (slope.empty())
  35.         return -INF;
  36.     if ((int)slope.size() == 1)
  37.         return f(x, 0);
  38.     if (f(x, 0) >= f(x, 1))
  39.         return f(x, 0);
  40.     if (f(x, (int)slope.size() - 2) <= f(x, (int)slope.size() - 1))
  41.         return f(x, (int)slope.size() - 1);
  42.  
  43.     int l = 1, r = (int)slope.size() - 2;
  44.     assert(l <= r);
  45.  
  46.     while (l + 1 < r) {
  47.         int m = (l + r) >> 1;
  48.         if (f(x, m - 1) <= f(x, m))
  49.             l = m;
  50.         else
  51.             r = m;
  52.     }
  53.  
  54.     return max(f(x, l), f(x, r));
  55. }
  56.  
  57. void add_line(pair<ll, ll> pt) {
  58.     int sz = (int)slope.size();  
  59.  
  60.     while (sz >= 2 && cross(slope[sz - 2], pt) >= cross(slope[sz - 1], pt))
  61.         slope.pop_back(), sz--;
  62.  
  63.     slope.push_back(pt);
  64. }
  65.  
  66. const int N = 2e5 + 128;
  67.  
  68. ll sum[2][N];
  69. ll pref[N], suf[N];
  70. ll a[N];
  71.  
  72. void solve() {  
  73.     cin >> n;
  74.  
  75.     for (int i = 1; i <= n; i++)
  76.         cin >> a[i];
  77.  
  78.     for (int i = 1; i <= n; i++) {
  79.         pref[i] = pref[i - 1] + a[i] * i;
  80.         sum[0][i] = sum[0][i - 1] + a[i];
  81.     }
  82.  
  83.     for (int i = n; i >= 1; i--) {
  84.         suf[i] = suf[i + 1] + a[i] * i;
  85.         sum[1][i] = sum[1][i + 1] + a[i];
  86.     }
  87.  
  88.     ll answer = pref[n];
  89.  
  90.     for (int i = 1; i <= n; i++) {
  91.         ll current = sum[0][i - 1] + pref[n] + get(a[i]) - a[i] * i;
  92.         answer = max(answer, current);
  93.         add_line(make_pair(i, -sum[0][i - 1]));
  94.     }
  95.      
  96.     slope.clear();
  97.    
  98.     for (int i = n; i >= 1; i--) {
  99.         ll current = sum[0][i] + pref[n] + get(a[i]) - a[i] * i;
  100.         answer = max(answer, current);
  101.         add_line(make_pair(i, -sum[0][i]));
  102.     }
  103.  
  104.     cout << answer << '\n';
  105. }  
  106.  
  107. int32_t main() {
  108.     boost;    
  109.     solve();
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement