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>
#include "tickets.h"
using namespace std;
 
using ll = long long;
using pii = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int INF = 5e8, N = 1e5+10, M = 998244353, LOG = 16;
const ll LINF = 1e18;
ll find_maximum(int k, vector<vector<int>> x) {
    ll tot_sum = 0;
    priority_queue<pair<int, int>> pq;
    
    int n = x.size(), m = x[0].size();
    vector<int> lp(n, k-1), rp(n, m-1);
    for (int i = 0; i < n; i++) {
        for(int j = 0; j < k; j++) {
            tot_sum -= x[i][j];
        }
        pq.push({x[i][lp[i]] + x[i][rp[i]], i});
    }
    int to_swap = n/2 * k;
    vector<vector<int>> b(n, vector<int>(m, -1));
    
    while(!pq.empty() and to_swap) {
        auto [inc, c] = pq.top(); pq.pop();
        
        tot_sum += inc;
        to_swap--;
        b[c][rp[c]] = 1;
        lp[c]--; rp[c]--;
        if(lp[c] >= 0) {
            pq.push({x[c][lp[c]] + x[c][rp[c]], c});
        }
    }
    assert(to_swap == 0);
 
    vector<int> cnt(k);
    
    for(int i = 0; i < n; i++) {
        vector<int> to_add, used(k);
        
        for(int j = 0; j < k; j++)
            if(cnt[j] < n/2) to_add.push_back(j);
        
        sort(all(to_add), [&] (int i, int j){
            return cnt[i] < cnt[j];
        });
        for(int j = 0; j <= lp[i]; j++) {
            b[i][j] = to_add[j];
            used[to_add[j]] = true;
            cnt[to_add[j]]++;
        }    
        vector<int> good;
        for(int j = 0; j < k; j++) 
            if(!used[j]) good.push_back(j);
        
        for(int j = rp[i]+1, ind = 0; j < m; j++) {
            b[i][j] = good[ind++];
        }
    }
    allocate_tickets(b);
    return tot_sum;
}
Compilation message (stderr)
tickets.cpp: In function 'll find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   31 |         auto [inc, c] = pq.top(); pq.pop();
      |              ^| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |