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;
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 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];
    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]);
            //out - out
            tmp = max(tmp, max(max(mxL[s], mxR[t]), L[s] + R[t] + cost));
            //in - out
            for(int x=s+1;x<=t-1;x++) {
                tmp = max(tmp, min(sum[x]-sum[s], sum[t]-sum[x]+cost) + val[x] + L[s]);
                tmp = max(tmp, min(sum[x]-sum[s]+cost, sum[t]-sum[x]) + val[x] + R[t]);
            }
            ans = min(ans, tmp);
//            printf("\tcost = %lld\n",tmp);
        }
    }
    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... |