1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>
#include <map>
#include <climits>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

#define N_MAX 100000
#define K_MAX 5

int n,k;
LL orig[N_MAX][K_MAX];
LL pi[N_MAX][K_MAX];
int neg[N_MAX];

void readData(){
    scanf("%d%d",&n,&k);
    REP(j,k) REP(i,n) scanf("%lld",&(pi[i][j]));
    while (k < 5){
        REP(i,n) pi[i][k] = pi[i][0];
        k++;
    }
    REP(i,n) REP(j,k) orig[i][j] = pi[i][j];
   /* REP(i,n){
        REP(j,k) cerr << pi[i][j] << " ";
        cerr << endl;
    }*/
}

void transformNegate(){
    REP(i,n)
        if (pi[i][0] > pi[i][1]){
            neg[i] = 1;
            REP(j,k) pi[i][j] = -pi[i][j];
        } else
            neg[i] = 0;
}

int code(int i, VII &order){
    REP(j,k) order[j] = MP(pi[i][j],j);
    sort(ALL(order));
    int res = 0;
    REP(j,k){
        res = (res << 3) + order[j].Y;
    }
    return res;
}

#define P_MAX 60

map<int,int> permInd;
VVI pList;
LL p[P_MAX][K_MAX];
int o[P_MAX][K_MAX];
int P;

void transformGather(){
    VII order(k);
    P = 0;
    REP(i,n){
        int c = code(i,order);
        //DB(c);
        if (permInd.find(c) == permInd.end()){
            permInd[c] = P;
            pList.PB(VI());
            REP(j,k) p[P][j] = 0;
            REP(j,k) o[P][j] = order[j].Y;
            P++;
        }
        int ind = permInd[c];
        //DB(ind);
        pList[ind].PB(i);
        REP(j,k)
            p[ind][j] += pi[i][j];
    }
    /*REP(i,P){
        REP(j,k) cout << p[i][j] << " ";
        cout << endl;
    }*/
    //DB(P);
}

LL contr[P_MAX][K_MAX];
LL sum[K_MAX];
LL score;

void update(int i, LL *sol){
    REP(j,k){
        contr[i][j] = abs(sol[i]-p[i][j]);
        sum[j] += contr[i][j];
    }
    score = 0;
    REP(j,k) score = max(score,sum[j]);
}

VC<pair<LL,int>> state;
int x;
VC<pair<LL,LL>> moves;
VC<pair<LL,LL>> toMove;
LL extraMove;
int y,z;
LL extraToMove;

LL sol[P_MAX];

void initX(){
    REP(j,k) sum[j] = 0;
    REP(i,P) { 
        sol[i] = p[i][o[i][2]];
        update(i,sol);
    }
    //DB(score);
    //REP(j,5) DB(sum[j]);
    
    state.clear();
    REP(j,5) state.PB(MP(sum[j],j));
    sort(ALL(state)); reverse(ALL(state));
    x = state[0].Y;
    y = state[1].Y;
    z = state[2].Y;
    //DB(x); DB(y); DB(z);
}

void computeMoves(){
    moves.clear();
    moves.resize(5,MP(0LL,0LL));
    toMove.clear();
    toMove.resize(5,MP(0LL,0LL));
    extraMove = 0; extraToMove = 0;
    REP(i,P){
        if (o[i][0] == x){
            moves[o[i][1]].X += p[i][o[i][2]]-p[i][o[i][1]];
            moves[o[i][1]].Y += p[i][o[i][1]]-p[i][o[i][0]];
        } else if (o[i][1] == x){
            moves[o[i][0]].X += p[i][o[i][2]]-p[i][o[i][1]];
        } else if (o[i][4] == x){
            moves[o[i][3]].X += p[i][o[i][3]]-p[i][o[i][2]];
            moves[o[i][3]].Y += p[i][o[i][4]]-p[i][o[i][3]];
        } else if (o[i][3] == x){
            moves[o[i][4]].X += p[i][o[i][3]]-p[i][o[i][2]];
        }

        if ((o[i][0] == y && o[i][1] == z) || (o[i][0] == z && o[i][1] == y))
            extraMove += p[i][o[i][2]] - p[i][o[i][1]];
        if ((o[i][3] == y && o[i][4] == z) || (o[i][3] == z && o[i][4] == y))
            extraMove += p[i][o[i][3]] - p[i][o[i][2]];
    }
    //REP(j,5) cerr << moves[j].X << " " << moves[j].Y << endl;
}

