Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> piipi;
typedef pair<pii, pii> piipii;

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back

int t[300005];
int p[300005], x[300005];

bool Q;
struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const {
        return Q ? p < o.p : k < o.k;
    }
};
 
struct LineContainer : multiset<Line> {
    const ll inf = LLONG_MAX;
    void init(){
        clear();
    }
    ll div(ll a, ll b){
        return a / b - ((a ^ b) < 0 && a % b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) { x->p = inf; return false; }
        if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m) {
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        Q = 1; auto l = *lower_bound({0,0,x}); Q = 0;
        return l.k * x + l.m;
    }
}C;
ll pref[300005], suff[300005];
ll sum[300005];
ll dp[300005];

void dfs(int s, int e){
    if(s == e){
        dp[s] = max(dp[s], 1-t[s]);
        return;
    }
    int mid = (s+e)/2;
    dfs(s, mid);
    dfs(mid+1, e);

    ll mx = -1e18;
    C.init();
    for(int i=mid+1;i<=e;i++){
        int r = i;
        ll k = -r;
        ll m = (r*1ll*r + r)/2 + suff[i+1] - sum[i];
        C.add(k, m);
    }
    for(int i=s;i<=mid;i++){
        int l = i-1;
        mx = max(mx, C.query(l) + (l*1ll*l - l)/2 + sum[i-1]);
        dp[i] = max(dp[i], mx);
    }

    mx = -1e18;
    C.init();
    for(int i=s;i<=mid;i++){
        int l = i-1;
        ll k = -l;
        ll m = (l*1ll*l - l)/2 + pref[i-1] + sum[i-1];
        C.add(k, m);
    }
    for(int i=e;i>=mid+1;i--){
        int r = i;
        mx = max(mx, C.query(r) + (r*1ll*r + r)/2 - sum[i]);
        dp[i] = max(dp[i], mx);
    }
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i=1;i<=n;i++) scanf("%d", &t[i]), sum[i] = sum[i-1] + t[i];
    for(int i=1;i<=n;i++) dp[i] = -1e18;
    int m;
    scanf("%d", &m);
    for(int i=1;i<=m;i++) scanf("%d%d", &p[i], &x[i]);

    C.init();
    for(int i=1;i<=n;i++){
        int l = i-1, r = i;
        ll k = -l;
        ll m = (l*1ll*l - l)/2 + pref[i-1] + sum[i-1];
        C.add(k, m);
        pref[i] = C.query(r) + (r*1ll*r + r)/2 - sum[i];
        pref[i] = max(pref[i], pref[i-1]);
    }

    C.init();
    for(int i=n;i>=1;i--){
        int l = i-1, r = i;
        ll k = -r;
        ll m = (r*1ll*r + r)/2 + suff[i+1] - sum[i];
        C.add(k, m);
        suff[i] = C.query(l) + (l*1ll*l - l)/2 + sum[i-1];
        suff[i] = max(suff[i], suff[i+1]);
    }
    dfs(1, n);
    for(int i=1;i<=m;i++) printf("%lld\n", max(pref[p[i]-1] + suff[p[i]+1], dp[p[i]] + t[p[i]] - x[i]));
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:60:20: error: no matching function for call to 'max(ll&, int)'
   60 |         dp[s] = max(dp[s], 1-t[s]);
      |                 ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:60:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   60 |         dp[s] = max(dp[s], 1-t[s]);
      |                 ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:60:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   60 |         dp[s] = max(dp[s], 1-t[s]);
      |                 ~~~^~~~~~~~~~~~~~~