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
#include <cstdio>
#include <vector>
#include <algorithm>

//#define WYNIK

using namespace std;

typedef long long int ll;

inline int bez(int a) { return (a < 0) ? -a : a; }

int n, k;

int cia[6][100005];

vector<int> wynik;

void dwa();
void trzy();
void poprawiaj();

void poprawiaj2(int a, int b, int c)
{
	ll par[6][6];
	ll wys[6];
	for(int i = 0; i < 6; i++)
	{
		for(int j = 0; j < 6; j++)
			par[i][j] = 0LL;
		wys[i] = 0LL;
	}
	vector<pair<int, int> > v;
	for(int i = 1; i <= n; i++)
	{
		v.clear();
		for(int j = 1; j <= k; j++)
		{
			v.push_back(make_pair(cia[j][i], j));
			wys[j] += bez(cia[j][i] - wynik[i]);
		}
		sort(v.begin(), v.end());
		ll h = wynik[i] - v[1].first;
		par[v[0].second][v[1].second] += h;
		par[v[1].second][v[0].second] += h;
		h = v[k - 2].first - wynik[i];
		par[v[k - 1].second][v[k - 2].second] += h;
		par[v[k - 2].second][v[k - 1].second] += h;
	}
	vector<pair<ll, int> > heh;
	for(int i = 1; i <= k; i++)
		heh.push_back(make_pair(wys[i], i));
	sort(heh.begin(), heh.end());
	reverse(heh.begin(), heh.end());
	ll nope = (heh[a].first - heh[c].first) / 2;
	nope = min(nope, par[heh[a].second][heh[b].second]);
	for(int i = 1; i <= n && nope > 0; i++)
	{
		v.clear();
		for(int j = 1; j <= k; j++)
			v.push_back(make_pair(cia[j][i], j));
		sort(v.begin(), v.end());
		if((v[0].second == heh[a].second && v[1].second == heh[b].second)
		|| (v[1].second == heh[a].second && v[0].second == heh[b].second))
		{
			ll h = min(nope, (ll) wynik[i] - v[1].first);
			wynik[i] -= h;
			nope -= h;
		}
		else if((v[k - 1].second == heh[a].second && v[k - 2].second == heh[b].second)
		     || (v[k - 2].second == heh[a].second && v[k - 1].second == heh[b].second))
		{
			ll h = min(nope, (ll) v[k - 2].first - wynik[i]);
			wynik[i] += h;
			nope -= h;
		}
	}
}

void piecCztery()
{
	wynik.push_back(17);
	vector<pair<int, int> > v;
	for(int i = 1; i <= n; i++)
	{
		v.clear();
		for(int j = 1; j <= k; j++)
			v.push_back(make_pair(cia[j][i], j));
		sort(v.begin(), v.end());
		wynik.push_back(v[k - 3].first);
	}
	poprawiaj2(0, 2, 1);
	poprawiaj2(0, 1, 2);
	poprawiaj();
	wynik.erase(wynik.begin());
}

int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= k; i++)
		for(int j = 1; j <= n; j++)
			scanf("%d", &cia[i][j]);
	if(k == 2)
		dwa();
	else if(k == 3)
		trzy();
	else if(k == 4)
		piecCztery();
	else if(k == 5)
		piecCztery();
	else
		printf("(;_;)");
	#ifdef WYNIK
	ll suma[6] = {0LL, 0LL, 0LL, 0LL, 0LL, 0LL};
	#endif
	for(int i = 0; i < wynik.size(); i++)
	{
		#ifdef WYNIK
		for(int j = 1; j <= k; j++)
			suma[j] += bez(wynik[i] - cia[j][i + 1]);
		#else
		if(i) printf(" ");
		printf("%d", wynik[i]);
		#endif
	}
	#ifndef WYNIK
	printf("\n");
	#else
	ll wyn = 0LL;
	for(int i = 1; i <= k; i++)
		wyn = max(wyn, suma[i]);
	printf("%lld\n", wyn);
	#endif
	return 0;
}

void dwa()
{
	int ost = 0;
	for(int i = 1; i <= n; i++)
	{
		if((cia[1][i] + cia[2][i]) % 2 == 0)
		{
			wynik.push_back((cia[1][i] + cia[2][i]) / 2);
		}
		else if(cia[1][i] < cia[2][i])
		{
			if(ost)
				wynik.push_back(cia[1][i] + (cia[2][i] - cia[1][i]) / 2);
			else
				wynik.push_back(cia[1][i] + (cia[2][i] - cia[1][i] + 1) / 2);
			ost ^= 1;
		}
		else
		{
			if(ost)
				wynik.push_back(cia[2][i] + (cia[1][i] - cia[2][i] + 1) / 2);
			else
				wynik.push_back(cia[2][i] + (cia[1][i] - cia[2][i]) / 2);
			ost ^= 1;
		}
	}
}

void poprawiaj()
{
	ll wys[6] = {0LL, 0LL, 0LL, 0LL, 0LL, 0LL};
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= k; j++)
			wys[j] += bez(wynik[i] - cia[j][i]);
	}
	vector<pair<ll, int> > heh;
	for(int i = 1; i <= k; i++)
		heh.push_back(make_pair(wys[i], i));
	sort(heh.begin(), heh.end());
	reverse(heh.begin(), heh.end());
	ll nope = (heh[0].first - heh[1].first) / 2;
	nope = min(nope, wys[heh[0].second]);
	int j = heh[0].second;
	for(int i = 1; i <= n && nope > 0; i++)
	{
		if(cia[j][i] < wynik[i])
		{
			ll ile = wynik[i] - cia[j][i];
			ile = min(ile, nope);
			wynik[i] -= ile;
			nope -= ile;
		}
		else
		{
			ll ile = cia[j][i] - wynik[i];
			ile = min(ile, nope);
			wynik[i] += ile;
			nope -= ile;
		}
	}
}

void trzy()
{
	wynik.push_back(17);
	vector<int> v;
	for(int i = 1; i <= n; i++)
	{
		v.clear();
		for(int j = 1; j <= 3; j++)
			v.push_back(cia[j][i]);
		sort(v.begin(), v.end());
		wynik.push_back(v[1]);
	}
	poprawiaj();
	wynik.erase(wynik.begin());
}