#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int MAX = 300 + 10;
const int BOUND = 300 + 10;
int N,M;
vector<vector<pair<int,int>>> ch(MAX);
vector<vector<int>> dp(MAX,vector<int>(BOUND));
vector<int> gen(int w) {
vector<int> res(BOUND);
for (int i = 0; i < BOUND; i++) res[i] = abs(w-i);
return res;
}
vector<int> merge1(vector<int> dp1, vector<int> dp2) {
vector<int> res(BOUND,INF);
for (int i = 0; i < BOUND; i++) {
for (int j = 0; i+j < BOUND; j++) {
res[i+j] = min(res[i+j], dp1[i] + dp2[j]);
}
}
return res;
}
vector<int> merge2(vector<int> dp1, vector<int> dp2) {
vector<int> res(BOUND,INF);
for (int i = 0; i < BOUND; i++) {
res[i] = dp1[i] + dp2[i];
}
return res;
}
void dfs(int u) {
if (ch[u].size() == 0) {
for (int i = 1; i < BOUND; i++) dp[u][i] = INF;
}
for (auto e: ch[u]) {
int v,w; tie(v,w) = e;
dfs(v);
//if (u == 3) cout << w << "\n";
dp[u] = merge2(dp[u],merge1(dp[v],gen(w)));
}
}
main() {
cin >> N >> M;
for (int i = 1; i < N+M; i++) {
int P,C; cin >> P >> C;
ch[P-1].push_back(make_pair(i,C));
}
dfs(0);
//cout << ch[2].size() << " " << ch[2][0].first << "\n";
// auto print = merge2(merge1(gen(3),dp[9]),merge1(gen(4),dp[8]));
// vector<int> print(BOUND,INF);
// print = dp[3];
// for (int i = 0; i <= 5; i++) cout << i << " " << print[i] << "\n";
cout << *min_element(dp[0].begin(),dp[0].end()) << "\n";
return 0;
}
Compilation message
fireworks.cpp:51:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
7 ms |
1152 KB |
Output is correct |
5 |
Correct |
7 ms |
1152 KB |
Output is correct |
6 |
Correct |
8 ms |
1152 KB |
Output is correct |
7 |
Correct |
9 ms |
1152 KB |
Output is correct |
8 |
Correct |
34 ms |
1152 KB |
Output is correct |
9 |
Correct |
9 ms |
1152 KB |
Output is correct |
10 |
Correct |
12 ms |
1152 KB |
Output is correct |
11 |
Correct |
13 ms |
1152 KB |
Output is correct |
12 |
Correct |
17 ms |
1152 KB |
Output is correct |
13 |
Correct |
14 ms |
1152 KB |
Output is correct |
14 |
Correct |
15 ms |
1152 KB |
Output is correct |
15 |
Correct |
16 ms |
1152 KB |
Output is correct |
16 |
Correct |
16 ms |
1152 KB |
Output is correct |
17 |
Correct |
16 ms |
1152 KB |
Output is correct |
18 |
Correct |
15 ms |
1152 KB |
Output is correct |
19 |
Correct |
15 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |