// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
const ll Mod = 1000000007LL;
const int N = 4e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
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 r, int dif){
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] ++;
df[b] --;
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, id_mx + n, 0), Solve(id_mx, id_mx + n, 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 |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12808 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12732 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12840 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12808 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12732 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12840 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
16 |
Correct |
9 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12776 KB |
Output is correct |
18 |
Correct |
9 ms |
12840 KB |
Output is correct |
19 |
Correct |
9 ms |
12800 KB |
Output is correct |
20 |
Correct |
8 ms |
12748 KB |
Output is correct |
21 |
Correct |
8 ms |
12876 KB |
Output is correct |
22 |
Correct |
7 ms |
12748 KB |
Output is correct |
23 |
Correct |
9 ms |
12748 KB |
Output is correct |
24 |
Correct |
8 ms |
12872 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12748 KB |
Output is correct |
27 |
Correct |
8 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12808 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12732 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12840 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
16 |
Correct |
9 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12776 KB |
Output is correct |
18 |
Correct |
9 ms |
12840 KB |
Output is correct |
19 |
Correct |
9 ms |
12800 KB |
Output is correct |
20 |
Correct |
8 ms |
12748 KB |
Output is correct |
21 |
Correct |
8 ms |
12876 KB |
Output is correct |
22 |
Correct |
7 ms |
12748 KB |
Output is correct |
23 |
Correct |
9 ms |
12748 KB |
Output is correct |
24 |
Correct |
8 ms |
12872 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12748 KB |
Output is correct |
27 |
Correct |
8 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13004 KB |
Output is correct |
29 |
Correct |
10 ms |
13092 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
11 ms |
13004 KB |
Output is correct |
32 |
Correct |
11 ms |
13004 KB |
Output is correct |
33 |
Correct |
13 ms |
13004 KB |
Output is correct |
34 |
Correct |
11 ms |
13004 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
12 ms |
13108 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12808 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12732 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12840 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
16 |
Correct |
9 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12776 KB |
Output is correct |
18 |
Correct |
9 ms |
12840 KB |
Output is correct |
19 |
Correct |
9 ms |
12800 KB |
Output is correct |
20 |
Correct |
8 ms |
12748 KB |
Output is correct |
21 |
Correct |
8 ms |
12876 KB |
Output is correct |
22 |
Correct |
7 ms |
12748 KB |
Output is correct |
23 |
Correct |
9 ms |
12748 KB |
Output is correct |
24 |
Correct |
8 ms |
12872 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12748 KB |
Output is correct |
27 |
Correct |
8 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13004 KB |
Output is correct |
29 |
Correct |
10 ms |
13092 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
11 ms |
13004 KB |
Output is correct |
32 |
Correct |
11 ms |
13004 KB |
Output is correct |
33 |
Correct |
13 ms |
13004 KB |
Output is correct |
34 |
Correct |
11 ms |
13004 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
12 ms |
13108 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13088 KB |
Output is correct |
40 |
Correct |
177 ms |
23588 KB |
Output is correct |
41 |
Correct |
184 ms |
23596 KB |
Output is correct |
42 |
Correct |
195 ms |
23088 KB |
Output is correct |
43 |
Correct |
160 ms |
23064 KB |
Output is correct |
44 |
Correct |
165 ms |
23144 KB |
Output is correct |
45 |
Correct |
169 ms |
22656 KB |
Output is correct |
46 |
Correct |
155 ms |
22684 KB |
Output is correct |
47 |
Correct |
151 ms |
22656 KB |
Output is correct |
48 |
Correct |
164 ms |
22448 KB |
Output is correct |
49 |
Correct |
121 ms |
21800 KB |
Output is correct |
50 |
Correct |
146 ms |
24012 KB |
Output is correct |
51 |
Correct |
124 ms |
22212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12808 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12732 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12840 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
7 ms |
12748 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
16 |
Correct |
9 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12776 KB |
Output is correct |
18 |
Correct |
9 ms |
12840 KB |
Output is correct |
19 |
Correct |
9 ms |
12800 KB |
Output is correct |
20 |
Correct |
8 ms |
12748 KB |
Output is correct |
21 |
Correct |
8 ms |
12876 KB |
Output is correct |
22 |
Correct |
7 ms |
12748 KB |
Output is correct |
23 |
Correct |
9 ms |
12748 KB |
Output is correct |
24 |
Correct |
8 ms |
12872 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12748 KB |
Output is correct |
27 |
Correct |
8 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13004 KB |
Output is correct |
29 |
Correct |
10 ms |
13092 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
11 ms |
13004 KB |
Output is correct |
32 |
Correct |
11 ms |
13004 KB |
Output is correct |
33 |
Correct |
13 ms |
13004 KB |
Output is correct |
34 |
Correct |
11 ms |
13004 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
12 ms |
13108 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13088 KB |
Output is correct |
40 |
Correct |
177 ms |
23588 KB |
Output is correct |
41 |
Correct |
184 ms |
23596 KB |
Output is correct |
42 |
Correct |
195 ms |
23088 KB |
Output is correct |
43 |
Correct |
160 ms |
23064 KB |
Output is correct |
44 |
Correct |
165 ms |
23144 KB |
Output is correct |
45 |
Correct |
169 ms |
22656 KB |
Output is correct |
46 |
Correct |
155 ms |
22684 KB |
Output is correct |
47 |
Correct |
151 ms |
22656 KB |
Output is correct |
48 |
Correct |
164 ms |
22448 KB |
Output is correct |
49 |
Correct |
121 ms |
21800 KB |
Output is correct |
50 |
Correct |
146 ms |
24012 KB |
Output is correct |
51 |
Correct |
124 ms |
22212 KB |
Output is correct |
52 |
Correct |
189 ms |
23548 KB |
Output is correct |
53 |
Correct |
200 ms |
25340 KB |
Output is correct |
54 |
Correct |
205 ms |
25852 KB |
Output is correct |
55 |
Correct |
172 ms |
25156 KB |
Output is correct |
56 |
Correct |
182 ms |
25156 KB |
Output is correct |
57 |
Correct |
168 ms |
24820 KB |
Output is correct |
58 |
Incorrect |
170 ms |
24828 KB |
Output isn't correct |
59 |
Halted |
0 ms |
0 KB |
- |