#define INFTY LLONG_MAX

void makeMove(int j, LL step){
    //DB(j); DB(step);
    toMove[j].X += step;
    moves[j].X -= step;
    REP(q,5) if (state[q].Y == x || state[q].Y == j)
        state[q].X -= step;
    else
        state[q].X += step;
    score -= step;
}

void makeMoveY(int j, LL step){
    //DB(j); DB(step);
    toMove[j].Y += step;
    moves[j].Y -= step;
    REP(q,5) if (state[q].Y == x || state[q].Y == j)
        state[q].X -= step;
    else
        state[q].X += step;
    score -= step;
}

LL singleOut(){
    initX();
    
    computeMoves();

    //REP(j,5) cerr << p[0][j] << " "; cerr << endl; 

    VC<LL> C(5); REP(j,5) C[j] = (sum[x]-sum[j])/2;
    LL range = INFTY; 
    REP(j,5) if (j != x) range = min(range,C[j] + moves[j].X);

    LL left = 0, right = range+1;
    while (left+1 < right){
        LL mid = (left+right)/2;
        LL Csum = 0, Csmall = 0;
        REP(j,5) if (j != x && C[j] < mid){
            Csum += C[j]; Csmall++;
        }
        if (mid * (Csmall-1) <= Csum)
            left = mid;
        else
            right = mid;
    }

    LL best = left; //min(CS/3,range);
    LL curBest = best;
    //DB(best);
    REP(j,5) if (j != x){
        // push best-C[j]
        LL step = best-C[j];
        if (step <= 0)
            continue;
        makeMove(j,step);
        curBest -= step;
    }

    REP(j,5) if (j != x && moves[j].X > 0 && curBest > 0){
        LL step = min(moves[j].X,curBest);
        makeMove(j,step);
        curBest -= step;
    }
    
    //DB(extraMove);
    //REP(j,5) cerr << state[j].X << " "; cerr << endl;

    if (state[2].X >= state[0].X-1 && state[1].X >= state[0].X-1 &&
            state[0].X >= state[3].X+4 && state[0].X >= state[4].X+4){
        LL step = min( (state[0].X-state[3].X)/4,(state[0].X-state[4].X)/4);
        FOR(j,1,3) step = min(step,moves[state[j].Y].X);
        step = min(step,extraMove);
        if (step > 0){
            FOR(j,1,3) makeMove(state[j].Y,step);
            extraToMove += step;
            extraMove -= step;
            score += step;
            state[0].X += step;
            FOR(j,1,3) state[j].X -= step;
            FOR(j,3,5) state[j].X += step;
        }
    }

    //REP(j,5) cerr << state[j].X << " "; cerr << endl;
    //REP(j,5) cerr << moves[state[j].Y].X << " "; cerr << endl;
    //REP(j,5) cerr << toMove[state[j].Y].X << " "; cerr << endl;
    //DB(extraToMove);
    // superStep
    auto stateCopy = state;
    sort(stateCopy.begin()+1,stateCopy.end());
    if (stateCopy[4].X == stateCopy[0].X-1 && 
        stateCopy[3].X == stateCopy[0].X-1 &&
        stateCopy[2].X <= stateCopy[0].X-3){
        makeMove(stateCopy[4].Y,1);
        makeMove(stateCopy[3].Y,1);
    }
     
    LL restMax = 0LL;
    REP(j,5) if (j != 0) restMax = max(restMax,state[j].X);
    LL spaceLeft = state[0].X - restMax;
    //DB(spaceLeft);
    REP(j,5) if (j != x && moves[j].Y > 0 && spaceLeft > 1){
        LL step = min(spaceLeft/2,moves[j].Y);
        makeMoveY(j,step);
        spaceLeft -= 2*step;
    }

    return score;
}

