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
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

ll min (ll a, int b) {
   return a < b ? a : b;
}

#define N 100007
int x[6][N], n, k, res[N];

bool dasie (const ll M) {
   // powinno wypelnic res[] jesli zwraca true
   FORI(i,n) res[i] = x[3][i];
   ll dist[4] = {0,0,0,0}; // dist[t] = dist(res, x[t])
   FORI(t,2) FORI(i,n) dist[t] += abs(res[i] - x[t][i]);
   if (dist[1] <= M && dist[2] <= M) return 1; // ok
   FORI(i,n) {
      ll mini = min(x[1][i], x[2][i]), maxi = max(x[1][i], x[2][i]);
      if (mini <= res[i] && res[i] <= maxi) {
         // nie jest jakos epicko dobrze
      } else if (res[i] < mini) {
         ll todo = min(max(dist[1] - M, dist[2] - M), mini - res[i]);
         if (todo > 0) {
            res[i] += todo;
            dist[1] -= todo;
            dist[2] -= todo;
            dist[3] += todo; // to kosztuje, ale robie to bo musze (bo dist[1] > M lub dist[2] > M)
         }
      } else { // maxi < res[i]
         ll todo = min(max(dist[1] - M, dist[2] - M), res[i] - maxi);
         if (todo > 0) {
            res[i] -= todo;
            dist[1] -= todo;
            dist[2] -= todo;
            dist[3] += todo;
         }
      }
   }
   if (dist[3] > M) return 0;
   if (dist[1] <= M && dist[2] <= M) return 1; // ok
   /*FORI(i,n) {
      assert(res[i] == x[1][i] || res[i] == x[2][i] || res[i] == x[3][i]);
      assert(!(res[i] > x[1][i] && res[i] > x[2][i]));
      assert(!(res[i] < x[1][i] && res[i] < x[2][i]));
      ll suma = (ll)x[1][i] + x[2][i] + x[3][i];
      suma -= min(min(x[1][i], x[2][i]), x[3][i]);
      suma -= max(max(x[1][i], x[2][i]), x[3][i]);
      // srodek
      assert(suma == res[i]);
   }*/
   if (dist[1] + dist[2] > 2 * M) return 0;
   if (dist[1] <= M) {
      //assert(dist[2] > M);
      FORI(i,n) {
         if (x[2][i] <= res[i]) {
            // w lewo
            ll todo = min(dist[2] - M, res[i] - x[2][i]);
            if (todo > 0) {
               res[i] -= todo;
               dist[2] -= todo;
               dist[1] += todo;
               dist[3] += todo;
            }
            if (dist[3] > M) return 0;
            if (dist[1] <= M && dist[2] <= M) return 1; // ok
         } else {
            // w prawo
            ll todo = min(dist[2] - M, x[2][i] - res[i]);
            if (todo > 0) {
               res[i] += todo;
               dist[2] -= todo;
               dist[1] += todo;
               dist[3] += todo;
            }
            if (dist[3] > M) return 0;
            if (dist[1] <= M && dist[2] <= M) return 1; // ok
         }
      }
   } else {
      // analogicznie
      //assert(dist[1] > M);
      FORI(i,n) {
         if (x[1][i] <= res[i]) {
            // w lewo
            ll todo = min(dist[1] - M, res[i] - x[1][i]);
            if (todo > 0) {
               res[i] -= todo;
               dist[1] -= todo;
               dist[2] += todo;
               dist[3] += todo;
            }
            if (dist[3] > M) return 0;
            if (dist[1] <= M && dist[2] <= M) return 1; // ok
         } else {
            // w prawo
            ll todo = min(dist[1] - M, x[1][i] - res[i]);
            if (todo > 0) {
               res[i] += todo;
               dist[1] -= todo;
               dist[2] += todo;
               dist[3] += todo;
            }
            if (dist[3] > M) return 0;
            if (dist[1] <= M && dist[2] <= M) return 1; // ok
         }
      }
   }
   // juz raczej powinien byc return do tej pory
   //assert(0);
   if (dist[3] > M) return 0;
   if (dist[1] <= M && dist[2] <= M) return 1; // ok
   return 0; // no tutaj to juz na 100% nie powinnismy dojsc
}

int main () {
   scanf("%d %d", &n, &k);
   //k = 3;
   FORI(t,k) FORI(i,n) scanf("%d", &x[t][i]);
   if (k == 2) {
      ll dist = 0;
      FORI(i,n) dist += abs(x[1][i] - x[2][i]);
      dist = (dist + 1) / 2;
      //cout << dist; return 0;
      FORI(i,n) {
         if (abs(x[1][i] - x[2][i]) <= dist) {
            dist -= abs(x[1][i] - x[2][i]);
            x[2][i] = x[1][i];
         } else {
            if (x[1][i] > x[2][i]) {
               x[2][i] += dist;
            } else {
               x[2][i] -= dist;
            }
            break;
         }
      }
      FORI(i,n) printf("%d ", x[2][i]);
   } else if (k == 3) {
      ll from = 0, best = 10000000000000000LL, to = best - 1;
      while (from <= to) {
         ll mid = (from + to) / 2;
         if (dasie(mid)) {
            best = mid;
            to = best - 1;
         } else {
            from = mid + 1;
         }
      }
      dasie(best);
      
      //assert(dasie(best));
      //cout << best; return 0;
      /*ll dist[4] = {0,0,0,0};
      FORI(t,3) FORI(i,n) dist[t] += abs(res[i] - x[t][i]);
      ll maxdist = max(max(dist[1], dist[2]), dist[3]);
      //cout << best; return 0;
      assert(maxdist == best);
      cout << maxdist << endl;
      return 0;*/
      
      FORI(i,n) printf("%d ", res[i]);
   } else {
      // TODO LOL
      FORI(i,n) printf("%d ", x[1 + rand() % k][i]);
   }
}