#include<bits/stdc++.h>
#pragma optimize ("g",on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e15, N = 501, mod = 998244353;
double eps = 1e-6;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pii> adj[N];
int cnt[N], dp[N][N];
int n, m;
void calc(int u) {
if (u > n) {
cnt[u]++;
}
for (auto to: adj[u]) {
calc(to.ff);
cnt[u] += cnt[to.ff];
}
}
void dfs(int u) {
if (u > n) {
for (int val = 0; val <= 300; val++) dp[u][val] = val;
return;
}
for (auto to: adj[u]) {
if (cnt[to.ff]) {
dfs(to.ff);
}
}
for (int val = 0; val <= 300; val++) {
dp[u][val] = 0;
for (auto to: adj[u]) {
if (!cnt[to.ff]) continue;
int mn = inf;
for (int c = 0; c <= val; c++) {
mn = min(mn, dp[to.ff][val - c] + abs(to.ss - c));
}
dp[u][val] += mn;
}
}
}
main() {
//freopen("F.in", "r", stdin);
//freopen("F.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cin >> n >> m;
vector<int> s;
for (int i = 2; i <= n + m; i++) {
int p, c;
cin >> p >> c;
s.pb(c);
adj[p].pb({i, c});
}
if (n == 1) {
sort(all(s));
int val = s[sz(s) / 2], ans = 0;
for (auto x: s) ans += abs(x - val);
cout << ans;
return 0;
}
calc(1);
dfs(1);
int ans = inf;
for (int val = 0; val <= 300; val++) ans = min(ans, dp[1][val]);
cout << ans;
}
Compilation message
fireworks.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize ("g",on)
|
fireworks.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
75 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
6 ms |
712 KB |
Output is correct |
5 |
Correct |
5 ms |
832 KB |
Output is correct |
6 |
Correct |
5 ms |
844 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
972 KB |
Output is correct |
9 |
Correct |
7 ms |
972 KB |
Output is correct |
10 |
Correct |
7 ms |
1088 KB |
Output is correct |
11 |
Correct |
8 ms |
1228 KB |
Output is correct |
12 |
Correct |
9 ms |
1308 KB |
Output is correct |
13 |
Correct |
9 ms |
1356 KB |
Output is correct |
14 |
Correct |
9 ms |
1484 KB |
Output is correct |
15 |
Correct |
11 ms |
1420 KB |
Output is correct |
16 |
Correct |
10 ms |
1508 KB |
Output is correct |
17 |
Correct |
12 ms |
1432 KB |
Output is correct |
18 |
Correct |
11 ms |
1452 KB |
Output is correct |
19 |
Correct |
10 ms |
1460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
6 ms |
712 KB |
Output is correct |
15 |
Correct |
5 ms |
832 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
6 ms |
860 KB |
Output is correct |
18 |
Correct |
6 ms |
972 KB |
Output is correct |
19 |
Correct |
7 ms |
972 KB |
Output is correct |
20 |
Correct |
7 ms |
1088 KB |
Output is correct |
21 |
Correct |
8 ms |
1228 KB |
Output is correct |
22 |
Correct |
9 ms |
1308 KB |
Output is correct |
23 |
Correct |
9 ms |
1356 KB |
Output is correct |
24 |
Correct |
9 ms |
1484 KB |
Output is correct |
25 |
Correct |
11 ms |
1420 KB |
Output is correct |
26 |
Correct |
10 ms |
1508 KB |
Output is correct |
27 |
Correct |
12 ms |
1432 KB |
Output is correct |
28 |
Correct |
11 ms |
1452 KB |
Output is correct |
29 |
Correct |
10 ms |
1460 KB |
Output is correct |
30 |
Incorrect |
16 ms |
2120 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
6 ms |
712 KB |
Output is correct |
15 |
Correct |
5 ms |
832 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
6 ms |
860 KB |
Output is correct |
18 |
Correct |
6 ms |
972 KB |
Output is correct |
19 |
Correct |
7 ms |
972 KB |
Output is correct |
20 |
Correct |
7 ms |
1088 KB |
Output is correct |
21 |
Correct |
8 ms |
1228 KB |
Output is correct |
22 |
Correct |
9 ms |
1308 KB |
Output is correct |
23 |
Correct |
9 ms |
1356 KB |
Output is correct |
24 |
Correct |
9 ms |
1484 KB |
Output is correct |
25 |
Correct |
11 ms |
1420 KB |
Output is correct |
26 |
Correct |
10 ms |
1508 KB |
Output is correct |
27 |
Correct |
12 ms |
1432 KB |
Output is correct |
28 |
Correct |
11 ms |
1452 KB |
Output is correct |
29 |
Correct |
10 ms |
1460 KB |
Output is correct |
30 |
Incorrect |
16 ms |
2120 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |