#include<bits/stdc++.h>
#define MAXN 1007
using namespace std;
const long long inf=1e15;
int n;
long long pref[MAXN],suff[MAXN],sum[MAXN],dist[MAXN];
long long curr,ans,c,d1,d2,e,w[MAXN];
multiset<long long> s;
multiset<long long>::iterator it;
long long getmax(){
it=s.end(); it--;
return *it;
}
void dfs(int x,int p,int l,int r,long long lim,long long s){
e=max(e,s+dist[x]);
if(x==l and p!=r and s+c<=lim)dfs(r,l,l,r,lim,s+c);
if(x==r and p!=l and s+c<=lim)dfs(l,r,l,r,lim,s+c);
if(x<r and x+1!=p and s+sum[x+1]-sum[x]<=lim)dfs(x+1,x,l,r,lim,s+sum[x+1]-sum[x]);
if(x>l and x-1!=p and s+sum[x]-sum[x-1]<=lim)dfs(x-1,x,l,r,lim,s+sum[x]-sum[x-1]);
}
long long cycle(int from,int to){
long long ss=sum[to]-sum[from]+c,res=0,len=0,torem=0;
int pt=from;
s.clear();
for(int i=from;i<=to;i++){
if(pt>=i and !s.empty())s.erase(s.find(w[i]));
pt=max(pt,i);
while(true){
if(pt>=i and pt<to and sum[pt+1]-sum[i]<=ss/2){
len=sum[pt+1]-sum[from]; pt++; w[pt]=len+dist[pt]; s.insert(w[pt]);
}else if(pt==to and sum[to]-sum[i]+c<=ss/2){
len=sum[to]-sum[from]+c; pt=from; w[pt]=len+dist[pt]; s.insert(w[pt]);
}else if(pt<i and sum[to]-sum[i]+c+sum[pt+1]-sum[from]<=ss/2){
len=sum[to]-sum[from]+c+sum[pt+1]-sum[from]; pt++; w[pt]=len+dist[pt]; s.insert(w[pt]);
}else break;
}
if(!s.empty())res=max(res,getmax()-torem+dist[i]);
torem+=sum[i+1]-sum[i];
}
e=0; dfs(from,0,from,to,ss/2,0); d1=e;
e=0; dfs(to,0,from,to,ss/2,0); d2=e;
return res;
}
long long find_shortcut(int N,vector<int> l,vector<int> d, int C){
n=N; c=C;
for(int i=1;i<=n;i++){
dist[i]=d[i-1];
}
pref[1]=d[0]; sum[1]=0;
for(int i=0;i<l.size();i++){
pref[i+2]=max(pref[i+1]+l[i],d[i+1]*1LL);
sum[i+2]=sum[i+1]+l[i];
}
suff[n]=d[n-1];
for(int i=l.size()-1;i>=0;i--){
suff[i+1]=max(suff[i+2]+l[i],d[i]*1LL);
}
ans=inf;
for(int i=1;i<=n;i++){
for(int f=i+1;f<=n;f++){
curr=min(pref[i]+c+suff[f],pref[i]+sum[f]-sum[i]+suff[f]);
curr=max(curr,cycle(i,f));
if(i>1)curr=max(curr,max(pref[i-1]+l[i-2]+d1,d2+l[f-1]+suff[f+1]));
else curr=max(curr,max(pref[i-1]+d1,d2+l[f-1]+suff[f+1]));
ans=min(ans,curr);
}
}
return ans;
}
/*
int main(){
//cout<<find_shortcut(4, {10, 20, 20}, {0, 40, 0, 30}, 10)<<"\n";
//cout<<find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10}, {20, 0, 30, 0, 0, 40, 0, 40, 0}, 30)<<"\n";
//cout<<find_shortcut(4, {2, 2, 2}, {1, 10, 10, 1}, 1)<<"\n";
//cout<<find_shortcut(3, {1, 1},{1, 1, 1}, 3)<<"\n";
}
*/
Compilation message
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<l.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
212 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
264 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
212 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
212 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
212 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
300 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
212 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
304 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
212 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
212 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
212 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
300 KB |
n = 5, 4000000000 is a correct answer |
17 |
Incorrect |
1 ms |
304 KB |
n = 10, incorrect answer: jury 1000000343 vs contestant 1000000358 |
18 |
Halted |
0 ms |
0 KB |
- |