#include<bits/stdc++.h>
#define lf double
#define ll long long
#define cc pair<char,char>
#define ull unsigned ll
#define ii pair<int,int>
#define li pair<ll,int>
#define iii pair<int,ii>
#define iiii pair<ii,ii>
#define iiii2 pair<int,iii>
#define lii pair<ll,ii>
#define lolo pair<ll,ll>
#define heap priority_queue
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) x.begin(),x.end()
#define len(x) strlen(x)
#define sz(x) (int) x.size()
#define orta ((bas+son)/2)
#define min3(x,y,z) min(min(x,y),z)
#define max3(x,y,z) max(max(x,y),z)
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define MOD 1000000007
#define inf 100000000000000000
#define M 10000002
#define N 5005
#define LOG 20
#define magic 100
#define KOK 250
#define EPS 0.0025
#define pw(x) ((1ll)<<(x))
#define PI 3.1415926535
using namespace std;
ll ans=inf,mn[N],mx[N];
int n,m,x,y,cst[N];
vector<ii> v[N];
ll solve(int node,ll cost,ll lazy=0) {
ll res=0;
if(node>1) {
if(mx[node]+lazy<cost) {
res+=cost-mx[node]-lazy;
lazy+=cost-mx[node]-lazy;
}
else if(mn[node]+lazy>cost) {
res+=mn[node]+lazy-cost;
lazy-=mn[node]+lazy-cost;
}
}
for(ii i:v[node]) {
res+=solve(i.st,cost,lazy);
}
return res;
}
void dfs1(int node,ll sum=0) {
if(node>n) mn[node]=mx[node]=sum;
for(ii i:v[node]) {
dfs1(i.st,sum+i.nd);
umin(mn[node],mn[i.st]);
umax(mx[node],mx[i.st]);
}
}
int main() {
#if 0
freopen("input.txt","r",stdin);
#endif
fill(mn,mn+N,inf);
fill(mx,mx+N,-inf);
scanf("%d %d",&n,&m);
for(int i=2;i<=n;i++) {
scanf("%d %d",&x,&y);
v[x].pb({i,y});
}
for(int i=1;i<=m;i++) {
scanf("%d %d",&x,&y);
cst[i+n]=y;
v[x].pb({i+n,y});
}
dfs1(1);
for(int i=n+1;i<=n+m;i++) {
umin(ans,solve(1,mn[i]));
}
ll cev=ans;
for(int i=0;i<=10000;i++) umin(ans,solve(1,i));
assert(ans==cev);
printf("%lld",ans);
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
664 KB |
Output is correct |
3 |
Correct |
10 ms |
680 KB |
Output is correct |
4 |
Correct |
12 ms |
680 KB |
Output is correct |
5 |
Correct |
7 ms |
756 KB |
Output is correct |
6 |
Correct |
8 ms |
756 KB |
Output is correct |
7 |
Correct |
9 ms |
756 KB |
Output is correct |
8 |
Correct |
12 ms |
756 KB |
Output is correct |
9 |
Correct |
10 ms |
756 KB |
Output is correct |
10 |
Correct |
9 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
864 KB |
Output is correct |
2 |
Incorrect |
4 ms |
868 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
664 KB |
Output is correct |
3 |
Correct |
10 ms |
680 KB |
Output is correct |
4 |
Correct |
12 ms |
680 KB |
Output is correct |
5 |
Correct |
7 ms |
756 KB |
Output is correct |
6 |
Correct |
8 ms |
756 KB |
Output is correct |
7 |
Correct |
9 ms |
756 KB |
Output is correct |
8 |
Correct |
12 ms |
756 KB |
Output is correct |
9 |
Correct |
10 ms |
756 KB |
Output is correct |
10 |
Correct |
9 ms |
864 KB |
Output is correct |
11 |
Correct |
3 ms |
864 KB |
Output is correct |
12 |
Incorrect |
4 ms |
868 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
664 KB |
Output is correct |
3 |
Correct |
10 ms |
680 KB |
Output is correct |
4 |
Correct |
12 ms |
680 KB |
Output is correct |
5 |
Correct |
7 ms |
756 KB |
Output is correct |
6 |
Correct |
8 ms |
756 KB |
Output is correct |
7 |
Correct |
9 ms |
756 KB |
Output is correct |
8 |
Correct |
12 ms |
756 KB |
Output is correct |
9 |
Correct |
10 ms |
756 KB |
Output is correct |
10 |
Correct |
9 ms |
864 KB |
Output is correct |
11 |
Correct |
3 ms |
864 KB |
Output is correct |
12 |
Incorrect |
4 ms |
868 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |