-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSUMTRIAN.cpp
More file actions
56 lines (51 loc) · 870 Bytes
/
SUMTRIAN.cpp
File metadata and controls
56 lines (51 loc) · 870 Bytes
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
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
inline int scan()
{
register int n=0;
register char c;
c=getchar_unlocked();
while( c<'0' || c>'9')
c=getchar_unlocked();
while( c>='0' && c<='9' )
{
n=n*10+c-'0';
c=getchar_unlocked();
}
return n;
}
int sum[100];
int arr[100][100]; // the array to store the numbers
int n;
int k;
inline int max(int i,int j)
{
if( i< j ) return j;
else return i;
}
int main()
{
register int i,j;
n=scan();
while(n--)
{
k=scan();
for(i=1;i<=k;i++)
for(j=0;j<i;j++)
arr[i-1][j]=scan();
//use the dp approach
for(i=0;i<k;i++)
sum[i]=arr[k-1][i];
for(i=k-2;i>=0;i--) // for all the rows
{
for(j=0;j<=i;j++)
{
sum[j]=max(sum[j],sum[j+1])+arr[i][j];
}
}
cout<<sum[0]<<endl;
}
return 0;
}