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
#define debug if(0)
// Grzegorz Guspiel
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for(int i=0;i<int(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define VAR(x) #x << " " << x << " "
#define pb push_back
#define mp make_pair
#define st first
#define nd second


typedef pair<int,int> PII;
typedef long long LL;
template<typename T> void maxE(T& a, const T& b) { a = max(a, b); }
template<typename T> void minE(T& a, const T& b) { a = min(a, b); }

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& t) {
    out << "[";
    FOREACH (i, t) out << *i << ", ";
    out << "]";
    return out;
}

template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
    out << "(" << rhs.st << "," << rhs.nd << ")";
    return out;
}

LL abs(LL a, LL b) { return (a > b) ? a - b : b - a; }
LL sgn(LL a) {
    if (a > 0) return 1;
    else if (a == 0) return 0;
    else return -1;
}

const int MAXN = 100100;
const int MAXK = 6;
const LL INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000LL;

int n, k;
vector<LL> t[MAXK];
vector<LL> r;

LL dist(vector<LL>& a, vector<LL>& b) {
    LL r = 0;
    REP (i, n) r += abs(a[i], b[i]);
    return r;
}

LL dist(int a, int b) { return abs(a - b); }

PII farthestTwo() {
    pair<LL, PII> best(-INF, mp(0, 0));
    REP (i, k) for (int j = i + 1; j < k; j++) {
        pair<LL, PII> cur;
        cur.st = dist(t[i], t[j]);
        cur.nd = mp(i, j);
        best = max(best, cur);
    }
    return best.nd;
}

LL div2Floor(LL a) {
    if (a < 0) return -(((-a) + 1) / 2);
    else return a / 2;
}
LL div2Ceil(LL a) {
    if (a < 0) return -((-a) / 2);
    else return (a + 1) / 2;
}

void solveTwo() {
    int flip = 0;
    REP (i, n) {
        if (dist(t[0][i], t[1][i]) % 2 == 0) {
            r[i] = (t[0][i] + t[1][i]) / 2;
            continue;
        }
        if ((flip + (t[0][i] < t[1][i])) % 2) r[i] = div2Ceil(t[0][i] + t[1][i]);
        else r[i] = div2Floor(t[0][i] + t[1][i]);
        flip = 1 - flip;
    }
}

void solveThree() {
    PII f2 = farthestTwo();
    if (f2.st > f2.nd) swap(f2.st, f2.nd);
    if (f2 == mp(0, 2)) swap(t[1], t[2]);
    if (f2 == mp(1, 2)) swap(t[2], t[0]);

    REP (i, n) {
        r[i] = t[2][i];
        LL lo = t[1][i];
        LL hi = t[0][i];
        if (lo > hi) swap(lo, hi);
        minE(r[i], hi);
        maxE(r[i], lo);
    }

    LL dist10 = dist(t[1], r) - dist(t[0], r);
    debug cout << VAR(t[0][0]) << VAR(t[1][0]) << VAR(t[2][0]) << endl;
    REP (i, n) {
        LL goal = dist10 / 2;
        if (!goal) continue;
        if (goal > 0 && r[i] == t[1][i]) continue;
        if (goal < 0 && r[i] == t[0][i]) continue;

        debug cout << VAR(i) << VAR(dist10) << VAR(goal) << endl;
        // goal < 0
        if (goal < 0) {
            LL steps = abs(goal);
            minE(steps, abs(r[i] - t[0][i]));
            r[i] += sgn(t[0][i] - r[i]) * steps;
            dist10 += 2 * steps;
            //assert(dist10 == dist(t[1], r) - dist(t[0], r));
        }
        if (goal > 0) {
            LL steps = abs(goal);
            minE(steps, abs(r[i] - t[1][i]));
            r[i] += sgn(t[1][i] - r[i]) * steps;
            dist10 -= 2 * steps;
            //assert(dist10 == dist(t[1], r) - dist(t[0], r));
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n >> k;
    //k = 2;
    REP (i, k) REP (j, n) {
        int a; cin >> a;
        t[i].pb(a);
    }
    r.assign(n, 0);
    if (k == 2) solveTwo();
    else if (k == 3) solveThree();
    if (true) {
        REP (i, n) cout << r[i] << " ";
        cout << endl;
    } 
    if (false) {
        LL res = 0;
        REP (i, k) maxE(res, dist(r, t[i]));
        cout << res << endl;
    }
	return 0;
}