View Code of Problem 65

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<set>
using namespace std;
int t,n;
int a[1000009];
int dfs(int index,int high,int nowindex)
{
    if(index>n*(n+1)/2)return 0;
    int l=dfs((high+1)*high/2+nowindex,high+1,nowindex);
    int r=dfs((high+1)*high/2+nowindex+1,high+1,nowindex+1);
    return max(l,r)+a[index];
}
int main(){

    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n*(n+1)/2;i++)
            cin>>a[i];
        int maxnum=dfs(1,1,1);
        cout<<maxnum<<endl;
    }
    return 0;
}
/*
1
2 3
4 5 6
7 8 9 10

*/

Double click to view unformatted code.


Back to problem 65