/**
* Dynamic Programming - Triangle Problem
* 2
* 3 5
* 6 3 4
* 5 6 1 4
*
* The new Triangle
* C[i,j] = max(T[i,j]+C[i+1,j], T[i,j] + C[i+1,j+1])
where i apartine [1,n-1]
* 17
* 15 14
* 12 9 8
* 5 6 1 4
*/
#include <bits/stdc++.h>
#define FIN "triangle.in"
#define FOUT "triangle.out"
#define SIZE 101
using namespace std;
int i,n,Tri[ SIZE ][ SIZE ],
C[ SIZE ][ SIZE ],
Road[ SIZE ][ SIZE ];
ifstream fin(FIN);
ofstream fout(FOUT);
int main( int argc, char const *argv[] ) {
cin>>n;
for( int i = 1; i <= n; ++i )
for(int j = 1; j <= i; ++j)
cin>>Tri[i][j];
for( int i = 1; i <= n; ++i ) {
for(int j = 1; j <= i; ++j) {
cout<<Tri[i][j]<<" ";
}
cout<<endl;
}
for(int j = 1; j <= n; ++j) C[n][j] = Tri[n][j];
for(int i = n - 1; i >= 1; i--) {
for(int j = 1; j <= i; ++j) {
if(C[i+1][j] > C[i+1][j+1]) {
C[i][j] = Tri[i][j] + C[i+1][j];
Road[i][j] = j;
} else {
C[i][j] = Tri[i][j] + C[i+1][j+1];
Road[i][j] = j+1;
}
}
}
cout<<C[1][1]<<"\n";
int i,j;
i = j = 1;
while(i<=n) {
cout<<Tri[i][j]<<" ";
j = Road[i][j];
i++;
}
return 0;
}
LyoqCiAqICBEeW5hbWljIFByb2dyYW1taW5nIC0gVHJpYW5nbGUgUHJvYmxlbQogKiAgMgogKiAgMyA1CiAqICA2IDMgNAogKiAgNSA2IDEgNAogKgogKiAgVGhlIG5ldyBUcmlhbmdsZQogKiAgQ1tpLGpdID0gbWF4KFRbaSxqXStDW2krMSxqXSwgVFtpLGpdICsgQ1tpKzEsaisxXSkKICAgIHdoZXJlIGkgYXBhcnRpbmUgWzEsbi0xXQogKiAgMTcKICogIDE1IDE0CiAqICAxMiA5IDgKICogICA1IDYgMSA0CiAqLwojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBGSU4gInRyaWFuZ2xlLmluIgojZGVmaW5lIEZPVVQgInRyaWFuZ2xlLm91dCIKI2RlZmluZSBTSVpFIDEwMQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBpLG4sVHJpWyBTSVpFIF1bIFNJWkUgXSwKICAgICAgQ1sgU0laRSBdWyBTSVpFIF0sCiAgICAgIFJvYWRbIFNJWkUgXVsgU0laRSBdOwoKaWZzdHJlYW0gZmluKEZJTik7Cm9mc3RyZWFtIGZvdXQoRk9VVCk7CgppbnQgbWFpbiggaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSApIHsKCiAgICBjaW4+Pm47CgogICAgZm9yKCBpbnQgaSA9IDE7IGkgPD0gbjsgKytpICkKCiAgICAgICAgZm9yKGludCBqID0gMTsgaiA8PSBpOyArK2opCgogICAgICAgICAgICBjaW4+PlRyaVtpXVtqXTsKCiAgICBmb3IoIGludCBpID0gMTsgaSA8PSBuOyArK2kgKSB7CgogICAgICAgIGZvcihpbnQgaiA9IDE7IGogPD0gaTsgKytqKSB7CgogICAgICAgICAgICAgICAgICAgIGNvdXQ8PFRyaVtpXVtqXTw8IiAiOwogICAgICAgIH0KICAgICAgICBjb3V0PDxlbmRsOwogICAgfQoKICAgIGZvcihpbnQgaiA9IDE7IGogPD0gbjsgKytqKSBDW25dW2pdID0gVHJpW25dW2pdOwoKICAgIGZvcihpbnQgaSA9IG4gLSAxOyBpID49IDE7IGktLSkgewoKICAgICAgICBmb3IoaW50IGogPSAxOyBqIDw9IGk7ICsraikgewoKICAgICAgICAgICAgaWYoQ1tpKzFdW2pdID4gQ1tpKzFdW2orMV0pIHsKCiAgICAgICAgICAgICAgICBDW2ldW2pdID0gVHJpW2ldW2pdICsgQ1tpKzFdW2pdOwoKICAgICAgICAgICAgICAgIFJvYWRbaV1bal0gPSBqOwoKICAgICAgICAgICAgfSBlbHNlIHsKCiAgICAgICAgICAgICAgICBDW2ldW2pdID0gVHJpW2ldW2pdICsgQ1tpKzFdW2orMV07CgogICAgICAgICAgICAgICAgUm9hZFtpXVtqXSA9IGorMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBjb3V0PDxDWzFdWzFdPDwiXG4iOwoKICAgIGludCBpLGo7CiAgICBpID0gaiA9IDE7CiAgICB3aGlsZShpPD1uKSB7CiAgICAgIGNvdXQ8PFRyaVtpXVtqXTw8IiAiOwogICAgICBqID0gUm9hZFtpXVtqXTsKICAgICAgaSsrOwogICAgfQoKICAgIHJldHVybiAwOwp9