// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 4e5 + 10;
vector<pii> G[N];
int n, m;
void Add_Edge(int u, int v, int c){
G[u].pb({v, c});
}
ll ps[N];
ll Solve(int l, int dif){
int r = l + n;
memset(ps, 0, sizeof ps);
multiset<pii> ms;
ll res = 0;
for(int i = l; i < r - 1; i++){
for(auto [x, c] : G[i]){
if(x >= r) continue;
ps[i] += c;
ps[x] -= c;
ms.insert({x, c});
}
ps[i] += ps[i - 1];
while(ps[i] > dif){
auto [fr, cnt] = *ms.rbegin();
ms.erase(ms.find({fr, cnt}));
if(fr <= i)
continue;
int del = min((ll) cnt, (ps[i] - dif + 1) / 2);
ps[i] -= 2 * del;
ps[fr] += 2 * del;
res += del;
if(cnt > del)
ms.insert({fr, cnt - del});
}
}
return res + dif;
}
ll df[N];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
int a, b, c;
for(int i = 0; i < m; i++){
cin >> a >> b >> c;
// assert(c == 1);
if(a > b) swap(a, b);
df[a] += c;
df[b] -= c;
Add_Edge(a, b, c);
Add_Edge(b, n + a, c);
Add_Edge(n + a, n + b, c);
}
for(int i = 1; i <= n; i++) df[i] += df[i - 1];
int id_mx = max_element(df + 1, df + n) - df;
id_mx ++;
ll ans = min(Solve(id_mx, 0), Solve(id_mx, 1));
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12792 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
7 ms |
12828 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
8 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12788 KB |
Output is correct |
10 |
Correct |
8 ms |
12788 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
9 ms |
12748 KB |
Output is correct |
15 |
Correct |
8 ms |
12840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12792 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
7 ms |
12828 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
8 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12788 KB |
Output is correct |
10 |
Correct |
8 ms |
12788 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
9 ms |
12748 KB |
Output is correct |
15 |
Correct |
8 ms |
12840 KB |
Output is correct |
16 |
Correct |
8 ms |
12876 KB |
Output is correct |
17 |
Correct |
8 ms |
12824 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12868 KB |
Output is correct |
20 |
Correct |
8 ms |
12808 KB |
Output is correct |
21 |
Correct |
9 ms |
12748 KB |
Output is correct |
22 |
Correct |
8 ms |
12876 KB |
Output is correct |
23 |
Correct |
8 ms |
12856 KB |
Output is correct |
24 |
Correct |
8 ms |
12876 KB |
Output is correct |
25 |
Correct |
8 ms |
12876 KB |
Output is correct |
26 |
Correct |
8 ms |
12792 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12792 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
7 ms |
12828 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
8 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12788 KB |
Output is correct |
10 |
Correct |
8 ms |
12788 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
9 ms |
12748 KB |
Output is correct |
15 |
Correct |
8 ms |
12840 KB |
Output is correct |
16 |
Correct |
8 ms |
12876 KB |
Output is correct |
17 |
Correct |
8 ms |
12824 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12868 KB |
Output is correct |
20 |
Correct |
8 ms |
12808 KB |
Output is correct |
21 |
Correct |
9 ms |
12748 KB |
Output is correct |
22 |
Correct |
8 ms |
12876 KB |
Output is correct |
23 |
Correct |
8 ms |
12856 KB |
Output is correct |
24 |
Correct |
8 ms |
12876 KB |
Output is correct |
25 |
Correct |
8 ms |
12876 KB |
Output is correct |
26 |
Correct |
8 ms |
12792 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
28 |
Correct |
11 ms |
13132 KB |
Output is correct |
29 |
Correct |
15 ms |
13132 KB |
Output is correct |
30 |
Correct |
11 ms |
13072 KB |
Output is correct |
31 |
Correct |
10 ms |
13056 KB |
Output is correct |
32 |
Correct |
10 ms |
13004 KB |
Output is correct |
33 |
Correct |
10 ms |
13132 KB |
Output is correct |
34 |
Correct |
10 ms |
13060 KB |
Output is correct |
35 |
Correct |
11 ms |
13140 KB |
Output is correct |
36 |
Correct |
10 ms |
13064 KB |
Output is correct |
37 |
Correct |
11 ms |
13132 KB |
Output is correct |
38 |
Correct |
11 ms |
13184 KB |
Output is correct |
39 |
Correct |
11 ms |
13192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12792 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
7 ms |
12828 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
8 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12788 KB |
Output is correct |
10 |
Correct |
8 ms |
12788 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
9 ms |
12748 KB |
Output is correct |
15 |
Correct |
8 ms |
12840 KB |
Output is correct |
16 |
Correct |
8 ms |
12876 KB |
Output is correct |
17 |
Correct |
8 ms |
12824 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12868 KB |
Output is correct |
20 |
Correct |
8 ms |
12808 KB |
Output is correct |
21 |
Correct |
9 ms |
12748 KB |
Output is correct |
22 |
Correct |
8 ms |
12876 KB |
Output is correct |
23 |
Correct |
8 ms |
12856 KB |
Output is correct |
24 |
Correct |
8 ms |
12876 KB |
Output is correct |
25 |
Correct |
8 ms |
12876 KB |
Output is correct |
26 |
Correct |
8 ms |
12792 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
28 |
Correct |
11 ms |
13132 KB |
Output is correct |
29 |
Correct |
15 ms |
13132 KB |
Output is correct |
30 |
Correct |
11 ms |
13072 KB |
Output is correct |
31 |
Correct |
10 ms |
13056 KB |
Output is correct |
32 |
Correct |
10 ms |
13004 KB |
Output is correct |
33 |
Correct |
10 ms |
13132 KB |
Output is correct |
34 |
Correct |
10 ms |
13060 KB |
Output is correct |
35 |
Correct |
11 ms |
13140 KB |
Output is correct |
36 |
Correct |
10 ms |
13064 KB |
Output is correct |
37 |
Correct |
11 ms |
13132 KB |
Output is correct |
38 |
Correct |
11 ms |
13184 KB |
Output is correct |
39 |
Correct |
11 ms |
13192 KB |
Output is correct |
40 |
Correct |
176 ms |
23844 KB |
Output is correct |
41 |
Correct |
184 ms |
23848 KB |
Output is correct |
42 |
Correct |
176 ms |
23392 KB |
Output is correct |
43 |
Correct |
160 ms |
23440 KB |
Output is correct |
44 |
Correct |
181 ms |
23364 KB |
Output is correct |
45 |
Correct |
162 ms |
22916 KB |
Output is correct |
46 |
Correct |
156 ms |
22852 KB |
Output is correct |
47 |
Correct |
152 ms |
22932 KB |
Output is correct |
48 |
Correct |
153 ms |
22724 KB |
Output is correct |
49 |
Correct |
124 ms |
22044 KB |
Output is correct |
50 |
Correct |
137 ms |
24348 KB |
Output is correct |
51 |
Correct |
130 ms |
22544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12792 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
7 ms |
12828 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
8 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12788 KB |
Output is correct |
10 |
Correct |
8 ms |
12788 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
9 ms |
12748 KB |
Output is correct |
15 |
Correct |
8 ms |
12840 KB |
Output is correct |
16 |
Correct |
8 ms |
12876 KB |
Output is correct |
17 |
Correct |
8 ms |
12824 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12868 KB |
Output is correct |
20 |
Correct |
8 ms |
12808 KB |
Output is correct |
21 |
Correct |
9 ms |
12748 KB |
Output is correct |
22 |
Correct |
8 ms |
12876 KB |
Output is correct |
23 |
Correct |
8 ms |
12856 KB |
Output is correct |
24 |
Correct |
8 ms |
12876 KB |
Output is correct |
25 |
Correct |
8 ms |
12876 KB |
Output is correct |
26 |
Correct |
8 ms |
12792 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
28 |
Correct |
11 ms |
13132 KB |
Output is correct |
29 |
Correct |
15 ms |
13132 KB |
Output is correct |
30 |
Correct |
11 ms |
13072 KB |
Output is correct |
31 |
Correct |
10 ms |
13056 KB |
Output is correct |
32 |
Correct |
10 ms |
13004 KB |
Output is correct |
33 |
Correct |
10 ms |
13132 KB |
Output is correct |
34 |
Correct |
10 ms |
13060 KB |
Output is correct |
35 |
Correct |
11 ms |
13140 KB |
Output is correct |
36 |
Correct |
10 ms |
13064 KB |
Output is correct |
37 |
Correct |
11 ms |
13132 KB |
Output is correct |
38 |
Correct |
11 ms |
13184 KB |
Output is correct |
39 |
Correct |
11 ms |
13192 KB |
Output is correct |
40 |
Correct |
176 ms |
23844 KB |
Output is correct |
41 |
Correct |
184 ms |
23848 KB |
Output is correct |
42 |
Correct |
176 ms |
23392 KB |
Output is correct |
43 |
Correct |
160 ms |
23440 KB |
Output is correct |
44 |
Correct |
181 ms |
23364 KB |
Output is correct |
45 |
Correct |
162 ms |
22916 KB |
Output is correct |
46 |
Correct |
156 ms |
22852 KB |
Output is correct |
47 |
Correct |
152 ms |
22932 KB |
Output is correct |
48 |
Correct |
153 ms |
22724 KB |
Output is correct |
49 |
Correct |
124 ms |
22044 KB |
Output is correct |
50 |
Correct |
137 ms |
24348 KB |
Output is correct |
51 |
Correct |
130 ms |
22544 KB |
Output is correct |
52 |
Correct |
191 ms |
23828 KB |
Output is correct |
53 |
Correct |
197 ms |
23360 KB |
Output is correct |
54 |
Correct |
199 ms |
23876 KB |
Output is correct |
55 |
Correct |
173 ms |
23256 KB |
Output is correct |
56 |
Correct |
176 ms |
23232 KB |
Output is correct |
57 |
Correct |
171 ms |
22980 KB |
Output is correct |
58 |
Correct |
171 ms |
22984 KB |
Output is correct |
59 |
Correct |
142 ms |
21372 KB |
Output is correct |