双向dp。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define PB push_back
#define INS insert
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define nMax 1010
#define bug puts("Fuck");
#define LL long long
#define pb push_back
int n,m;
int dp[nMax][nMax],fa[nMax][nMax];
LL a[nMax][nMax];
vector<int> ans;
void out(int x,int y){
/*if(x==1) {
printf("%d",y);
return ;
}
if(fa[x][y]==0) out(x-1,y);
else out(x,y+fa[x][y]);
printf(" %d",y);*/
ans.clear();
while(1){
ans.pb(y);
if(x==1) break;
if(fa[x][y] == 0) x--;
else y+=fa[x][y];
}
DOR(i,ans.size()-1,0) {
printf("%d",ans[i]);
if(i>0) printf(" ");
}puts("");
return ;
}
void sovle(){
FOR(i,1,n){
FOR(j,1,m) dp[i][j] = dp[i-1][j]+a[i][j] ,fa[i][j] = 0;
FOR(j,2,m) if(dp[i][j] > dp[i][j-1] + a[i][j] ){
dp[i][j] = dp[i][j-1] + a[i][j];
fa[i][j] = -1;
}
DOR(j,m-1,1) if(dp[i][j] > dp[i][j+1] + a[i][j] ){
dp[i][j] = dp[i][j+1] + a[i][j] ;
fa[i][j] = 1;
}
}
int tot = 1;
FOR(i,1,m) {
if(dp[n][i] < dp[n][tot]) tot = i;
}
out(n,tot);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
// freopen("output.txt","w",stdout);
while(~scanf("%d%d",&n,&m)){
FOR(i,1,n) FOR(j,1,m) scanf("%lld",&a[i][j]);
sovle();
}
return 0;
}
