#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5000, inf=1e18;
int n, m, c, ll[N];
vector<pair<int, int>> g[N];
int tl, tr;
pair<int, int> f[N];
pair<int, int> dfs(int u, int p){
pair<int, int> ans;
int mx1=0, mx2=0;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (v==p || (tl<=v && v<=tr)) continue;
auto ff=dfs(v, u);
ans.first=max(ans.first, ff.first);
ans.second=max(ans.second, ff.second+w);
if (mx1<ff.second+w) mx2=mx1, mx1=ff.second+w;
else mx2=max(mx2, ff.second+w);
}
ans.first=max({ans.first, ans.second, mx1+mx2});
return f[u]=ans;
}
int pf[N];
int calculate(int u, int v){
tl=u, tr=v;
pf[u]=0;
for (int i=u; i<v; ++i) pf[i+1]=pf[i]+ll[i];
int sum=pf[v]+c, ans=0;
for (int i=u; i<=v; ++i){
dfs(i, -1);
ans=max(ans, f[i].first);
}
deque<int> dq;
int mx=-inf;
for (int i=u; i<=v; ++i){
while (dq.size() && (pf[i]-pf[dq.front()])*2>sum){
mx=max(mx, f[dq.front()].second+pf[dq.front()]);
dq.pop_front();
}
while (dq.size() && f[dq.back()].second-pf[dq.back()]<f[i].second-pf[i]) dq.pop_back();
dq.push_back(i);
if (dq.size()) ans=max(ans, f[i].second+f[dq.front()].second+pf[i]-pf[dq.front()]);
if (mx!=-inf) ans=max(ans, f[i].second+sum-pf[i]+mx);
}
return ans;
}
long long find_shortcut(int32_t _n, vector<int32_t> _l, vector<int32_t> _d, int32_t _c)
{
n=_n, c=_c;
for (int i=0; i<n-1; ++i) g[i].emplace_back(i+1, _l[i]), g[i+1].emplace_back(i, _l[i]), ll[i]=_l[i];
m=n;
for (int i=0; i<n; ++i) if (_d[i]){
g[i].emplace_back(m, _d[i]);
g[m].emplace_back(i, _d[i]);
++m;
}
int ans=inf;
for (int i=0; i<n; ++i) for (int j=i+1; j<n; ++j){
ans=min(ans, calculate(i, j));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
348 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
348 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
0 ms |
564 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |