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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <typeinfo>
#include <vector>

#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;
#define rep(i,b) for(auto i=(0);i<(b);++i)
#define fo(i,a,b) for(auto i=(a);i<=(b);++i)
#define ford(i,a,b) for(auto i=(a);i>=(b);--i)
#define fore(a,b) for(auto &a:(b))
#define v vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define no(x) (x).begin(),(x).begin()
#define clr(x,a) memset(x,a,sizeof(x))
#define SIZE(x) (int)(x).size()

#if 0
template <typename T, size_t N> struct ma : public array<T,N> { using array<T,N>::array; T& operator[](size_t n) {
#ifdef DEBUG
           assert(n < N);
#endif
return (*static_cast<array<T,N>*>(this))[n]; } } ;
#define array ma
template <class C, class=typename C::iterator> struct _cprint { using type = void;}; template<> struct _cprint<string> {};
template <class C, class=typename _cprint<C>::type> ostream& operator<<(ostream &o, const C& v){ for(auto x:v) o<<x<<" "; return o; }

#endif

#define int long long
typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss;

//template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); }
//template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); }
//template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; }

int cond = (ll)0;
#define db(x...) { if (cond > 0) { cond--; rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } }
#define dbv(x, a, b) { if (cond > 0) { cerr << __LINE__<<": " << #x << " "; for (auto __it = (x).begin() + (a); __it < (x).begin() + (b); __it++) cerr << *__it <<" "; cerr << endl; } cerr.flush(); }

namespace Simplex{
    /*jak chcemy zmienic typ, to zmieniamy typedef'a oraz wartosc EPS,
      na doublach tez powinno dzialac, ale troche szybciej i mniej dokladnie*/
#define EPS 1E-9
    typedef long double T;
    typedef vector<T> VT;
    vector<VT> A;
    VT b,c,res;
    vi kt,N;
    int m;
    inline void pivot(int k,int l,int e){
        int x=kt[l]; T p=A[l][e];
        rep(i,k) A[l][i]/=p; b[l]/=p; N[e]=0;
        rep(i,m) if (i!=l) b[i]-=A[i][e]*b[l],A[i][x]=A[i][e]*-A[l][x];
        rep(j,k) if (N[j]){
            c[j]-=c[e]*A[l][j];
            rep(i,m) if (i!=l) A[i][j]-=A[i][e]*A[l][j];
        }
        kt[l]=e; N[x]=1; c[x]=c[e]*-A[l][x];
    }
    /*k == #zmiennych bazowych + #zmiennych niebazowych*/
    VT doit(int k){
        VT wynik; T best;
        while (1){
            int e=-1,l=-1; rep(i,k) if (N[i] && c[i]>EPS) {e=i; break;}
            if (e==-1) break;
            rep(i,m) if (A[i][e]>EPS && (l==-1 || best>b[i]/A[i][e]))
                best=b[ l=i ]/A[i][e];
            if (l==-1) /*rozwiazanie nieograniczone,zwracam cokolwiek*/ return VT();
            pivot(k,l,e);
        }
        wynik.resize(k,0); rep(i,m) wynik[kt[i]]=b[i];
        return wynik;
    }
    /*A*x<=b, maksymalizujemy x*c (a[0][0]*x[0]+a[0][1]*x[1]+...<=b[0]), dla x>=0*/
    /*jak chcemy zeby xj moglo byc ujemne to podstawiamy zamiast xj dwie nowe    */
    /*zmiene (xj1-xj2), gdzie xj1,xj2>=0, funkcja zwraca najlepszy wektor, jesli */
    /*rozwiazanie nie istnieje, lub jest nieograniczone, to zwracany jest pusty  */
    /*wektor                                                                     */
    VT simplex(vector<VT> &AA,VT &bb,VT &cc){
        int n=AA[0].size(),k;
        m=AA.size(); k=n+m+1; kt.resize(m); b=bb; c=cc; c.resize(n+m);
        A=AA; rep(i,m){ A[i].resize(k); A[i][n+i]=1; A[i][k-1]=-1; kt[i]=n+i;}
        N=vi(k,1); rep(i,m) N[kt[i]]=0;
        int pos=min_element(all(b))-b.begin();
        if (b[pos]<EPS){ /*uwaga na epsilony*/
            c=VT(k,0); c[k-1]=-1; pivot(k,pos,k-1); res=doit(k);
            if (res[k-1]>EPS) /*brak rozwiazan*/ return VT();
            rep(i,m) if (kt[i]==k-1)
                rep(j,k-1) if (N[j] && (A[i][j]<-EPS || EPS<A[i][j])){
                    pivot(k,i,j); break;
                }
            c=cc; c.resize(k,0); rep(i,m) rep(j,k) if (N[j]) c[j]-=c[kt[i]]*A[i][j];
        }
        res=doit(k-1); if (!res.empty()) res.resize(n);
        return res;
    }
};
//Begin of the code
#define int long long
typedef long double T;
typedef vector<T> VT;
typedef array<ll, 5> d5;
typedef array<array<ll, 2>, 5> d5_2;

