#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 1e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
template<class T, class U> std::ostream& operator<<(ostream& s, const pair<T, U>& x) {
return s << "( " << x.first << ", " << x.second << ")";
}
template<class T> void stlts(T V){
cout << "[";
bool f = 1;
trav(x, V){
if(f)
cout << x, f = 0;
else{
cout << ", " << x;
}
}
cout << "]" << endl;
}
int n, m, k, p[mxN];
pair<ll, ll> f[mxN]; map<int, ll> dp[mxN];
ll res = 0;
void upd(map<int, ll>& c, ll d, ll w){
//cout << "WE GOT HERE" << nl;
c[d]+=w;
auto it = c.find(d);
it++;
for(; it != c.end() && w != 0; ){
int mn = min(w, it->s);
w-=mn; it->s-=mn;
if(it->s == 0){
c.erase(it++);
}
else{
break;
}
}
}
void comb(map<int, ll>&x, map<int, ll>& y){
if(siz(x) < siz(y)) swap(x, y);
trav(v, y){
x[v.f]+=v.s;
}
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> k;
FOR(i, 2, n+1){
cin >> p[i];
}
F0R(i, m){
int x, y, z; cin >> x >> y >> z;
f[x] = {y, z};
}
ROF(i, 2, n + 1){
if(f[i].f) upd(dp[i], f[i].f, f[i].s);
//stlts(dp[i]);
comb(dp[p[i]], dp[i]);
}
trav(v, dp[1]){
res += v.s;
}
cout << res << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
17912 KB |
Output is correct |
2 |
Correct |
34 ms |
9840 KB |
Output is correct |
3 |
Correct |
191 ms |
40696 KB |
Output is correct |
4 |
Correct |
103 ms |
18552 KB |
Output is correct |
5 |
Correct |
138 ms |
23544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
5120 KB |
Output is correct |
4 |
Correct |
46 ms |
8952 KB |
Output is correct |
5 |
Correct |
77 ms |
12920 KB |
Output is correct |
6 |
Correct |
61 ms |
8960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
12920 KB |
Output is correct |
2 |
Correct |
53 ms |
12024 KB |
Output is correct |
3 |
Correct |
64 ms |
10360 KB |
Output is correct |
4 |
Correct |
46 ms |
14332 KB |
Output is correct |
5 |
Correct |
41 ms |
9080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
86 ms |
16120 KB |
Output is correct |
11 |
Correct |
78 ms |
14584 KB |
Output is correct |
12 |
Correct |
44 ms |
9848 KB |
Output is correct |
13 |
Correct |
43 ms |
13808 KB |
Output is correct |
14 |
Correct |
41 ms |
8440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5760 KB |
Output is correct |
2 |
Correct |
16 ms |
7548 KB |
Output is correct |
3 |
Correct |
17 ms |
7552 KB |
Output is correct |
4 |
Correct |
17 ms |
7712 KB |
Output is correct |
5 |
Correct |
12 ms |
7296 KB |
Output is correct |
6 |
Correct |
16 ms |
7552 KB |
Output is correct |
7 |
Correct |
17 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
46 ms |
8952 KB |
Output is correct |
14 |
Correct |
77 ms |
12920 KB |
Output is correct |
15 |
Correct |
61 ms |
8960 KB |
Output is correct |
16 |
Correct |
86 ms |
16120 KB |
Output is correct |
17 |
Correct |
78 ms |
14584 KB |
Output is correct |
18 |
Correct |
44 ms |
9848 KB |
Output is correct |
19 |
Correct |
43 ms |
13808 KB |
Output is correct |
20 |
Correct |
41 ms |
8440 KB |
Output is correct |
21 |
Correct |
22 ms |
8952 KB |
Output is correct |
22 |
Correct |
72 ms |
17632 KB |
Output is correct |
23 |
Correct |
87 ms |
21496 KB |
Output is correct |
24 |
Correct |
145 ms |
31992 KB |
Output is correct |
25 |
Correct |
99 ms |
17784 KB |
Output is correct |
26 |
Correct |
112 ms |
18040 KB |
Output is correct |
27 |
Correct |
77 ms |
11768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
59 ms |
17912 KB |
Output is correct |
11 |
Correct |
34 ms |
9840 KB |
Output is correct |
12 |
Correct |
191 ms |
40696 KB |
Output is correct |
13 |
Correct |
103 ms |
18552 KB |
Output is correct |
14 |
Correct |
138 ms |
23544 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
5120 KB |
Output is correct |
18 |
Correct |
46 ms |
8952 KB |
Output is correct |
19 |
Correct |
77 ms |
12920 KB |
Output is correct |
20 |
Correct |
61 ms |
8960 KB |
Output is correct |
21 |
Correct |
59 ms |
12920 KB |
Output is correct |
22 |
Correct |
53 ms |
12024 KB |
Output is correct |
23 |
Correct |
64 ms |
10360 KB |
Output is correct |
24 |
Correct |
46 ms |
14332 KB |
Output is correct |
25 |
Correct |
41 ms |
9080 KB |
Output is correct |
26 |
Correct |
86 ms |
16120 KB |
Output is correct |
27 |
Correct |
78 ms |
14584 KB |
Output is correct |
28 |
Correct |
44 ms |
9848 KB |
Output is correct |
29 |
Correct |
43 ms |
13808 KB |
Output is correct |
30 |
Correct |
41 ms |
8440 KB |
Output is correct |
31 |
Correct |
7 ms |
5760 KB |
Output is correct |
32 |
Correct |
16 ms |
7548 KB |
Output is correct |
33 |
Correct |
17 ms |
7552 KB |
Output is correct |
34 |
Correct |
17 ms |
7712 KB |
Output is correct |
35 |
Correct |
12 ms |
7296 KB |
Output is correct |
36 |
Correct |
16 ms |
7552 KB |
Output is correct |
37 |
Correct |
17 ms |
7552 KB |
Output is correct |
38 |
Correct |
22 ms |
8952 KB |
Output is correct |
39 |
Correct |
72 ms |
17632 KB |
Output is correct |
40 |
Correct |
87 ms |
21496 KB |
Output is correct |
41 |
Correct |
145 ms |
31992 KB |
Output is correct |
42 |
Correct |
99 ms |
17784 KB |
Output is correct |
43 |
Correct |
112 ms |
18040 KB |
Output is correct |
44 |
Correct |
77 ms |
11768 KB |
Output is correct |
45 |
Correct |
25 ms |
9464 KB |
Output is correct |
46 |
Correct |
83 ms |
18936 KB |
Output is correct |
47 |
Correct |
93 ms |
23032 KB |
Output is correct |
48 |
Correct |
163 ms |
35332 KB |
Output is correct |
49 |
Correct |
102 ms |
18616 KB |
Output is correct |
50 |
Correct |
120 ms |
19064 KB |
Output is correct |
51 |
Correct |
84 ms |
12536 KB |
Output is correct |