#include "tickets.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1501;
int cnt[N],lst[N];
vector<int> add[N],mns[N];
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
std::vector<std::vector<int>> answer;
set<pair<ll,int>> fst;
for (int i = 0; i < n; i++) {
std::vector<int> row(m,-1);
answer.push_back(row);
}
ll ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
ans-=x[i][j];
answer[i][j]=j;
fst.insert({x[i][m-k+j]+x[i][j],i*m+j});
}
}
for(int i=0;i<(k*n)/2;i++)
{
ans+=rbegin(fst)->first;
int j=rbegin(fst)->second/m,k1=rbegin(fst)->second%m;
// swap(answer[j][k1],answer[j][m-k+k1]);
fst.erase(--end(fst));
}
// ll ans=0;
// int tot=n/2 * k;
// while(tot>0)
// {
// auto it=*rbegin(fst);
// int i=it.second;
// fst.erase(--end(fst));
// if(cnt[i]==k)continue;
// add[i].push_back(lst[i]);
// tot--;
// cnt[i]++;
// ans+=it.first;
// fst.insert({x[i][--lst[i]],i});
// }
// tot=n/2 * k;
// fst.clear();
// for(int i=0;i<n;i++)
// {
// lst[i]=0;
// fst.insert({x[i][lst[i]++],i});
// }
// while(tot>0)
// {
// auto it=*begin(fst);
// int i=it.second;
// fst.erase(begin(fst));
// if(cnt[i]==k)continue;
// mns[i].push_back(lst[i]-1);
// tot--;
// cnt[i]++;
// ans-=it.first;
// fst.insert({x[i][lst[i]++],i});
// }
// int l=0;
// for(int i=0;i<n;i++)
// {
// for(auto j:add[i])
// {
// answer[i][j]=(l++)%k;
// }
// int r=l;
// for(auto j:mns[i])
// {
// answer[i][j]=(r++)%k;
// }
// }
allocate_tickets(answer);
return ans;
}