int main2(){
    {
        int n, k;
        scanf("%lld%lld", &n, &k);
        v<ll> wynik(n);
        vector<d5_2> data(n);
        rep (j, k) rep (i, n) scanf("%lld", &data[i][j][0]);
        rep (j, k) rep (i, n) data[i][j][1] = j;

        vector<d5_2> old_data = data;

        rep (i, n) sort(data[i].begin(), data[i].begin() + k);//no(data[i]) + k);

        v<int> dp(1<<k);
        v<ll> co(k);

        //if (cond) rep (i, n)  dbv(data[i], 0, k); 
        rep (i, n) {
            int mask = 0;
            int mid = k / 2;
            rep (prz, k-1) {
                int diff = abs(data[i][prz][0] - data[i][prz+1][0]);
                int mask = 0;
                if (prz < mid) { 
                    fo (g, 0, prz) mask |= 1<<data[i][g][1];
                }
                else {
                    fo (g, prz+1, k-1) mask |= 1<<data[i][g][1];
                }
                if (diff > 0) {
                    dp[mask] += diff;
                }
            }
            rep (g, k) co[g] += abs(old_data[i][g][0] - data[i][mid][0]);
        }
        //rep (g, k) cerr << co[g] << endl;
        ll min_val = 0;
        ll max_val = (ll)1e18;
        db(min_val<<" "<<max_val);
        dbv(co, 0, k);
        dbv(dp, 0, 1 << k);
        while (min_val <= max_val) {
            ll mid = (max_val - min_val) / 2 + min_val;
            int nzero = 1<<k;

            int method = 1;
            auto fun = [&] (){
                int n = nzero;
                int m = k + nzero;
                db(m<<" "<<n);

                vector<VT> A(m,VT(n));

                db(m<<" "<<n);
                VT b(m),c(n),res;
                int row = 0;
                db("");
                rep (i, 1 << k) {
                    db(i<<" "<<dp[i])
                        A[row][row] = 1;
                    b[row] = dp[i];
                    rep (j, k) A[nzero+j][row] = (i&(1<<j)) ? -1 : 1;
                    row++;
                }
                rep (j, k) b[nzero + j] = mid-co[j];
                if (method == 0)
                    rep (j, nzero) c[j] = -(1);
                else
                    rep (j, nzero) {
                        c[j] =  -(1 + (__builtin_popcount(j) == 1 ? (int)(int)1e4 : 0));
                        //cerr << " " << j << " " <<c[j] <<endl;
                    }


                res=Simplex::simplex(A,b,c);

                T acc=0; rep(i,SIZE(res)) acc+=res[i]*c[i];
                //cerr << "Maks :  acc " << acc << endl;
                //printf("maksymalna funkcja celu: %.6Lf\n", acc);
                return res;
            };

            auto res = fun();

            //if (SIZE(res) == 0) cerr << "FALSE " << endl;
            /*
               printf("\n");
               T acc=0; rep(i,SIZE(res)) acc+=res[i]*c[i];
               printf("maksymalna funkcja celu: %.6Lf\n", acc);
               */
            if (min_val == max_val) {
                int cnt = 0;
                int cnt2 = 0;
                rep (i, SIZE(res)) if (res[i] >= 5 * EPS) cnt++;
                rep (i, SIZE(res)) if (abs(res[i] - (ll)(res[i] + 2*EPS)) >= 10*EPS) cnt2++;

                if (cnt2 > 0) {
                    int cnt3 = 0;
                    rep (i, SIZE(res)) if (abs(res[i] - (ll)(res[i] + 2*EPS)) >= 10*EPS) cnt3++;
                    if (cnt2 != cnt3) {
                        cout << cnt2 << " " << cnt3 << endl;
                    }
                }
                //cerr << "XX " << cnt2 << endl;
                db(min_val);
                //if (cond) rep (i, 1 << k) cerr << "I << " << i << " " << res[i] << endl;
                v<ll> res_int(1<<k);
                //rep (i, SIZE(res)) if ( (ll)(res[i] + 0.1) != (ll)(res[i] + 0.8)) 
                    //cerr << "XX " << i << " "<<res[i] <<  endl;

                rep (i, SIZE(res)) res_int[i] = (ll)(res[i] + 0.8);
                auto ocen = [&] (){
                    v<ll> wart = co;
                    rep (i, 1<<k) if (res_int[i]) {
                        rep (j, k) 
                            if ((i&(1<<j))) wart[j] -= res_int[i]; 
                            else wart[j] += res_int[i];
                    }
                    rep (j, 5) {
                        //cerr<<"!! " << j << " " <<wart[j] << endl;
                    }
                    
                    return  *max_element(all(wart));
                };
                ocen();
                vi nz;
                rep (gg, 1<<k) if (res_int[gg] > 0) {
                    nz.pb(gg);
                }
                //cerr << fixed;
                //rep (i, 1 << k) if (dp[i]) cerr << "! " << i << " "<<dp[i] << endl;
                //rep (i, 1 << k) if (res[i]) cerr << "!X " << i << " " << res[i] << endl;

                ll best = (ll)ocen();
                auto best_res = res_int;
                rep (it, (1<<(int)nz.size())) {
                    bool ok = 1;
                    rep (j, k) if (it & (1<<j)) {
                        res_int[nz[j]]--;
                    }

                    ll cur = ocen();
                    if (cur < best) {
                        best = cur;
                        best_res = res_int;
                    }

                    rep (j, k) if (it & (1<<j)) res_int[nz[j]]++;
                }
                res_int = best_res;
                //cerr << "BEST " << best << endl;
                
                rep (i, n) {
                    int mask = 0;
                    int mid = k / 2;
                    v<ll> ddiff(k-1);
                    v<ll> mma(k-1);
                    rep (prz, k - 1) {
                        int diff = abs(data[i][prz][0] - data[i][prz+1][0]);
                        int mask = 0;
                        if (prz < mid) { 
                            fo (g, 0, prz) mask |= 1<<data[i][g][1];
                        }
                        else {
                            fo (g, prz+1, k-1) mask |= 1<<data[i][g][1];
                        }
                        ddiff[prz] = diff;
                        mma[prz] = mask;
                    }
                    int st = 0;
                    ll cval = data[i][mid][0];
                    ford (x, mid - 1, 0){ 
                        ll ile = min(res_int[mma[x]], ddiff[x]);
                        res_int[mma[x]] -= ile;
                        cval -= ile;
                        if (ile) st |= 1;
                    }
                    fo (x, mid, k - 2) {
                        ll ile = min(res_int[mma[x]], ddiff[x]);
                        if (ile) st |= 2;
                        res_int[mma[x]] -= ile;
                        cval += ile;
                    }
                    wynik[i] = cval;
                   // assert(st != 3);
                }
                //rep (i, 1 << k) if (res_int[i] > 0) cerr << "I << " << i << " " << res_int[i] << endl;
                v<ll> val(k, 0);
                rep (j, k) {
                    rep (i, n) {
                        ll diff = abs(old_data[i][j][0] - wynik[i]);
                        assert(old_data[i][j][1] == j);
                        if (cond) cerr << " " << i << " " << j << " " << diff << " "<<old_data[i][j][0] << endl;

                        val[j] += diff;
                    }

                }
                //rep (j, k ) cerr <<" " << j << " " << val[j] << endl;
                //assert(*max_element(all(res_int)) == 0);
                //assert(*max_element(all(val)) == min_val + cnt2 / 2);
#if 1
                rep (i, n) {
                    if (i) printf(" ");
                    printf("%d", wynik[i]);
                }
                printf("\n");
#else
                rep (i, (int)res.size()) {
                    if (res[i] < 0.3)
                        dp[i] = 0;
                }
                //dp[17] = (ll)res[17];
                auto hmm = fun();
                cout.precision(10);
                cout << fixed;
                dbv(hmm, 0, (int)hmm.size());
                /*
                rep (i, (int)hmm.size()) cout << hmm[i] << " "; cout << endl;
                cout << endl;


                rep (i, (int)res.size()) cout << res[i] << " "; cout << endl;
                */
                cout << *max_element(all(val)) << endl;
#endif


                if (cond) cerr<< " X " << (*max_element(all(val))) <<" "<<min_val<< endl;
                break;
                }
                if (SIZE(res) == 0) {
                    min_val = mid + 1;
                }
                else
                    max_val = mid;
                db(SIZE(res)<<" "<<mid);

            }
        }
        if (0)
        {
            int m=2,n=3; // wyr zmienne
            vector<VT> A(m,VT(n));
            VT b(m),c(n),res;
            A[0][0]=-0.5; A[0][1]=-1; A[0][2]=2; b[0]=-2;
            A[1][0]=1; A[1][1]=2; A[1][2]=0; b[1]=100;
            c[0]=5; c[1]=-1.5; c[2]=0.1;
            printf("Ograniczenia:\nx1,x2,x3>=0\n");
            rep(i,m){
                rep(j,n) {printf(" %.6Lf*x%d ",A[i][j],j); if (j<n-1) printf("+");}
                printf("<= %.6Lf\n",b[i]);
            }
            printf("Maksymalizujemy: ");
            rep(i,n) {printf(" %.6Lf*x%d ",c[i],i); if (i<n-1) printf("+");}
            printf("\n");
            res=Simplex::simplex(A,b,c);
            printf("najlepszy wektor: "); rep(i,SIZE(res)) printf("x%d = %.6Lf, ",i,res[i]);
            printf("\n");
            T acc=0; rep(i,SIZE(res)) acc+=res[i]*c[i];
            printf("maksymalna funkcja celu: %.6Lf\n", acc);
            return 0;
        }
    }

    void solve() {

    }

    void brute() {

    }

    void gen(int val, int n, int k) {
        srand(time(0) + val * (int)1e9 + 7);
        n += rand() % 20;
        cout << n << " " << k << endl;
        rep (j, k) {
            rep (i, n) {
                cout << rand() % 10 << " ";
            }
            cout << endl;
        }
    }

