#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = 2e3 + 5;
pair<ll,ll> konce[MAXN][MAXN];
ll tab[MAXN];
pair<ll,ll> ans[MAXN];
ll P[MAXN];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    ll x;
    for(ll i = 1; i <= n; ++i){
        P[i] = -1;
    }
    for(ll i = 1; i <= n; ++i){
        ll s = 0;
        for(int j = 1; j <= k; ++j){
            cin >> x;
            s += x;
            x *= n;
            tab[j] = x;
        }
        ll cur = 0;
        ll k = 1;
        for(ll j = 1; j < n; ++j){
            cur += s;
            while(cur >= tab[k]){
                cur -= tab[k];
                k++;
            }
            konce[j][i] = {cur, tab[k]};
            ll g = __gcd(konce[j][i].first, konce[j][i].second);
            konce[j][i].first /= g;
            konce[j][i].second /= g;
            konce[j][i].first += (k-1) * konce[j][i].second;
        }
    }
    for(ll i = 1; i < n; ++i){
        pair<ll,ll> obecna = {k + 10, 1};
        int v = -1;
        for(int j = 1; j <= n; ++j){
            if(P[j] != -1) continue;
            if(konce[j][i].first * obecna.second < obecna.first * konce[j][i].second){
                obecna = konce[j][i];
                v = j;
            }
        }
        ans[i] = obecna;
        P[i] = v;
    }
    for(int i = 1; i < n; ++i){
        cout << ans[i].first << " " << ans[i].second << "\n";
    }
    for(int i = 1; i <= n; ++i){
        if(P[i] == -1) P[i] = n; 
        cout << P[i] << " ";
    }
    cout << "\n";
    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... |