This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define pb       push_back
#define Sz(x)    int((x).size())
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
#define cl       clear
 
const int maxn  =  1e6   + 10;
const int maxa  =  2e9   +  5;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;
const double eps = 1e-9;
int a[maxn], b[maxn], c[maxn], ps[maxn], n, m;
vector <int> vc[maxn];
multiset <int> s;
int cal(int ind){
    s.cl();
    fill(ps, ps + n, 0);
    for (int i = 0; i < n; i ++) vc[i].cl();
    for (int i = 0; i < m; i ++) a[i] = (a[i] - ind + n) % n, b[i] = (b[i] - ind + n) % n;
    for (int i = 0; i < m; i ++){
        int x = min(a[i], b[i]), y = max(a[i], b[i]);
        ps[x] ++;
        ps[y] --;
        vc[x].pb(y);
    }
    int ans = 0;
    for (int i = 0; i < n; i ++){
        while (!s.empty() && *s.begin() == i) s.erase(s.begin());
        for (int x : vc[i]) s.insert(x);
        if(i) ps[i] += ps[i - 1];
        while (ps[i] > 0){
            int t = *s.rbegin();
            s.erase(s.find(t));
            ps[i] -= 2;
            ps[t] += 2;
            ans ++;
        }
    }
    for (int i = 0; i < m; i ++) a[i] = (a[i] + ind) % n, b[i] = (b[i] + ind) % n;
    return ans;
}
int main(){
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i ++) cin >> a[i] >> b[i] >> c[i], a[i] --, b[i] --;
    int ans = mod;
    for (int i = 0; i < n; i ++) ans = min(ans, cal(i));
    cout << ans << endl;
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |