//
// Created by Ильдар Ялалов on 28.10.2019.
//
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf_int = 1e9 + 100;
const ll inf_ll = 1e18;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double dbl;
#define pb push_back
#define eb emplace_back
const double pi = 3.1415926535898;
#define dout if(debug) cout
#define fi first
#define se second
#define sp setprecision
#define sz(a) (int(a.size()))
#define mp make_pair
#define all(a) a.begin(),a.end()
bool debug = 0;
const int MAXN = 3e5 + 100;
const int LOG = 21;
const int mod = 998244353;
const int MX = (1e7 + 100) * 1.5;
typedef long long li;
template<class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
out << "( " << rhs.first << " , " << rhs.second << " )";
return out;
}
template<typename A, typename B>
string to_string(pair<A, B> p);
template<typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template<typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) {
return '"' + s + '"';
}
string to_string(const char *s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template<size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template<typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template<typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template<typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " +
to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef zxc
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int p[MAXN], c[MAXN];
ll a[MAXN], b[MAXN];
priority_queue<ll> q[MAXN];
void union_set(int i, int j) {
a[i] += a[j];
b[i] += b[j];
if (q[i].size() < q[j].size())
q[i].swap(q[j]);
while (!q[j].empty()) {
q[i].push(q[j].top());
q[j].pop();
}
}
void solve() {
int n, m;
cin >> n >> m;
for (int i = 2; i <= n + m; ++i) {
cin >> p[i] >> c[i];
}
for (int i = n + m; i >= 2; --i) {
if (i > n) {
a[i] = 1;
b[i] = -c[i];
q[i].push(c[i]);
q[i].push(c[i]);
} else {
while (a[i] > 1) {
a[i]--;
b[i] += q[i].top();
q[i].pop();
}
ll x = q[i].top();
q[i].pop();
ll y = q[i].top();
q[i].pop();
q[i].push(x + c[i]);
q[i].push(y + c[i]);
b[i] -= c[i];
}
union_set(p[i], i);
}
while(a[1] > 0){
a[1]--;
b[1]+=q[1].top();
q[1].pop();
}
cout << b[1];
}
signed main() {
#ifdef zxc
debug = 0;
freopen("../input.txt", "r", stdin);
// freopen("../output.txt", "w", stdout);
#else
#endif //zxc
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
int t = 1;
while (t--)
solve();
if (debug)
cerr << endl << "time : " << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
12 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9692 KB |
Output is correct |
5 |
Correct |
11 ms |
9720 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9720 KB |
Output is correct |
4 |
Correct |
12 ms |
9816 KB |
Output is correct |
5 |
Correct |
11 ms |
9720 KB |
Output is correct |
6 |
Correct |
11 ms |
9848 KB |
Output is correct |
7 |
Correct |
12 ms |
9764 KB |
Output is correct |
8 |
Correct |
12 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
12 ms |
9848 KB |
Output is correct |
11 |
Correct |
12 ms |
9848 KB |
Output is correct |
12 |
Correct |
12 ms |
9848 KB |
Output is correct |
13 |
Correct |
11 ms |
9852 KB |
Output is correct |
14 |
Correct |
12 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
12 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
12 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9692 KB |
Output is correct |
5 |
Correct |
11 ms |
9720 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
12 ms |
9816 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
12 ms |
9764 KB |
Output is correct |
18 |
Correct |
12 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
12 ms |
9848 KB |
Output is correct |
21 |
Correct |
12 ms |
9848 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
11 ms |
9852 KB |
Output is correct |
24 |
Correct |
12 ms |
9720 KB |
Output is correct |
25 |
Correct |
11 ms |
9720 KB |
Output is correct |
26 |
Correct |
11 ms |
9848 KB |
Output is correct |
27 |
Correct |
12 ms |
9848 KB |
Output is correct |
28 |
Correct |
11 ms |
9720 KB |
Output is correct |
29 |
Correct |
11 ms |
9876 KB |
Output is correct |
30 |
Correct |
11 ms |
9848 KB |
Output is correct |
31 |
Correct |
12 ms |
9848 KB |
Output is correct |
32 |
Correct |
15 ms |
9848 KB |
Output is correct |
33 |
Correct |
12 ms |
9848 KB |
Output is correct |
34 |
Correct |
14 ms |
9976 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
14 ms |
10076 KB |
Output is correct |
37 |
Correct |
14 ms |
10104 KB |
Output is correct |
38 |
Correct |
14 ms |
10104 KB |
Output is correct |
39 |
Correct |
15 ms |
10132 KB |
Output is correct |
40 |
Correct |
14 ms |
9976 KB |
Output is correct |
41 |
Correct |
13 ms |
9976 KB |
Output is correct |
42 |
Correct |
13 ms |
9976 KB |
Output is correct |
43 |
Correct |
15 ms |
10104 KB |
Output is correct |
44 |
Correct |
15 ms |
10104 KB |
Output is correct |
45 |
Correct |
14 ms |
10108 KB |
Output is correct |
46 |
Correct |
16 ms |
10360 KB |
Output is correct |
47 |
Correct |
16 ms |
10360 KB |
Output is correct |
48 |
Correct |
15 ms |
10232 KB |
Output is correct |
49 |
Correct |
15 ms |
10232 KB |
Output is correct |
50 |
Correct |
15 ms |
10232 KB |
Output is correct |
51 |
Correct |
15 ms |
10104 KB |
Output is correct |
52 |
Correct |
15 ms |
10232 KB |
Output is correct |
53 |
Correct |
15 ms |
10232 KB |
Output is correct |
54 |
Correct |
15 ms |
10232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
12 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9692 KB |
Output is correct |
5 |
Correct |
11 ms |
9720 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
12 ms |
9816 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
12 ms |
9764 KB |
Output is correct |
18 |
Correct |
12 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
12 ms |
9848 KB |
Output is correct |
21 |
Correct |
12 ms |
9848 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
11 ms |
9852 KB |
Output is correct |
24 |
Correct |
12 ms |
9720 KB |
Output is correct |
25 |
Correct |
11 ms |
9720 KB |
Output is correct |
26 |
Correct |
11 ms |
9848 KB |
Output is correct |
27 |
Correct |
12 ms |
9848 KB |
Output is correct |
28 |
Correct |
11 ms |
9720 KB |
Output is correct |
29 |
Correct |
11 ms |
9876 KB |
Output is correct |
30 |
Correct |
11 ms |
9848 KB |
Output is correct |
31 |
Correct |
12 ms |
9848 KB |
Output is correct |
32 |
Correct |
15 ms |
9848 KB |
Output is correct |
33 |
Correct |
12 ms |
9848 KB |
Output is correct |
34 |
Correct |
14 ms |
9976 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
14 ms |
10076 KB |
Output is correct |
37 |
Correct |
14 ms |
10104 KB |
Output is correct |
38 |
Correct |
14 ms |
10104 KB |
Output is correct |
39 |
Correct |
15 ms |
10132 KB |
Output is correct |
40 |
Correct |
14 ms |
9976 KB |
Output is correct |
41 |
Correct |
13 ms |
9976 KB |
Output is correct |
42 |
Correct |
13 ms |
9976 KB |
Output is correct |
43 |
Correct |
15 ms |
10104 KB |
Output is correct |
44 |
Correct |
15 ms |
10104 KB |
Output is correct |
45 |
Correct |
14 ms |
10108 KB |
Output is correct |
46 |
Correct |
16 ms |
10360 KB |
Output is correct |
47 |
Correct |
16 ms |
10360 KB |
Output is correct |
48 |
Correct |
15 ms |
10232 KB |
Output is correct |
49 |
Correct |
15 ms |
10232 KB |
Output is correct |
50 |
Correct |
15 ms |
10232 KB |
Output is correct |
51 |
Correct |
15 ms |
10104 KB |
Output is correct |
52 |
Correct |
15 ms |
10232 KB |
Output is correct |
53 |
Correct |
15 ms |
10232 KB |
Output is correct |
54 |
Correct |
15 ms |
10232 KB |
Output is correct |
55 |
Correct |
20 ms |
10744 KB |
Output is correct |
56 |
Correct |
44 ms |
13680 KB |
Output is correct |
57 |
Correct |
70 ms |
16072 KB |
Output is correct |
58 |
Correct |
87 ms |
18000 KB |
Output is correct |
59 |
Correct |
110 ms |
20588 KB |
Output is correct |
60 |
Correct |
140 ms |
23128 KB |
Output is correct |
61 |
Correct |
167 ms |
25456 KB |
Output is correct |
62 |
Correct |
188 ms |
26996 KB |
Output is correct |
63 |
Correct |
214 ms |
30172 KB |
Output is correct |
64 |
Correct |
220 ms |
31088 KB |
Output is correct |
65 |
Correct |
114 ms |
21624 KB |
Output is correct |
66 |
Correct |
114 ms |
21688 KB |
Output is correct |
67 |
Correct |
117 ms |
21752 KB |
Output is correct |
68 |
Correct |
184 ms |
28408 KB |
Output is correct |
69 |
Correct |
206 ms |
29036 KB |
Output is correct |
70 |
Correct |
211 ms |
29012 KB |
Output is correct |
71 |
Correct |
265 ms |
44076 KB |
Output is correct |
72 |
Correct |
270 ms |
44136 KB |
Output is correct |
73 |
Correct |
222 ms |
40456 KB |
Output is correct |
74 |
Correct |
232 ms |
40920 KB |
Output is correct |
75 |
Correct |
227 ms |
39400 KB |
Output is correct |
76 |
Correct |
228 ms |
39392 KB |
Output is correct |
77 |
Correct |
343 ms |
38852 KB |
Output is correct |
78 |
Correct |
241 ms |
38376 KB |
Output is correct |
79 |
Correct |
215 ms |
36188 KB |
Output is correct |