void dump(VC<pair<LL,LL>> &toMove){
    /* converts to original coordinates and dumps solution to out */
    //REP(j,5) cerr << toMove[j].X << " " << toMove[j].Y << endl;
    //cerr << "toMove: " << endl;
    //REP(j,k) cerr << toMove[j].X << " ";
    //cerr << endl;
    VC<LL> sol(n);
    REP(i,P) for(auto e : pList[i]){
        if (o[i][0] == x){
            LL step = min(pi[e][o[i][2]]-pi[e][o[i][1]],toMove[o[i][1]].X);
            sol[e] = pi[e][o[i][2]] - step;
            toMove[o[i][1]].X -= step;
            if (sol[e] == pi[e][o[i][1]] && toMove[o[i][1]].Y > 0){
                step = min(pi[e][o[i][1]]-pi[e][o[i][0]],toMove[o[i][1]].Y);
                sol[e] = pi[e][o[i][1]] - step;
                toMove[o[i][1]].Y -= step;
            }
        } else if (o[i][1] == x){
            LL step = min(pi[e][o[i][2]]-pi[e][o[i][1]],toMove[o[i][0]].X);
            sol[e] = pi[e][o[i][2]] - step;
            toMove[o[i][0]].X -= step;
        } else if (o[i][4] == x){
            LL step = min(pi[e][o[i][3]]-pi[e][o[i][2]],toMove[o[i][3]].X);
            sol[e] = pi[e][o[i][2]] + step;
            toMove[o[i][3]].X -= step;
            if (sol[e] == pi[e][o[i][3]] && toMove[o[i][3]].Y > 0){
                step = min(pi[e][o[i][4]]-pi[e][o[i][3]],toMove[o[i][3]].Y);
                sol[e] = pi[e][o[i][3]] + step;
                toMove[o[i][3]].Y -= step;
            }
        } else if (o[i][3] == x){
            LL step = min(pi[e][o[i][3]]-pi[e][o[i][2]],toMove[o[i][4]].X);
            sol[e] = pi[e][o[i][2]] + step;
            toMove[o[i][4]].X -= step;
        } else if ((o[i][0] == y && o[i][1] == z) || (o[i][0] == z && o[i][1] == y)){
            LL step = min(pi[e][o[i][2]]-pi[e][o[i][1]],extraToMove);
            sol[e] = pi[e][o[i][2]] - step;
            extraToMove -= step;
        } else if ((o[i][3] == y && o[i][4] == z) || (o[i][3] == z && o[i][4] == y)){
            LL step = min(pi[e][o[i][3]]-pi[e][o[i][2]],extraToMove);
            sol[e] = pi[e][o[i][2]] + step;
            extraToMove -= step;
        } else 
            sol[e] = pi[e][o[i][2]];

    }
    //print(shift);
    //print(sol);
    /*VC<LL> c(5,0LL);
    REP(i,n)
        REP(j,5)
            c[j] += abs(sol[i]-pi[i][j]);*/
    //print(sol);
    //DB(score);
    //LL s = 0;
    //REP(j,5) DB(c[j]);
    /*REP(j,5) s = max(s,c[j]);
    DB(s);*/
    
    REP(i,n)
        if (neg[i])
            sol[i] = -sol[i];
        
    /*VC<LL> c2(5,0LL);
    REP(i,n)
        REP(j,5)
            c2[j] += abs(sol[i]-orig[i][j]);
    s = 0;
    REP(j,5) s = max(s,c2[j]);
    DB(s);*/
    REP(i,n-1)
        printf("%lld ",sol[i]);
    printf("%lld\n",sol[n-1]);
}

void solve(){
    transformNegate();
    transformGather();
    singleOut();
    dump(toMove);
}

int main(int argc, char *argv[]){
    readData();
    solve();
    return 0;
}