#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct SlopeTrick{
ll coeff, init_slope;
priority_queue<ll> slope_point;
SlopeTrick(){
init_slope = 0;
coeff = 0;
}
};
const int N = 3e5 + 69;
const ll INF = 1e18 + 69;
int n, m;
vector<pair<int,int>> graph[N];
SlopeTrick dp[N];
void dfs(int u){
if (u > n){
return;
}
for(pair<int, int> v: graph[u]){
dfs(v.first);
SlopeTrick child_slope;
if (v.first > n){
child_slope.coeff = v.second;
child_slope.init_slope = -1;
child_slope.slope_point.push(v.second);
child_slope.slope_point.push(v.second);
}
else{
swap(child_slope, dp[v.first]);
child_slope.coeff += v.second;
ll x = child_slope.slope_point.top(); child_slope.slope_point.pop();
ll y = child_slope.slope_point.top(); child_slope.slope_point.pop();
child_slope.slope_point.push(x + v.second);
child_slope.slope_point.push(y + v.second);
}
dp[u].coeff += child_slope.coeff;
dp[u].init_slope += child_slope.init_slope;
if (child_slope.slope_point.size() > dp[u].slope_point.size())
swap(child_slope.slope_point, dp[u].slope_point);
while(child_slope.slope_point.size()){
dp[u].slope_point.push(child_slope.slope_point.top());
child_slope.slope_point.pop();
}
}
while(dp[u].slope_point.size() > (1 - dp[u].init_slope)) dp[u].slope_point.pop();
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 2; i<=n+m; ++i){
int p, c; cin >> p >> c;
graph[p].push_back({i, c});
}
dfs(1);
SlopeTrick ans = dp[1];
ll x = ans.coeff;
ll slope = ans.init_slope;
vector<ll> slope_point;
while(ans.slope_point.size()) {
slope_point.push_back(ans.slope_point.top());
ans.slope_point.pop();
}
slope_point.push_back(0);
reverse(ALL(slope_point));
for(int i = 1; i<slope_point.size(); ++i){
ll dx = slope_point[i] - slope_point[i-1];
x += slope * dx;
slope++;
if (slope == 0) break;
}
cout << x << "\n";
// for(int i = 0; i<N; ++i) minimize(ans, dp[1][i]);
// const int N = 41;
// int f[N], g[N], c[N];
// for(int i = 0; i<N; ++i) f[i] = abs(i-13) + abs(i-7);
// for(int i = 0; i<N; ++i) g[i] = abs(i - 3);
// memset(c, 63, sizeof c);
// for(int i = 0; i<N; ++i) for(int j = 0; i + j < N; ++j)
// minimize(c[i + j], g[i] + f[j]);
// for(int i= 0; i<N; ++i) cout << c[i] << " "; cout << "\n";
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:93:36: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
93 | while(dp[u].slope_point.size() > (1 - dp[u].init_slope)) dp[u].slope_point.pop();
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:118:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int i = 1; i<slope_point.size(); ++i){
| ~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
21596 KB |
Output is correct |
2 |
Correct |
6 ms |
21592 KB |
Output is correct |
3 |
Correct |
5 ms |
21596 KB |
Output is correct |
4 |
Correct |
5 ms |
21596 KB |
Output is correct |
5 |
Correct |
5 ms |
21592 KB |
Output is correct |
6 |
Correct |
5 ms |
21596 KB |
Output is correct |
7 |
Correct |
5 ms |
21592 KB |
Output is correct |
8 |
Correct |
6 ms |
21596 KB |
Output is correct |
9 |
Correct |
6 ms |
21340 KB |
Output is correct |
10 |
Correct |
6 ms |
21500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21340 KB |
Output is correct |
2 |
Correct |
6 ms |
21340 KB |
Output is correct |
3 |
Correct |
6 ms |
21596 KB |
Output is correct |
4 |
Correct |
5 ms |
21596 KB |
Output is correct |
5 |
Correct |
6 ms |
21596 KB |
Output is correct |
6 |
Correct |
5 ms |
21596 KB |
Output is correct |
7 |
Correct |
5 ms |
21596 KB |
Output is correct |
8 |
Correct |
5 ms |
21432 KB |
Output is correct |
9 |
Correct |
5 ms |
21596 KB |
Output is correct |
10 |
Correct |
5 ms |
21596 KB |
Output is correct |
11 |
Correct |
5 ms |
21592 KB |
Output is correct |
12 |
Correct |
5 ms |
21596 KB |
Output is correct |
13 |
Correct |
5 ms |
21592 KB |
Output is correct |
14 |
Correct |
5 ms |
21596 KB |
Output is correct |
15 |
Correct |
5 ms |
21596 KB |
Output is correct |
16 |
Correct |
6 ms |
21596 KB |
Output is correct |
17 |
Correct |
6 ms |
21596 KB |
Output is correct |
18 |
Correct |
6 ms |
21596 KB |
Output is correct |
19 |
Correct |
5 ms |
21596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
21596 KB |
Output is correct |
2 |
Correct |
6 ms |
21592 KB |
Output is correct |
3 |
Correct |
5 ms |
21596 KB |
Output is correct |
4 |
Correct |
5 ms |
21596 KB |
Output is correct |
5 |
Correct |
5 ms |
21592 KB |
Output is correct |
6 |
Correct |
5 ms |
21596 KB |
Output is correct |
7 |
Correct |
5 ms |
21592 KB |
Output is correct |
8 |
Correct |
6 ms |
21596 KB |
Output is correct |
9 |
Correct |
6 ms |
21340 KB |
Output is correct |
10 |
Correct |
6 ms |
21500 KB |
Output is correct |
11 |
Correct |
5 ms |
21340 KB |
Output is correct |
12 |
Correct |
6 ms |
21340 KB |
Output is correct |
13 |
Correct |
6 ms |
21596 KB |
Output is correct |
14 |
Correct |
5 ms |
21596 KB |
Output is correct |
15 |
Correct |
6 ms |
21596 KB |
Output is correct |
16 |
Correct |
5 ms |
21596 KB |
Output is correct |
17 |
Correct |
5 ms |
21596 KB |
Output is correct |
18 |
Correct |
5 ms |
21432 KB |
Output is correct |
19 |
Correct |
5 ms |
21596 KB |
Output is correct |
20 |
Correct |
5 ms |
21596 KB |
Output is correct |
21 |
Correct |
5 ms |
21592 KB |
Output is correct |
22 |
Correct |
5 ms |
21596 KB |
Output is correct |
23 |
Correct |
5 ms |
21592 KB |
Output is correct |
24 |
Correct |
5 ms |
21596 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
21596 KB |
Output is correct |
27 |
Correct |
6 ms |
21596 KB |
Output is correct |
28 |
Correct |
6 ms |
21596 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
5 ms |
21548 KB |
Output is correct |
31 |
Correct |
6 ms |
21848 KB |
Output is correct |
32 |
Correct |
6 ms |
21672 KB |
Output is correct |
33 |
Correct |
6 ms |
21596 KB |
Output is correct |
34 |
Correct |
7 ms |
21556 KB |
Output is correct |
35 |
Correct |
8 ms |
21852 KB |
Output is correct |
36 |
Correct |
7 ms |
21596 KB |
Output is correct |
37 |
Correct |
8 ms |
21852 KB |
Output is correct |
38 |
Correct |
8 ms |
21868 KB |
Output is correct |
39 |
Correct |
7 ms |
21848 KB |
Output is correct |
40 |
Correct |
8 ms |
22876 KB |
Output is correct |
41 |
Correct |
7 ms |
22876 KB |
Output is correct |
42 |
Correct |
6 ms |
21708 KB |
Output is correct |
43 |
Correct |
7 ms |
22364 KB |
Output is correct |
44 |
Correct |
8 ms |
22252 KB |
Output is correct |
45 |
Correct |
9 ms |
22112 KB |
Output is correct |
46 |
Correct |
8 ms |
21856 KB |
Output is correct |
47 |
Correct |
7 ms |
21852 KB |
Output is correct |
48 |
Correct |
7 ms |
21852 KB |
Output is correct |
49 |
Correct |
7 ms |
21876 KB |
Output is correct |
50 |
Correct |
7 ms |
21876 KB |
Output is correct |
51 |
Correct |
7 ms |
21680 KB |
Output is correct |
52 |
Correct |
7 ms |
21852 KB |
Output is correct |
53 |
Correct |
8 ms |
21852 KB |
Output is correct |
54 |
Correct |
8 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
21596 KB |
Output is correct |
2 |
Correct |
6 ms |
21592 KB |
Output is correct |
3 |
Correct |
5 ms |
21596 KB |
Output is correct |
4 |
Correct |
5 ms |
21596 KB |
Output is correct |
5 |
Correct |
5 ms |
21592 KB |
Output is correct |
6 |
Correct |
5 ms |
21596 KB |
Output is correct |
7 |
Correct |
5 ms |
21592 KB |
Output is correct |
8 |
Correct |
6 ms |
21596 KB |
Output is correct |
9 |
Correct |
6 ms |
21340 KB |
Output is correct |
10 |
Correct |
6 ms |
21500 KB |
Output is correct |
11 |
Correct |
5 ms |
21340 KB |
Output is correct |
12 |
Correct |
6 ms |
21340 KB |
Output is correct |
13 |
Correct |
6 ms |
21596 KB |
Output is correct |
14 |
Correct |
5 ms |
21596 KB |
Output is correct |
15 |
Correct |
6 ms |
21596 KB |
Output is correct |
16 |
Correct |
5 ms |
21596 KB |
Output is correct |
17 |
Correct |
5 ms |
21596 KB |
Output is correct |
18 |
Correct |
5 ms |
21432 KB |
Output is correct |
19 |
Correct |
5 ms |
21596 KB |
Output is correct |
20 |
Correct |
5 ms |
21596 KB |
Output is correct |
21 |
Correct |
5 ms |
21592 KB |
Output is correct |
22 |
Correct |
5 ms |
21596 KB |
Output is correct |
23 |
Correct |
5 ms |
21592 KB |
Output is correct |
24 |
Correct |
5 ms |
21596 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
21596 KB |
Output is correct |
27 |
Correct |
6 ms |
21596 KB |
Output is correct |
28 |
Correct |
6 ms |
21596 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
5 ms |
21548 KB |
Output is correct |
31 |
Correct |
6 ms |
21848 KB |
Output is correct |
32 |
Correct |
6 ms |
21672 KB |
Output is correct |
33 |
Correct |
6 ms |
21596 KB |
Output is correct |
34 |
Correct |
7 ms |
21556 KB |
Output is correct |
35 |
Correct |
8 ms |
21852 KB |
Output is correct |
36 |
Correct |
7 ms |
21596 KB |
Output is correct |
37 |
Correct |
8 ms |
21852 KB |
Output is correct |
38 |
Correct |
8 ms |
21868 KB |
Output is correct |
39 |
Correct |
7 ms |
21848 KB |
Output is correct |
40 |
Correct |
8 ms |
22876 KB |
Output is correct |
41 |
Correct |
7 ms |
22876 KB |
Output is correct |
42 |
Correct |
6 ms |
21708 KB |
Output is correct |
43 |
Correct |
7 ms |
22364 KB |
Output is correct |
44 |
Correct |
8 ms |
22252 KB |
Output is correct |
45 |
Correct |
9 ms |
22112 KB |
Output is correct |
46 |
Correct |
8 ms |
21856 KB |
Output is correct |
47 |
Correct |
7 ms |
21852 KB |
Output is correct |
48 |
Correct |
7 ms |
21852 KB |
Output is correct |
49 |
Correct |
7 ms |
21876 KB |
Output is correct |
50 |
Correct |
7 ms |
21876 KB |
Output is correct |
51 |
Correct |
7 ms |
21680 KB |
Output is correct |
52 |
Correct |
7 ms |
21852 KB |
Output is correct |
53 |
Correct |
8 ms |
21852 KB |
Output is correct |
54 |
Correct |
8 ms |
21852 KB |
Output is correct |
55 |
Correct |
10 ms |
22132 KB |
Output is correct |
56 |
Correct |
26 ms |
24020 KB |
Output is correct |
57 |
Correct |
44 ms |
25580 KB |
Output is correct |
58 |
Correct |
59 ms |
26832 KB |
Output is correct |
59 |
Correct |
75 ms |
28496 KB |
Output is correct |
60 |
Correct |
101 ms |
29984 KB |
Output is correct |
61 |
Correct |
99 ms |
31792 KB |
Output is correct |
62 |
Correct |
121 ms |
32808 KB |
Output is correct |
63 |
Correct |
151 ms |
34512 KB |
Output is correct |
64 |
Correct |
181 ms |
35380 KB |
Output is correct |
65 |
Correct |
110 ms |
101440 KB |
Output is correct |
66 |
Correct |
107 ms |
101456 KB |
Output is correct |
67 |
Correct |
93 ms |
35920 KB |
Output is correct |
68 |
Correct |
143 ms |
69132 KB |
Output is correct |
69 |
Correct |
153 ms |
64528 KB |
Output is correct |
70 |
Correct |
156 ms |
64348 KB |
Output is correct |
71 |
Correct |
174 ms |
42900 KB |
Output is correct |
72 |
Correct |
153 ms |
43612 KB |
Output is correct |
73 |
Correct |
153 ms |
42064 KB |
Output is correct |
74 |
Correct |
144 ms |
40112 KB |
Output is correct |
75 |
Correct |
144 ms |
39004 KB |
Output is correct |
76 |
Correct |
144 ms |
41000 KB |
Output is correct |
77 |
Correct |
146 ms |
41604 KB |
Output is correct |
78 |
Correct |
151 ms |
40740 KB |
Output is correct |
79 |
Correct |
141 ms |
43724 KB |
Output is correct |