#undef int
    int main(int argc, char ** argv) {
        //ios::sync_with_stdio(false);
        //  freopen("../1.in","r",stdin); 
        //  freopen("../2.in","r",stdin); 
        //  freopen("../3.in","r",stdin); 
        //  freopen("../A-small.in","r",stdin); freopen("../A-small.out","w",stdout);
        //  freopen("../A-small-attempt0.in","r",stdin);freopen("../A-small-attempt0.out","w",stdout);
        //  freopen("../A-small-attempt1.in","r",stdin);freopen("../A-small-attempt1.out","w",stdout);
        //  freopen("../A-small-attempt2.in","r",stdin);freopen("../A-small-attempt2.out","w",stdout);
        //  freopen("../A-large.in","r",stdin); freopen("../A-large.out","w",stdout)

        vs args;
        int nr_test = -1;
        int t = 1;

        fo (i, 1, argc - 1) args.pb(argv[i]);
        fore (it, args) if (it == "q") cond = 1000;
        fore (it, args) if (it == "all") t = 1000;
        fore (it, args) if (atoi(it.c_str()) > 0) nr_test = atoi(it.c_str());

        fore (it, args) if (it == "b") { brute(); return 0; }
        fore (it, args) if (it == "g" && args.size() >= 4) { gen(atoi(args[1].c_str()), atoi(args[2].c_str()), atoi(args[3].c_str())); return 0; }


    fo (i, 1, t) {
        solve();
    }
    main2();
    return 0;
}