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

#define REP(i,n) for(int i=0; i<(n); ++i)

typedef long long LL;

struct In
{
  std::vector<std::vector<int>> A;

  void read()
  {
    int n,k; scanf("%d%d",&n,&k);
    A.resize(k); REP(i,k) A[i].resize(n);
    REP(i,k) REP(j,n) scanf("%d",&A[i][j]);
  }

  void gen()
  {

  }
};

void heura(const In &I, std::vector<int> &res)
{
  
  int k = I.A.size(), n = I.A[0].size();
  LL D[5]={};
  REP(i,n)
  {
    int B[5];
    REP(j,k) B[j] = I.A[j][i];
    std::sort(B,B+k); res[i] = B[k/2];
    REP(j,k) D[j] += std::abs(I.A[j][i]-res[i]);
  }
}

void go(const In &I, std::vector<int> &res)
{
  int k = I.A.size(), n = I.A[0].size();
  res.resize(n);
  if(k==2)
  {
    LL s = 0; REP(i,n) s += std::abs(I.A[0][i]-I.A[1][i]);
    LL r = 0; REP(i,n)
    {
      int d = I.A[0][i]-I.A[1][i], ad = std::abs(d);
      if(r+ad<s/2) res[i] = I.A[0][i];
      else if(r>s/2) res[i] = I.A[1][i];
      else res[i] = I.A[1][i] + (d>0?s/2-r:r-s/2);
      r += ad;
    }
  }
  else heura(I,res);
}

void brute(const In &I)
{

}

void test()
{

}

int main()
{
  test();
  In I; I.read();
  std::vector<int> res;
  go(I,res);
  for(int i : res) printf("%d ",i); puts("");

  return 0;
}