# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
946496 | n3rm1n | Fireworks (APIO16_fireworks) | C++17 | 1 ms | 760 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.
/// fireworks
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long MAXN = 5005;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long n, m;
long long p[MAXN], c[MAXN];
vector < pair < long long, long long > > g[MAXN];
void read()
{
cin >> n >> m;
for (long long i = 2; i <= n + m; ++ i)
{
cin >> p[i] >> c[i];
g[p[i]].push_back(make_pair(i, c[i]));
}
}
long long used[MAXN], dist[MAXN];
void dfs(long long beg, long long path)
{
used[beg] = 1;
dist[beg] = path;
long long nb, nb_cost;
for (long long i = 0; i < g[beg].size(); ++ i)
{
nb = g[beg][i].first;
nb_cost = g[beg][i].second;
if(!used[nb])
dfs(nb, path + nb_cost);
}
}
long long get_diff(long long x)
{
long long ans = 0;
for (long long i = n+1; i <= n+m; ++ i)
ans += max(dist[i], x) - min(dist[i], x);
return ans;
}
int main()
{
speed();
read();
dfs(1, 0);
if(n == 1)
{
vector < long long > f;
for (long long i = n+1; i <= n+m; ++ i)
{
f.push_back(dist[i]);
}
long long ans = 1e18;
for (long long i = 0; i < f.size(); ++ i)
ans = min(ans, get_diff(f[i]));
cout << ans << endl;
}
return 0;
}
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... |