AlgorithmIdea: simple DP, classic Tree Tower Model, and reverse recurrence.
CodeAs follows:
// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <queue> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t;} template <CL Ass T> string tostring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B) ;}# define local // template end (General Part) # define maxn 105int cost [maxn] [maxn]; int DP [maxn] [maxn]; int N; void Init () {memset (DP, 0, sizeof (DP);} int main () {# ifdef local // freopen ("shuju.txt", "r", stdin); # endifint CAS; CIN> CAS; For (int K = 0; k <CAS; k ++) {CIN> N; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= I; j ++) {CIN> cost [I] [J] ;}} Init (); For (INT I = N; I >=1; I --) {for (Int J = 1; j <= I; j ++) {DP [I] [J] = max (DP [I + 1] [J], DP [I + 1] [J + 1]) + cost [I] [J] ;}} cout <DP [1] [1] <Endl ;} return 0 ;}