#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 300005
int n, m;
int p[MAXN], c[MAXN];
struct lines {
ll m, c; // gradient and y-intercept of the right-most line
priority_queue<int> pts; // points where slope changes
void join(lines& o) {
if (o.pts.size() > pts.size()) swap(o.pts, pts);
while (!o.pts.empty()) {
pts.push(o.pts.top());
o.pts.pop();
}
m += o.m;
c += o.c;
}
} dp[MAXN];
int main() {
scanf("%d%d", &n, &m);
REP (i, 2, n + m + 1) {
scanf("%d%d", &p[i], &c[i]);
}
REP (i, n + 1, n + m + 1) {
dp[i].pts.push(c[i]);
dp[i].pts.push(c[i]);
dp[i].m = 1;
dp[i].c = -c[i];
dp[p[i]].join(dp[i]);
}
RREP (i, n, 2) {
while (dp[i].m > 1) {
dp[i].m--;
dp[i].c += dp[i].pts.top();
dp[i].pts.pop();
}
int fi = dp[i].pts.top(); dp[i].pts.pop();
int se = dp[i].pts.top(); dp[i].pts.pop();
dp[i].pts.push(fi + c[i]);
dp[i].pts.push(se + c[i]);
dp[i].c -= c[i];
dp[p[i]].join(dp[i]);
}
while (dp[1].m > 0) {
dp[1].m--;
dp[1].c += dp[1].pts.top();
dp[1].pts.pop();
}
printf("%lld\n", dp[1].c);
return 0;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &p[i], &c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14284 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
10 ms |
14352 KB |
Output is correct |
4 |
Correct |
10 ms |
14388 KB |
Output is correct |
5 |
Correct |
8 ms |
14396 KB |
Output is correct |
6 |
Correct |
8 ms |
14388 KB |
Output is correct |
7 |
Correct |
8 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14356 KB |
Output is correct |
9 |
Correct |
8 ms |
14284 KB |
Output is correct |
10 |
Correct |
10 ms |
14376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14344 KB |
Output is correct |
2 |
Correct |
9 ms |
14388 KB |
Output is correct |
3 |
Correct |
10 ms |
14392 KB |
Output is correct |
4 |
Correct |
8 ms |
14356 KB |
Output is correct |
5 |
Correct |
10 ms |
14392 KB |
Output is correct |
6 |
Correct |
8 ms |
14284 KB |
Output is correct |
7 |
Correct |
9 ms |
14380 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14388 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14392 KB |
Output is correct |
12 |
Correct |
9 ms |
14284 KB |
Output is correct |
13 |
Correct |
8 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14284 KB |
Output is correct |
15 |
Correct |
10 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14396 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
9 ms |
14304 KB |
Output is correct |
19 |
Correct |
10 ms |
14412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14284 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
10 ms |
14352 KB |
Output is correct |
4 |
Correct |
10 ms |
14388 KB |
Output is correct |
5 |
Correct |
8 ms |
14396 KB |
Output is correct |
6 |
Correct |
8 ms |
14388 KB |
Output is correct |
7 |
Correct |
8 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14356 KB |
Output is correct |
9 |
Correct |
8 ms |
14284 KB |
Output is correct |
10 |
Correct |
10 ms |
14376 KB |
Output is correct |
11 |
Correct |
8 ms |
14344 KB |
Output is correct |
12 |
Correct |
9 ms |
14388 KB |
Output is correct |
13 |
Correct |
10 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14356 KB |
Output is correct |
15 |
Correct |
10 ms |
14392 KB |
Output is correct |
16 |
Correct |
8 ms |
14284 KB |
Output is correct |
17 |
Correct |
9 ms |
14380 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14388 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14392 KB |
Output is correct |
22 |
Correct |
9 ms |
14284 KB |
Output is correct |
23 |
Correct |
8 ms |
14392 KB |
Output is correct |
24 |
Correct |
8 ms |
14284 KB |
Output is correct |
25 |
Correct |
10 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14396 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14304 KB |
Output is correct |
29 |
Correct |
10 ms |
14412 KB |
Output is correct |
30 |
Incorrect |
8 ms |
14412 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14284 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
10 ms |
14352 KB |
Output is correct |
4 |
Correct |
10 ms |
14388 KB |
Output is correct |
5 |
Correct |
8 ms |
14396 KB |
Output is correct |
6 |
Correct |
8 ms |
14388 KB |
Output is correct |
7 |
Correct |
8 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14356 KB |
Output is correct |
9 |
Correct |
8 ms |
14284 KB |
Output is correct |
10 |
Correct |
10 ms |
14376 KB |
Output is correct |
11 |
Correct |
8 ms |
14344 KB |
Output is correct |
12 |
Correct |
9 ms |
14388 KB |
Output is correct |
13 |
Correct |
10 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14356 KB |
Output is correct |
15 |
Correct |
10 ms |
14392 KB |
Output is correct |
16 |
Correct |
8 ms |
14284 KB |
Output is correct |
17 |
Correct |
9 ms |
14380 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14388 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14392 KB |
Output is correct |
22 |
Correct |
9 ms |
14284 KB |
Output is correct |
23 |
Correct |
8 ms |
14392 KB |
Output is correct |
24 |
Correct |
8 ms |
14284 KB |
Output is correct |
25 |
Correct |
10 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14396 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14304 KB |
Output is correct |
29 |
Correct |
10 ms |
14412 KB |
Output is correct |
30 |
Incorrect |
8 ms |
14412 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |