#include <fstream>
using namespace std;
ifstream fin ("date.in");
ofstream fout ("date.out");
void citire(int &n, int a[100][100])
{
int i,j;
fin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fin>>a[i][j];
}
void drum(int n, int a[100][100], int s[100][100], int i, int j)
{
if(i>0)
{
if(s[i][j]-s[i-1][j+1]==a[i][j]) drum (n,a,s,i-1,j+1);
else if(s[i][j]-s[i-1][j-1]==a[i][j]) drum (n,a,s,i-1,j-1);
else drum (n,a,s,i-1,j);
fout<<a[i][j]<<" ";
}
}
int main()
{
int i,j,a[100][100]={0},n,s[100][100]={0},maxx;
citire(n,a);
for(j=1;j<=n;j++) s[1][j]=a[1][j];
for(i=2;i<=n;i++)
for(j=1;j<=n;j++)
if(s[i-1][j]<=s[i-1][j-1] && s[i-1][j+1]<=s[i-1][j-1]) s[i][j]=a[i][j]+s[i-1][j-1];
else if(s[i-1][j]>=s[i-1][j-1] && s[i-1][j]>=s[i-1][j+1]) s[i][j]=a[i][j]+s[i-1][j];
else s[i][j]=a[i][j]+s[i-1][j+1];
maxx=0;
int mj;
for(j=1;j<=n;j++) if(s[n][j]>maxx) { maxx=s[n][j]; mj=j;}
fout<<maxx<<endl;
drum(n,a,s,n,mj);
fin.close();
fout.close();
return 0;
}
|