#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxN=3e5;
int n, m, p[mxN], cc[mxN];
bool d[mxN];
vector<int> adj[mxN];
ll c[mxN], yi0[mxN], yi1[mxN];
vector<ll> pc[mxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=1; i<n+m; ++i) {
cin >> p[i] >> c[i], --p[i];
++cc[p[i]];
}
for(int i=n+m-1; i; --i) {
if(d[i])
continue;
while(p[i]&&cc[p[i]]==1) {
c[i]+=c[p[i]];
d[p[i]]=1;
p[i]=p[p[i]];
}
adj[p[i]].push_back(i);
}
for(int i=n+m-1; i>=0; --i) {
if(d[i])
continue;
if(i>=n) {
yi1[i]=-1;
pc[i].push_back(0);
pc[i].push_back(0);
}
for(int j : adj[i]) {
if(d[j])
continue;
yi0[i]+=yi0[j];
yi1[i]+=yi1[j];
pc[i].insert(pc[i].end(), pc[j].begin(), pc[j].end());
}
sort(pc[i].begin(), pc[i].end());
pc[i].erase(pc[i].begin()-yi1[i]+1, pc[i].end());
pc[i][-yi1[i]-1]+=c[i];
pc[i][-yi1[i]]+=c[i];
yi0[i]+=c[i];
// cout << yi0[i] << " " << yi1[i] << endl;
}
ll ans=yi0[0];
for(int i=0; i<-yi1[0]; ++i)
ans+=(yi1[0]+i)*(pc[0][i]-(i?pc[0][i-1]:0));
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
14 ms |
14684 KB |
Output is correct |
4 |
Correct |
15 ms |
14788 KB |
Output is correct |
5 |
Correct |
15 ms |
14788 KB |
Output is correct |
6 |
Correct |
15 ms |
14792 KB |
Output is correct |
7 |
Correct |
16 ms |
14792 KB |
Output is correct |
8 |
Correct |
17 ms |
14808 KB |
Output is correct |
9 |
Correct |
15 ms |
14808 KB |
Output is correct |
10 |
Correct |
18 ms |
14828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14828 KB |
Output is correct |
2 |
Correct |
15 ms |
14832 KB |
Output is correct |
3 |
Correct |
15 ms |
14836 KB |
Output is correct |
4 |
Correct |
14 ms |
14840 KB |
Output is correct |
5 |
Correct |
14 ms |
14904 KB |
Output is correct |
6 |
Correct |
15 ms |
14960 KB |
Output is correct |
7 |
Correct |
14 ms |
14960 KB |
Output is correct |
8 |
Correct |
14 ms |
14960 KB |
Output is correct |
9 |
Correct |
14 ms |
14988 KB |
Output is correct |
10 |
Correct |
16 ms |
14988 KB |
Output is correct |
11 |
Correct |
15 ms |
14988 KB |
Output is correct |
12 |
Correct |
16 ms |
14988 KB |
Output is correct |
13 |
Correct |
17 ms |
14988 KB |
Output is correct |
14 |
Correct |
16 ms |
14988 KB |
Output is correct |
15 |
Correct |
15 ms |
14988 KB |
Output is correct |
16 |
Correct |
14 ms |
14988 KB |
Output is correct |
17 |
Correct |
17 ms |
14988 KB |
Output is correct |
18 |
Correct |
18 ms |
14988 KB |
Output is correct |
19 |
Correct |
18 ms |
14988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
14 ms |
14684 KB |
Output is correct |
4 |
Correct |
15 ms |
14788 KB |
Output is correct |
5 |
Correct |
15 ms |
14788 KB |
Output is correct |
6 |
Correct |
15 ms |
14792 KB |
Output is correct |
7 |
Correct |
16 ms |
14792 KB |
Output is correct |
8 |
Correct |
17 ms |
14808 KB |
Output is correct |
9 |
Correct |
15 ms |
14808 KB |
Output is correct |
10 |
Correct |
18 ms |
14828 KB |
Output is correct |
11 |
Correct |
14 ms |
14828 KB |
Output is correct |
12 |
Correct |
15 ms |
14832 KB |
Output is correct |
13 |
Correct |
15 ms |
14836 KB |
Output is correct |
14 |
Correct |
14 ms |
14840 KB |
Output is correct |
15 |
Correct |
14 ms |
14904 KB |
Output is correct |
16 |
Correct |
15 ms |
14960 KB |
Output is correct |
17 |
Correct |
14 ms |
14960 KB |
Output is correct |
18 |
Correct |
14 ms |
14960 KB |
Output is correct |
19 |
Correct |
14 ms |
14988 KB |
Output is correct |
20 |
Correct |
16 ms |
14988 KB |
Output is correct |
21 |
Correct |
15 ms |
14988 KB |
Output is correct |
22 |
Correct |
16 ms |
14988 KB |
Output is correct |
23 |
Correct |
17 ms |
14988 KB |
Output is correct |
24 |
Correct |
16 ms |
14988 KB |
Output is correct |
25 |
Correct |
15 ms |
14988 KB |
Output is correct |
26 |
Correct |
14 ms |
14988 KB |
Output is correct |
27 |
Correct |
17 ms |
14988 KB |
Output is correct |
28 |
Correct |
18 ms |
14988 KB |
Output is correct |
29 |
Correct |
18 ms |
14988 KB |
Output is correct |
30 |
Correct |
15 ms |
14988 KB |
Output is correct |
31 |
Correct |
17 ms |
15060 KB |
Output is correct |
32 |
Correct |
19 ms |
15220 KB |
Output is correct |
33 |
Correct |
17 ms |
15392 KB |
Output is correct |
34 |
Correct |
24 ms |
15564 KB |
Output is correct |
35 |
Correct |
21 ms |
15960 KB |
Output is correct |
36 |
Correct |
21 ms |
16040 KB |
Output is correct |
37 |
Correct |
27 ms |
16040 KB |
Output is correct |
38 |
Correct |
33 ms |
16844 KB |
Output is correct |
39 |
Correct |
26 ms |
16844 KB |
Output is correct |
40 |
Correct |
20 ms |
16844 KB |
Output is correct |
41 |
Correct |
16 ms |
16844 KB |
Output is correct |
42 |
Correct |
16 ms |
16844 KB |
Output is correct |
43 |
Correct |
251 ms |
40620 KB |
Output is correct |
44 |
Correct |
192 ms |
40620 KB |
Output is correct |
45 |
Correct |
191 ms |
40620 KB |
Output is correct |
46 |
Correct |
17 ms |
40620 KB |
Output is correct |
47 |
Correct |
17 ms |
40620 KB |
Output is correct |
48 |
Correct |
23 ms |
40620 KB |
Output is correct |
49 |
Correct |
22 ms |
40620 KB |
Output is correct |
50 |
Correct |
19 ms |
40620 KB |
Output is correct |
51 |
Correct |
20 ms |
40620 KB |
Output is correct |
52 |
Correct |
21 ms |
40620 KB |
Output is correct |
53 |
Correct |
20 ms |
40620 KB |
Output is correct |
54 |
Correct |
19 ms |
40620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
14 ms |
14684 KB |
Output is correct |
4 |
Correct |
15 ms |
14788 KB |
Output is correct |
5 |
Correct |
15 ms |
14788 KB |
Output is correct |
6 |
Correct |
15 ms |
14792 KB |
Output is correct |
7 |
Correct |
16 ms |
14792 KB |
Output is correct |
8 |
Correct |
17 ms |
14808 KB |
Output is correct |
9 |
Correct |
15 ms |
14808 KB |
Output is correct |
10 |
Correct |
18 ms |
14828 KB |
Output is correct |
11 |
Correct |
14 ms |
14828 KB |
Output is correct |
12 |
Correct |
15 ms |
14832 KB |
Output is correct |
13 |
Correct |
15 ms |
14836 KB |
Output is correct |
14 |
Correct |
14 ms |
14840 KB |
Output is correct |
15 |
Correct |
14 ms |
14904 KB |
Output is correct |
16 |
Correct |
15 ms |
14960 KB |
Output is correct |
17 |
Correct |
14 ms |
14960 KB |
Output is correct |
18 |
Correct |
14 ms |
14960 KB |
Output is correct |
19 |
Correct |
14 ms |
14988 KB |
Output is correct |
20 |
Correct |
16 ms |
14988 KB |
Output is correct |
21 |
Correct |
15 ms |
14988 KB |
Output is correct |
22 |
Correct |
16 ms |
14988 KB |
Output is correct |
23 |
Correct |
17 ms |
14988 KB |
Output is correct |
24 |
Correct |
16 ms |
14988 KB |
Output is correct |
25 |
Correct |
15 ms |
14988 KB |
Output is correct |
26 |
Correct |
14 ms |
14988 KB |
Output is correct |
27 |
Correct |
17 ms |
14988 KB |
Output is correct |
28 |
Correct |
18 ms |
14988 KB |
Output is correct |
29 |
Correct |
18 ms |
14988 KB |
Output is correct |
30 |
Correct |
15 ms |
14988 KB |
Output is correct |
31 |
Correct |
17 ms |
15060 KB |
Output is correct |
32 |
Correct |
19 ms |
15220 KB |
Output is correct |
33 |
Correct |
17 ms |
15392 KB |
Output is correct |
34 |
Correct |
24 ms |
15564 KB |
Output is correct |
35 |
Correct |
21 ms |
15960 KB |
Output is correct |
36 |
Correct |
21 ms |
16040 KB |
Output is correct |
37 |
Correct |
27 ms |
16040 KB |
Output is correct |
38 |
Correct |
33 ms |
16844 KB |
Output is correct |
39 |
Correct |
26 ms |
16844 KB |
Output is correct |
40 |
Correct |
20 ms |
16844 KB |
Output is correct |
41 |
Correct |
16 ms |
16844 KB |
Output is correct |
42 |
Correct |
16 ms |
16844 KB |
Output is correct |
43 |
Correct |
251 ms |
40620 KB |
Output is correct |
44 |
Correct |
192 ms |
40620 KB |
Output is correct |
45 |
Correct |
191 ms |
40620 KB |
Output is correct |
46 |
Correct |
17 ms |
40620 KB |
Output is correct |
47 |
Correct |
17 ms |
40620 KB |
Output is correct |
48 |
Correct |
23 ms |
40620 KB |
Output is correct |
49 |
Correct |
22 ms |
40620 KB |
Output is correct |
50 |
Correct |
19 ms |
40620 KB |
Output is correct |
51 |
Correct |
20 ms |
40620 KB |
Output is correct |
52 |
Correct |
21 ms |
40620 KB |
Output is correct |
53 |
Correct |
20 ms |
40620 KB |
Output is correct |
54 |
Correct |
19 ms |
40620 KB |
Output is correct |
55 |
Correct |
48 ms |
40620 KB |
Output is correct |
56 |
Correct |
347 ms |
48684 KB |
Output is correct |
57 |
Correct |
649 ms |
82264 KB |
Output is correct |
58 |
Correct |
779 ms |
91832 KB |
Output is correct |
59 |
Correct |
1875 ms |
186644 KB |
Output is correct |
60 |
Correct |
1855 ms |
186916 KB |
Output is correct |
61 |
Execution timed out |
2070 ms |
217436 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |