#include<bits/stdc++.h>
#define inf 1<<28
using namespace std;
typedef long long ll;
ll arr[500][500];
ll dp[500][500];
ll n;
ll call(ll i,ll j)
{
ll k,l,m;
if(i>=0&&i<500&&j>=0&&j<500&&arr[i][j]!=-1)
{
if(dp[i][j]!=-1)
return dp[i][j];
ll ret=-inf;
ret=max(ret,call(i+1,j-1)+arr[i][j]);
ret=max(ret,call(i+1,j)+arr[i][j]);
return dp[i][j]=ret;
}
else return 0;
}
int main()
{
ll i,j,k,l,m,test;
cin>>test;
for(k=1;k<=test;k++)
{
memset(arr,-1,sizeof(arr));
memset(dp,-1,sizeof(dp));
cin>>n;
for(i=0,l=0;i<n;i++,l++)
{
for(j=n-l-1;j<n;j++)
{
cin>>arr[i][j];
}
}
for(i=n,l=1;i<2*n-1;i++,l++)
{
for(j=0;j<n-l;j++)
{
cin>>arr[i][j];
}
}
cout<<"Case "<<k<<": "<<call(0,n-1)<<endl;
}
return 0;
}
#define inf 1<<28
using namespace std;
typedef long long ll;
ll arr[500][500];
ll dp[500][500];
ll n;
ll call(ll i,ll j)
{
ll k,l,m;
if(i>=0&&i<500&&j>=0&&j<500&&arr[i][j]!=-1)
{
if(dp[i][j]!=-1)
return dp[i][j];
ll ret=-inf;
ret=max(ret,call(i+1,j-1)+arr[i][j]);
ret=max(ret,call(i+1,j)+arr[i][j]);
return dp[i][j]=ret;
}
else return 0;
}
int main()
{
ll i,j,k,l,m,test;
cin>>test;
for(k=1;k<=test;k++)
{
memset(arr,-1,sizeof(arr));
memset(dp,-1,sizeof(dp));
cin>>n;
for(i=0,l=0;i<n;i++,l++)
{
for(j=n-l-1;j<n;j++)
{
cin>>arr[i][j];
}
}
for(i=n,l=1;i<2*n-1;i++,l++)
{
for(j=0;j<n-l;j++)
{
cin>>arr[i][j];
}
}
cout<<"Case "<<k<<": "<<call(0,n-1)<<endl;
}
return 0;
}
No comments:
Post a Comment