#include <bits/stdc++.h>
#define f first
#define s second
#define ent '\n'
using namespace std;
typedef long long ll;
const int mx=2e5+12;
vector<pair<int,ll>> g[mx];
ll need[mx];
ll prefmx[mx];
ll prefv[mx];
ll prefu[mx];
ll dmx[mx];
ll dv[mx];
ll du[mx];
int n;
ll k;
void dfs(int v,int p,ll d[]){
if(p<0){
d[v]=0;
}
dmx[v]=max(d[v],dmx[v]);
for(auto To:g[v]){
int to=To.f;
ll w=To.s;
if(to!=p){
d[to]=d[v]+w;
dfs(to,v,d);
}
}
}
ll get(int l,int r,ll pref[]){
if(!l)return pref[r];
return pref[r]-pref[l-1];
}
int max_score(int N, int x, int y, long long K, std::vector<int> U, std::vector<int> V, std::vector<int> W){
n=N,k=K;
int ans=2;
for(int i=0;i<n;i++){
g[i].clear();
dmx[i]=dv[i]=du[i]=0;
}
for(int i=0;i<n-1;i++){
g[V[i]].push_back({U[i],W[i]});
g[U[i]].push_back({V[i],W[i]});
}
dfs(x,-1,dv);
dfs(y,-1,du);
prefv[0]=dv[0],prefu[0]=du[0];
prefmx[0]=dmx[0];
for(int i=1;i<n;i++){
prefv[i]=prefv[i-1]+dv[i];
prefu[i]=prefu[i-1]+du[i];
prefmx[i]=prefmx[i-1]+dmx[i-1];
}
int cnt=0;
for(int l=x-1,r=y+1;l>=0 || r<n;){
cnt++;
if(r==n || l!=-1 && dv[l]<du[r]){
need[cnt]=need[cnt-1]+dv[l];
l--;
}
else{
need[cnt]=need[cnt-1]+du[r];
r++;
}
if(need[cnt]<=k){
ans=max(ans,cnt+2);
}
}
for(int i=cnt+1;i<=n;i++){
need[i]=1e18+1;
}
for(int l=x;l<y;l++){
for(int r=y;r>x;r--){
ll cost=0;
int pos=0;
if(l<r){
cost=get(x,l,prefv)+get(r,y,prefu);
}
else{
cost=get(x,r-1,prefv)+get(l+1,y,prefu)+get(r,l,prefmx);
}
for(int l=0,r=n;l<=r;){
int mid=l+r>>1;
if(need[mid]+cost<=k){
pos=mid;
l=mid+1;
}
else r=mid-1;
}
if(cost<=k)ans=max(ans,l-x+2+y-r+pos);
}
}
for(int l1=x;l1>=0;l1--){
for(int r1=x;r1<n;r1++){
for(int l2=y;l2>=0;l2--){
for(int r2=y;r2<n;r2++){
long long sum=0,cnt=0;
for(int i=0;i<n;i++){
long long t=0;
if(l1<=i && i<=r1){
t=dv[i];
cnt++;
}
if(l2<=i && i<=r2){
t=max(t,du[i]);
cnt++;
}
sum+=t;
}
if(sum<=k){
ans=max((ll)ans,cnt);
}
}
}
}
}
return ans;
}
Compilation message
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:64:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
64 | if(r==n || l!=-1 && dv[l]<du[r]){
| ~~~~~~^~~~~~~~~~~~~~
closing.cpp:90:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1069 ms |
35432 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
3 ms |
13856 KB |
Output is correct |
6 |
Correct |
49 ms |
13660 KB |
Output is correct |
7 |
Correct |
38 ms |
13876 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
15708 KB |
Output is correct |
11 |
Correct |
5 ms |
15708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
3 ms |
13856 KB |
Output is correct |
6 |
Correct |
49 ms |
13660 KB |
Output is correct |
7 |
Correct |
38 ms |
13876 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
15708 KB |
Output is correct |
11 |
Correct |
5 ms |
15708 KB |
Output is correct |
12 |
Correct |
156 ms |
13860 KB |
Output is correct |
13 |
Correct |
103 ms |
13660 KB |
Output is correct |
14 |
Correct |
324 ms |
13660 KB |
Output is correct |
15 |
Correct |
148 ms |
13860 KB |
Output is correct |
16 |
Correct |
5 ms |
15708 KB |
Output is correct |
17 |
Correct |
5 ms |
15900 KB |
Output is correct |
18 |
Correct |
15 ms |
13768 KB |
Output is correct |
19 |
Execution timed out |
1032 ms |
13912 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
3 ms |
13856 KB |
Output is correct |
6 |
Correct |
49 ms |
13660 KB |
Output is correct |
7 |
Correct |
38 ms |
13876 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
5 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
15708 KB |
Output is correct |
11 |
Correct |
5 ms |
15708 KB |
Output is correct |
12 |
Correct |
156 ms |
13860 KB |
Output is correct |
13 |
Correct |
103 ms |
13660 KB |
Output is correct |
14 |
Correct |
324 ms |
13660 KB |
Output is correct |
15 |
Correct |
148 ms |
13860 KB |
Output is correct |
16 |
Correct |
5 ms |
15708 KB |
Output is correct |
17 |
Correct |
5 ms |
15900 KB |
Output is correct |
18 |
Correct |
15 ms |
13768 KB |
Output is correct |
19 |
Execution timed out |
1032 ms |
13912 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
13656 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |