Sunday, 5 April 2015

UVA 10688 The Poor Giant

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int dp[505][505], k;

int func(int lft, int rgt)
{
    if(lft>=rgt) return 0;

    int &ret=dp[lft][rgt];
    if(ret!=-1) return ret;
    ret=INT_MAX;

    for(int i=lft;i<=rgt;i++)
        ret=min(ret, (k+i)*(rgt-lft+1) + func(lft, i-1) + func(i+1, rgt));
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int kk=1, tc, n, m;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n>>k;
        SET(dp);
        cout<<"Case "<<kk++<<": "<< func(1, n)<<"\n";
    }

    return 0;
}

No comments:

Post a Comment