// 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];
int Solve(int l, int r, int dif){
memset(ps, 0, sizeof ps);
multiset<pii> ms;
int 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;
}
int Calc(int i, int dif){
if(i > n)
i -= n;
return Solve(i, i + n, dif);
}
int 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 ++;
int ans = min(Calc(id_mx, 0), Calc(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 |
12748 KB |
Output is correct |
3 |
Correct |
7 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12808 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 |
12748 KB |
Output is correct |
10 |
Correct |
8 ms |
12772 KB |
Output is correct |
11 |
Correct |
8 ms |
12828 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
9 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 |
7 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12808 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 |
12748 KB |
Output is correct |
10 |
Correct |
8 ms |
12772 KB |
Output is correct |
11 |
Correct |
8 ms |
12828 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
9 ms |
12748 KB |
Output is correct |
16 |
Correct |
8 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12748 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12796 KB |
Output is correct |
20 |
Correct |
9 ms |
12748 KB |
Output is correct |
21 |
Correct |
9 ms |
12764 KB |
Output is correct |
22 |
Correct |
9 ms |
12748 KB |
Output is correct |
23 |
Correct |
8 ms |
12748 KB |
Output is correct |
24 |
Correct |
9 ms |
12748 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12780 KB |
Output is correct |
27 |
Correct |
9 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 |
7 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12808 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 |
12748 KB |
Output is correct |
10 |
Correct |
8 ms |
12772 KB |
Output is correct |
11 |
Correct |
8 ms |
12828 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
9 ms |
12748 KB |
Output is correct |
16 |
Correct |
8 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12748 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12796 KB |
Output is correct |
20 |
Correct |
9 ms |
12748 KB |
Output is correct |
21 |
Correct |
9 ms |
12764 KB |
Output is correct |
22 |
Correct |
9 ms |
12748 KB |
Output is correct |
23 |
Correct |
8 ms |
12748 KB |
Output is correct |
24 |
Correct |
9 ms |
12748 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12780 KB |
Output is correct |
27 |
Correct |
9 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13080 KB |
Output is correct |
29 |
Correct |
10 ms |
13108 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
10 ms |
13100 KB |
Output is correct |
32 |
Correct |
11 ms |
13040 KB |
Output is correct |
33 |
Correct |
10 ms |
13004 KB |
Output is correct |
34 |
Correct |
10 ms |
13044 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
9 ms |
13004 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13144 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 |
7 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12808 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 |
12748 KB |
Output is correct |
10 |
Correct |
8 ms |
12772 KB |
Output is correct |
11 |
Correct |
8 ms |
12828 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
9 ms |
12748 KB |
Output is correct |
16 |
Correct |
8 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12748 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12796 KB |
Output is correct |
20 |
Correct |
9 ms |
12748 KB |
Output is correct |
21 |
Correct |
9 ms |
12764 KB |
Output is correct |
22 |
Correct |
9 ms |
12748 KB |
Output is correct |
23 |
Correct |
8 ms |
12748 KB |
Output is correct |
24 |
Correct |
9 ms |
12748 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12780 KB |
Output is correct |
27 |
Correct |
9 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13080 KB |
Output is correct |
29 |
Correct |
10 ms |
13108 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
10 ms |
13100 KB |
Output is correct |
32 |
Correct |
11 ms |
13040 KB |
Output is correct |
33 |
Correct |
10 ms |
13004 KB |
Output is correct |
34 |
Correct |
10 ms |
13044 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
9 ms |
13004 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13144 KB |
Output is correct |
40 |
Correct |
175 ms |
22776 KB |
Output is correct |
41 |
Correct |
184 ms |
22748 KB |
Output is correct |
42 |
Correct |
210 ms |
22288 KB |
Output is correct |
43 |
Correct |
171 ms |
22220 KB |
Output is correct |
44 |
Correct |
189 ms |
22268 KB |
Output is correct |
45 |
Correct |
171 ms |
21932 KB |
Output is correct |
46 |
Correct |
156 ms |
21880 KB |
Output is correct |
47 |
Correct |
184 ms |
21824 KB |
Output is correct |
48 |
Correct |
151 ms |
21700 KB |
Output is correct |
49 |
Correct |
129 ms |
21476 KB |
Output is correct |
50 |
Correct |
142 ms |
23728 KB |
Output is correct |
51 |
Correct |
131 ms |
21928 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 |
7 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12808 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 |
12748 KB |
Output is correct |
10 |
Correct |
8 ms |
12772 KB |
Output is correct |
11 |
Correct |
8 ms |
12828 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
7 ms |
12748 KB |
Output is correct |
15 |
Correct |
9 ms |
12748 KB |
Output is correct |
16 |
Correct |
8 ms |
12748 KB |
Output is correct |
17 |
Correct |
9 ms |
12748 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12796 KB |
Output is correct |
20 |
Correct |
9 ms |
12748 KB |
Output is correct |
21 |
Correct |
9 ms |
12764 KB |
Output is correct |
22 |
Correct |
9 ms |
12748 KB |
Output is correct |
23 |
Correct |
8 ms |
12748 KB |
Output is correct |
24 |
Correct |
9 ms |
12748 KB |
Output is correct |
25 |
Correct |
8 ms |
12748 KB |
Output is correct |
26 |
Correct |
8 ms |
12780 KB |
Output is correct |
27 |
Correct |
9 ms |
12748 KB |
Output is correct |
28 |
Correct |
10 ms |
13080 KB |
Output is correct |
29 |
Correct |
10 ms |
13108 KB |
Output is correct |
30 |
Correct |
10 ms |
13004 KB |
Output is correct |
31 |
Correct |
10 ms |
13100 KB |
Output is correct |
32 |
Correct |
11 ms |
13040 KB |
Output is correct |
33 |
Correct |
10 ms |
13004 KB |
Output is correct |
34 |
Correct |
10 ms |
13044 KB |
Output is correct |
35 |
Correct |
10 ms |
13004 KB |
Output is correct |
36 |
Correct |
10 ms |
13004 KB |
Output is correct |
37 |
Correct |
9 ms |
13004 KB |
Output is correct |
38 |
Correct |
10 ms |
13132 KB |
Output is correct |
39 |
Correct |
10 ms |
13144 KB |
Output is correct |
40 |
Correct |
175 ms |
22776 KB |
Output is correct |
41 |
Correct |
184 ms |
22748 KB |
Output is correct |
42 |
Correct |
210 ms |
22288 KB |
Output is correct |
43 |
Correct |
171 ms |
22220 KB |
Output is correct |
44 |
Correct |
189 ms |
22268 KB |
Output is correct |
45 |
Correct |
171 ms |
21932 KB |
Output is correct |
46 |
Correct |
156 ms |
21880 KB |
Output is correct |
47 |
Correct |
184 ms |
21824 KB |
Output is correct |
48 |
Correct |
151 ms |
21700 KB |
Output is correct |
49 |
Correct |
129 ms |
21476 KB |
Output is correct |
50 |
Correct |
142 ms |
23728 KB |
Output is correct |
51 |
Correct |
131 ms |
21928 KB |
Output is correct |
52 |
Incorrect |
197 ms |
22792 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |