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
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> PI;
typedef long long LL;
typedef double D;
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define R(I,N) for(int I=0;I<N;I++)
#define F(I,A,B) for(int I=A;I<B;I++)
#define FD(I,N) for(int I=N-1;I>=0;I--)
#define make(A) scanf("%d",&A)
#define MAX 100001
#define DB 0
#define db if(DB)printf
int n,k,kt,t[MAX][5];
int gl,p[5];
inline int daj_nr(){
	int wyn = 0;
	R(i,k){
		wyn*=5;
		wyn+=p[i];
	}
	return wyn;
}
bool qkol(int a,int b){
	return t[gl][a] < t[gl][b];
}
bool odw;
int mapp[4000];
LL tr[61][5];
int kol(int nr){
	gl = nr;
	R(i,k)p[i] = i;
	sort(p,p+k,qkol);
	odw = 0;
	
	R(i,k){
		if(p[i]==0){
			break;
		}
		if(p[i]==1){
			odw=1;
			R(i,k/2)swap(p[i],p[k-i-1]);
			break;
		}
	}
	int wyn = 0;
	return mapp[daj_nr()];
}

// Simplex z biblioteczki Marka Cygana
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

const double EPS = 1e-9;
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-16
  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 res; 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);
    }
    res.resize(k,0); REP(i,m) res[kt[i]]=b[i];
    return res;
  }
/*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
typedef long double T;
typedef vector<T> VT;
LL odp[61];
vector<int> nc;
void simplexuj(){
	int w = kt*(k+1) + 1;
	int h = 2*kt*k + k;
	vector<VT> A(h,VT(w));
	VT b(h),c(w),res;
	c[w-1] = -1;
	int ak = 0;
	R(i,k){
		F(j,(i+1)*kt,(i+2)*kt)
			A[ak][j] = 1;
		A[ak][w-1] = -1;
		ak++;
	}
	R(i,kt)R(j,k){
		A[ak][i] = 1;
		A[ak][i+(j+1)*kt] = -1;
		b[ak] = tr[i][j];
		ak++;
		
		A[ak][i] = -1;
		A[ak][i+(j+1)*kt] = -1;
		b[ak] = -tr[i][j];
		ak++;
	}
	res=Simplex::simplex(A,b,c);
	R(i,kt){
		odp[i] = res[i] + 0.05;
		//fprintf(stderr,"%.6Lf\n",res[i]);
		if(abs(odp[i] - res[i]) > 0.1)nc.PB(i);
	}
	//printf("najlepszy wektor: "); REP(i,/*SIZE(res)*/kt) printf("x%d = %.6Lf, ",i,res[i]);
	//printf("\n");
	T acc=0; REP(i,SIZE(res)) acc+=res[i]*c[i];
	//fprintf(stderr,"%.6Lf\n",-acc);

}

void calkowite(){
	int ncs = nc.size();
	LL naj = -1;
	int mb = -1;
	R(i,(1<<ncs)){
		R(j,ncs)if(i&(1<<j))odp[nc[j]]+=1;
		LL odl = 0;
		R(jj,k){
			LL wyn = 0;
			R(ii,kt)wyn += abs(tr[ii][jj] - odp[ii]);
			db("%lld\n",wyn);
			odl = max(odl,wyn);
		}
		db("\n");
		if(naj == -1 || odl < naj){
			naj = odl;
			mb = i;
		}
		R(j,ncs)if(i&(1<<j))odp[nc[j]]-=1;
	}
	R(j,ncs)if(mb&(1<<j))odp[nc[j]]+=1;
}
void wyswietl(){
	R(i,n){
		int pom = kol(i);
		if(odw)R(j,k)t[i][j] *= -1;
		int zm = -1;
		db("%lld\n",odp[pom]);
		R(jj,k){
			int j = p[jj];
			db("%lld %d\n",tr[pom][j],t[i][j]);
			if(tr[pom][j]<=odp[pom])
				zm = jj;
		}
		db("(%d)",zm);
		LL ile;
		if(zm == k-1)
			ile = 0;
		else
			ile = min(odp[pom] - tr[pom][p[zm]], LL(t[i][p[zm+1]] - t[i][p[zm]]));
		db("ile: %lld\n",ile);
		odp[pom] -= ile;
		db("wyn:");
		printf("%lld ",(odw?-1:1) * (t[i][p[zm]] + ile));
		db("\n");
	}
	puts("");
}

main(){
	make(n);make(k);
	R(i,k)p[i] = i;
	kt = 0;
	do{
		R(i,k){
			if(p[i]==0){
				mapp[daj_nr()] = kt;
				kt++;
			}
			if(p[i]==1){
				break;
			}
		}
	}while(next_permutation(p,p+k));
	//printf("%d\n",kt);
	R(j,k)R(i,n)make(t[i][j]);
	R(i,n){
		int pom = kol(i);
		//printf("%d\n",pom);
		if(odw){
			R(j,k)tr[pom][j] -= t[i][j];
		}else{
			R(j,k)tr[pom][j] += t[i][j];
		}
	}
	
	//zmienne maja być dodatnie
	R(i,kt){
		
		//R(j,k)printf("%lld ",tr[i][j]);puts("");
		LL mi = *min_element(tr[i],tr[i]+k);
		R(j,k)tr[i][j] -= mi;
		if(DB){R(j,k)printf("%lld ",tr[i][j]);puts("");}
	}
	simplexuj();
	calkowite();
	if(DB){R(i,kt)printf("%lld ",odp[i]);puts("");}
	wyswietl();
}