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
#include <iostream>

using namespace std;

int n,k;
int * b;

bool pierwsze=true;

int sred(int a, int b)
{
    if(  ((10*(a+b)/2)) %10 == 0 )
        return ((a+b)/2);
    else
    {
        if(pierwsze)
        {
            pierwsze=false;
            if(a>b)
                return( ((a+b+1)/2) );
            else
                return( ((a+b)/2) );
        }
        else
        {
            pierwsze=true;
            if(a<b)
                return( ((a+b+1)/2) );
            else
                return( ((a+b)/2));
        }
    }
}

int bez(int a, int b)
{
    int wynik=a-b;
    if(wynik<0)
        return (-wynik);
    else
        return wynik;
}

int Odl(int * to)
{
    int odle=0;
    int tamto;
    for(int i=0;i<n;i++)
    {
        tamto=*to;
        odle+=bez(tamto,b[i]);
        if(i!=(n-1))
            to++;
    }
    return odle;
}

int main()
{
    cin>>n>>k;
    int ** ciagi;
    ciagi= new int * [k];
    for(int x=0;x<k;x++)
        ciagi[x]=new int [n];
    int * sumy;
    int * bs;
    sumy= new int [k];
    b= new int [n];
    bs= new int [n];

    int * maxi=new int [n];
    int * mini=new int [n];
    for(int h=0;h<n;h++)
    {
        maxi[h]=-1000000004;
        mini[h]=1000000004;
    }

    if(k==2)
    {
        for(int i=0;i<k;i++)
            for(int j=0;j<n;j++)
                cin>>ciagi[i][j];

        for(int v=0;v<n;v++)
        {
            if(v<n)//-2)
                {
                    mini[v]=sred(ciagi[0][v],ciagi[1][v]);
                    maxi[v]=mini[v];
                }
                else
                {
                    mini[v]=sred(ciagi[0][v],ciagi[1][v]);
                    maxi[v]=sred(ciagi[0][v],ciagi[1][v]);
                }
        }
    }
    else
        for(int i=0;i<k;i++)
            for(int j=0;j<n;j++)
            {
                cin>>ciagi[i][j];
                if(ciagi[i][j]>maxi[j])
                    maxi[j]=ciagi[i][j];
                if(ciagi[i][j]<mini[j])
                    mini[j]=ciagi[i][j];
            }

    int maxodl=0;
    int minmaxodl=1000000004;

    if(k==2)
    {
            for(int p=0;p<n;p++)
                bs[p]=sred(ciagi[0][p],ciagi[1][p]);
    }
    else
    {
    for(int c=0;c<n;c++)
        b[c]=mini[c];
    if(n-1>=0)
        b[n-1]=mini[n-1]-1;
    for(int f=1;f>=1;f++)
        {
            b[n-1]++;
            for(int e=1;e<n;e++)
                if(b[n-e]>maxi[n-e])
                {
                    b[n-e-1]++;
                    b[n-e]=mini[n-e];
                }
            if(b[0]==maxi[0]+1)
                break;

            for(int u=0;u<k;u++)
                sumy[u]=Odl(ciagi[u]);
            maxodl=0;
            for(int t=0;t<k;t++)
                if(sumy[t]>maxodl)
                    maxodl=sumy[t];

            if(maxodl<minmaxodl)
            {
                for(int r=0;r<n;r++)
                    bs[r]=b[r];
                minmaxodl=maxodl;
            }
        }
    }
    for(int y=0;y<n;y++)
        cout<<bs[y]<<" ";
    for(int d=0;d<k;d++)
        delete [] ciagi[d];
    delete [] ciagi;
    delete [] sumy;
    delete [] b;
    delete [] bs;
    delete [] mini;
    delete [] maxi;

    return 0;
}