#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int MAX = 3e5 + 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 |
Runtime error |
379 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
363 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
379 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
379 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |