This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shortcut.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 3000 + 5;
const ll inf = 1e15;
int n;
ll cost;
ll len[maxn], val[maxn];
ll sum[maxn];
ll L[maxn], R[maxn], mxL[maxn], mxR[maxn];
ll ii[maxn][maxn];
ll wow[maxn][maxn];
ll solve() {
    for(int s=0;s<n;s++) sum[s] = sum[s-1] + len[s-1];
    ll tmp;
    tmp = 0;
    for(int s=0;s<n;s++) {
        mxL[s] = max(mxL[s-1], tmp + val[s]);
        tmp = max(tmp, val[s]);
        L[s] = tmp;
        tmp += len[s];
    }
    tmp = 0;
    for(int t=n-1;t>=0;t--) {
        mxR[t] = max(mxR[t+1], tmp + val[t]);
        tmp = max(tmp, val[t]);
        R[t] = tmp;
        tmp += len[t-1];
    }
    ll ans = mxL[n-1];
    //out - out
    for(int s=0;s<n;s++) {
        for(int t=s+1;t<n;t++) {
            wow[s][t] = max(wow[s][t], max(mxL[s], mxR[t]));
            wow[s][t] = max(wow[s][t], L[s] + R[t] + cost);
        }
    }
    //in - out
    for(int s=0;s<n;s++) {
        tmp = -inf;
        for(int t=s+1,x=s+1;t<n;t++) {
            while(x<t && sum[t]-sum[x] >= sum[x]-sum[s]+cost) {
                tmp = max(tmp, sum[x] + val[x]);
                x++;
            }
            wow[s][t] = max(wow[s][t], -sum[s] + tmp + cost + R[t]);
        }
    }
    for(int t=0;t<n;t++) {
        tmp = -inf;
        for(int s=t-1,x=t-1;s>=0;s--) {
            while(x>s && sum[x]-sum[s] >= sum[t]-sum[x]+cost) {
                tmp = max(tmp, -sum[x] + val[x]);
                x--;
            }
            wow[s][t] = max(wow[s][t], sum[t] + tmp + cost + L[s]);
        }
    }
    for(int s=0;s<n;s++) {
        tmp = -inf;
        for(int t=s+1,x=s+1;t<n;t++) {
            while(x<t && sum[t]-sum[x]+cost >= sum[x]-sum[s]) {
                tmp = max(tmp, sum[x] + val[x]);
                x++;
            }
            wow[s][t] = max(wow[s][t], -sum[s] + tmp + L[s]);
        }
    }
    for(int t=0;t<n;t++) {
        tmp = -inf;
        for(int s=t-1,x=t-1;s>=0;s--) {
            while(x>s && sum[x]-sum[s]+cost >= sum[t]-sum[x]) {
                tmp = max(tmp, -sum[x] + val[x]);
                x--;
            }
            wow[s][t] = max(wow[s][t], sum[t] + tmp + R[t]);
        }
    }
    //in-in
    for(int s=0;s<n;s++) {
        for(int t=s+1;t<n;t++) {
            ll tmp = 0;
//            printf("(%d, %d)\n",s,t);
            //in - in
            for(int x=s;x<=t;x++) {
                for(int y=x+1;y<=t;y++) {
                    tmp = max(tmp, min(sum[x]-sum[y]-sum[s]+sum[t]+cost, sum[y]-sum[x]) + val[x] + val[y]);
                }
            }
            ii[s][t] = tmp;
            assert(ii[s][t] >= ii[s][t-1]);
            ans = min(ans, max(tmp, wow[s][t]));
//            printf("\tcost = %lld and %lld\n",tmp,wow[s][t]);
        }
    }
    return ans;
}
ll find_shortcut(int N, vector<int> l, vector<int> d, int c) {
    n = N; cost = c;
    for(int i=0;i<n-1;i++) len[i] = l[i];
    for(int i=0;i<n;i++) val[i] = d[i];
    return solve();
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |