# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
400509 | Hazem | Fireworks (APIO16_fireworks) | C++14 | 36 ms | 5844 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F first
#define S second
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>
const int N = 2e5+10;
const int M = 2e3+10;
const LL INF = 1e9;
const LL LINF = 1e14;
const LL MOD = 1e9+7;
const double PI = 3.141592653589793;
vector<pair<int,LL>>adj[N];
LL dp[N][350];
set<LL>dis;
void dfs(int i,LL d){
if(adj[i].empty()){
dis.insert(d);
for(int j=1;j<=300;j++)
dp[i][j] = LINF;
}
for(auto x:adj[i]){
dfs(x.F,d+x.S);
for(int j=0;j<=300;j++){
LL mn = LINF;
for(int k=0;k<=j;k++)
mn = min(mn,dp[x.F][k]+abs(x.S-(abs(k-j))));
dp[i][j] += mn;
}
}
}
int main(){
//freopen("out.txt","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for(int i=2;i<=n+m;i++){
LL v,w;
scanf("%lld%lld",&v,&w);
adj[v].push_back({i,w});
}
dfs(1,0);
LL ans = LINF,val = 0;
for(int i=0;i<=300;i++){
if(dp[1][i]<ans)ans = dp[1][i],val = i;
}
if(dis.find(val)==dis.end())
return 0;
printf("%lld\n",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |