#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%d %lld",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define umax(a,b) a = max(a,b);
#define umin(a,b) a = min(a,b);
#define endi puts("");
#define eps 1e-12
const int N = 1e5+12,INF=1e18+7;
int q[N],res,dp[1001][1001];
vector <pii> g[N],ver[N];
void dfs(int x,int gl){
if (g[x].empty())ret ;
for (pii to:g[x])
dfs(to.fr,gl+1);
int i,j;
for (i=0;i<=600;++i){
dp[x][i]=0;
for (pii to:g[x]){
int mn = INF;
for (j=0;j<=i;++j){
mn = min(mn,dp[to.fr][i-j]+abs(to.sc-j));
}
dp[x][i] += mn;
}
}
}
main(){
int n,m,i,j,ans=INF,sum=0;
cin>>n>>m;
for (i=0;i<=1000;++i){
for (j=0;j<=1000;++j){
dp[i][j] = INF;
}
}
for (i=2;i<=m+n;++i){
int x,y;
cin>>x>>y;
g[x].pb({i,y});
if (i > n){
dp[i][0] = 0;
}
}
dfs(1,0);
for (i=1;i<=1000;++i){
ans = min(ans,dp[1][i]);
}
cout <<ans;
}
Compilation message
fireworks.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
54 | main(){
| ^~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:55:25: warning: unused variable 'sum' [-Wunused-variable]
55 | int n,m,i,j,ans=INF,sum=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12836 KB |
Output is correct |
2 |
Correct |
14 ms |
12836 KB |
Output is correct |
3 |
Correct |
19 ms |
12844 KB |
Output is correct |
4 |
Correct |
28 ms |
12848 KB |
Output is correct |
5 |
Correct |
32 ms |
12748 KB |
Output is correct |
6 |
Correct |
38 ms |
12748 KB |
Output is correct |
7 |
Correct |
39 ms |
12844 KB |
Output is correct |
8 |
Correct |
46 ms |
12748 KB |
Output is correct |
9 |
Correct |
48 ms |
12748 KB |
Output is correct |
10 |
Correct |
53 ms |
12748 KB |
Output is correct |
11 |
Correct |
60 ms |
12824 KB |
Output is correct |
12 |
Correct |
62 ms |
12828 KB |
Output is correct |
13 |
Correct |
65 ms |
12748 KB |
Output is correct |
14 |
Correct |
70 ms |
12844 KB |
Output is correct |
15 |
Correct |
74 ms |
12800 KB |
Output is correct |
16 |
Correct |
75 ms |
12748 KB |
Output is correct |
17 |
Correct |
74 ms |
12748 KB |
Output is correct |
18 |
Correct |
71 ms |
12848 KB |
Output is correct |
19 |
Correct |
73 ms |
12828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |