# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
218209 | SamAnd | Fireworks (APIO16_fireworks) | C++17 | 26 ms | 1792 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 m_p make_pair
const int N = 302;
const long long INF = 1000000009000000009;
int n, m;
int p[N];
vector<pair<int, int> > a[N];
long long dp[N][N];
long long dpp[N][N];
void dfs(int x)
{
if (x > n)
{
dp[x][0] = 0;
return;
}
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i].first;
dfs(h);
for (int j = 0; j < N; ++j)
{
for (int k = 0; j + k < N; ++k)
{
dpp[h][j + k] = min(dpp[h][j + k], dp[h][j] + abs(a[x][i].second - k));
}
}
}
for (int j = 0; j < N; ++j)
{
dp[x][j] = 0;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i].first;
dp[x][j] += dpp[h][j];
dp[x][j] = min(dp[x][j], INF);
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 2; i <= n; ++i)
{
int d;
scanf("%d%d", &p[i], &d);
a[p[i]].push_back(m_p(i, d));
}
for (int i = n + 1; i <= n + m; ++i)
{
int d;
scanf("%d%d", &p[i], &d);
a[p[i]].push_back(m_p(i, d));
}
for (int i = 1; i <= n + m; ++i)
{
for (int j = 0; j < N; ++j)
dpp[i][j] = dp[i][j] = INF;
}
dfs(1);
long long ans = INF;
for (int j = 0; j < N; ++j)
ans = min(ans, dp[1][j]);
printf("%lld\n", ans);
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... |