UVa 11850 – Alaska

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2950

Solution Idea:

The tricky part in this problem statement is this –

” Can Brenda drive her car from Dawson City to Delta Junction and back? ”

So Brenda need to reach her destination and return his start point. So just take input all the points and sort then in acceding order and check that the distance between two consecutive points is less than or equal to 200 or not. If it’s not then the ans will be impossible and if the 2*(distance between the last point and and destination) >200 the ans will be impossible. Otherwise the ans is possible.


/*
     If opportunity doesn't knock, build a door.

            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    Success is how high you bounce when you hit bottom.
*/


#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/



int main()
{
//    freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int n;
    while(cin>>n && n)
    {
        vector<int>v;

        loop(i,n)
        {
            int a;
            cin>>a;
            v.pb(a);
        }

        sort(all(v));

        bool test=0;

        REP(i,1,n)
        {
            if(v[i]-v[i-1]>200)
            {
                cout<<"IMPOSSIBLE"<<endl;
                test=1;
                break;
            }
        }

        if(test==1) continue;

        if((1422-v[n-1])*2>200) test=1;

        if(test==0)
            cout<<"POSSIBLE"<<endl;
        else
            cout<<"IMPOSSIBLE"<<endl;

    }
    return 0;
}

Advertisements

USACO: Ordered Fractions

Problem Link : http://train.usaco.org/usacoprob2?a=qBACVBxoOsW&S=frac1


/*
PROG: frac1
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int n;

map<double,bool>mp;
vector< pair<double, pii > > v;

int main()
{
    freopen("frac1.in","r",stdin);
    freopen("frac1.out","w",stdout);
    cin>>n;
    mp[0/1.0]=1;
    v.pb(MP(0/1.0,MP(0,1)));

    REP(i,1,n) REP(j,1,n)

    {
        double a=i*1.0;
        double b=j*1.0;
        if(a/b<=1.0)
        {
            if(!mp[a/b])
            {
                mp[a/b]=1;
                v.pb(MP(a/b,MP(i,j)));
            }
        }
    }

    sort(all(v));
    loop(i,SZ(v))
    cout<<v[i].ss.ff<<"/"<<v[i].ss.ss<<endl;
    return 0;
}

USACO: The Castle

Problem Link : http://train.usaco.org/usacoprob2?a=qBACVBxoOsW&S=castle


/*
PROG: castle
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
/*------------------------------------------------*/

int graph[52][52];
int dp[2505];
int visited[52][52];

int r,c,s,extend=0;

int dfs(int i, int j, int x)
{
//    if(visited[i][j]) return 0;
    visited[i][j]=x;
    int d=0;
    if(!check(graph[i][j],0) && !visited[i][j-1]) d+=1+dfs(i,j-1,x);
    if(!check(graph[i][j],2) && !visited[i][j+1]) d+=1+dfs(i,j+1,x);
    if(!check(graph[i][j],1) && !visited[i-1][j]) d+=1+dfs(i-1,j,x);
    if(!check(graph[i][j],3) && !visited[i+1][j]) d+=1+dfs(i+1,j,x);
    return d;
}

int n,m;

void func(int i, int j,int x)
{
    int t=visited[i][j];
    int d=0;
    if(check(graph[i][j],0) && t!=visited[i][j-1] && j-1>=1)
    {
        d=dp[t]+dp[visited[i][j-1]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=0;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=0;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=0;
                }
            }
        }
    }

    if(check(graph[i][j],2) && t!=visited[i][j+1] && j+1<=m)
    {
        d=dp[t]+dp[visited[i][j+1]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=2;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=2;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=2;
                }
                else if(i==r)
                {
                    if(s!=1)
                    {
                        r=i,j=c;
                        s=2;
                    }
                }
            }
        }
    }

    // if(check(graph[i][j],2) && !visited[i][j+1]) d+=1+dfs(i,j+1,x);
    //if(check(graph[i][j],1) && !visited[i-1][j]) d+=1+dfs(i-1,j,x);
    if(check(graph[i][j],1) && t!=visited[i-1][j] && i-1>=1)
    {
        d=dp[t]+dp[visited[i-1][j]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=1;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=1;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=1;
                }
                else if(i==r)
                {
                    r=i,j=c;
                    s=1;
                }
            }
        }
    }
    //if(check(graph[i][j],3) && !visited[i+1][j]) d+=1+dfs(i+1,j,x);

    if(check(graph[i][j],3) && t!=visited[i+1][j] && i+1<=n)
    {
        d=dp[t]+dp[visited[i+1][j]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=3;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=3;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=3;
                }
            }
        }
    }
}

int main()
{
    freopen("castle.in","r",stdin);
    freopen("castle.out","w",stdout);
    cin>>n>>m;
    swap(n,m);
    REP(i,1,n) REP(j,1,m) cin>>graph[i][j];

    int k=0;
    int ans=-1;
    REP(i,1,n) REP(j,1,m)
    {
        if(!visited[i][j])
        {
            k++;
            dp[k]=dfs(i,j,k)+1;
            ans=max(ans,dp[k]);
        }
    }

    cout<<k<<endl<<ans<<endl;
    REP(i,1,n) REP(j,1,m)
    {
        func(i,j,visited[i][j]);
//        cout<<i<<" "<<j<<" "<<extend<<" "<<s<<endl;
    }
    cout<<extend<<endl;
    cout<<r<<" "<<c;
    if(s==2)pf(" E\n");
    else
        pf(" N\n");
    return 0;
}

USACO: Superprime Rib

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=sprime


/*
PROG: sprime
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

vector<ll>v;
int n;

bool isprime(ll n)
{
    if(n==2) return 1;
    if(!(n & 1)) return 0;

    for(int i=3; i*i<=n; i+=2)
        if(n%i==0) return 0;
    return 1;
}

void dfs(ll u,int m)
{
    if(!isprime(u)) return;
    if(m==n)
    {
        v.pb(u);
        return;
    }
    for(int i=1; i<=9; i+=2)
    {
        int x=u*10+i;
        dfs(x,m+1);
    }
}

int main()
{
    freopen("sprime.in","r",stdin);
    freopen("sprime.out","w",stdout);
    cin>>n;
    for(int i=2; i<=9; i++)
        dfs(i,1);
    sort(all(v));
    loop(i,SZ(v)) cout<<v[i]<<endl;
    return 0;
}

USACO : Prime Palindromes

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=pprime


/*
PROG: pprime
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/


bool isPrime(ll n)
{
    if(!(n & 1))
        return false;
    else
    {
        int root= sqrt(n);
        for(int i=3; i<=root+1; i+=2)
            if(n%i==0) return false;
    }
    return true;
}


vector<ll>v;

int main()
{
    freopen("pprime.in","r",stdin);
    freopen("pprime.out","w",stdout);

    ll a,b;
    cin>>a>>b;

    bool test=1;

    for(int i=0; i<10 && test; i++)
        for(int j=0; j<10 && test; j++)
            for(int k=0; k<10 && test; k++)
                for(int l=0; l<10 && test; l++)
                {
                    ll palin=10*l+l;
                    if(k)
                    {
                        palin=1000*k+k+palin*10;
                    }
                    if(j)
                    {
                        if(k==0) palin=100000*j+j+palin*100;
                        else
                            palin=100000*j+j+palin*10;
                    }
                    if(i)
                    {
                        if(k==0 && j==0) palin=10000000*i+i+palin*1000;
                        else if(k!=0 && j==0) palin=10000000*i+i+palin*100;
                        else
                            palin=10000000*i+i+palin*10;
                    }
                    if(palin>b)
                    {
                        test=0;
                        break;
                    }
                    if(palin>=a && isPrime(palin))
                        v.pb(palin);
                }

    test=1;

    for(int i=0; i<10 && test; i++)
        for(int j=0; j<10 && test; j++)
            for(int k=0; k<10 && test; k++)
                for(int l=0; l<10 && test; l++)
                {
                    ll palin=l;
                    if(k)
                    {
                        palin=100*k+k+palin*10;
                    }
                    if(j)
                    {
                        if(k==0) palin=10000*j+j+palin*100;
                        else
                            palin=10000*j+j+palin*10;
                    }
                    if(i)
                    {
                        if(k==0 && j==0) palin=1000000*i+i+palin*1000;
                        else if(k!=0 && j==0) palin=1000000*i+i+palin*100;
                        else
                            palin=1000000*i+i+palin*10;
                    }
                    if(palin>b)
                    {
                        test=0;
                        break;
                    }
                    if(palin>=a && isPrime(palin))
                        v.pb(palin);
                }
    sort(all(v));
    loop(i,SZ(v)) cout<<v[i]<<endl;
    return 0;
}


USACO: Number Triangles

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=numtri


/*
PROG: numtri
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int ara[1005][1005];
int dp[1005][1005];
int r;

int func(int i,int j)
{
    if(i>r || j>r) return 0;

    int &ret=dp[i][j];
    if(ret!=-1) return ret;
    ret=0;
//    return ret=max(ara[i][j]+func(i+1,j),ara[i][j]+func(i+1,j+1));
    return ret=ara[i][j]+max(func(i+1,j),func(i+1,j+1));
}

int main()
{
    CIN;
    freopen("numtri.in","r",stdin);
    freopen("numtri.out","w",stdout);

    cin>>r;
    REP(i,1,r) REP(j,1,i) cin>>ara[i][j];
    ms(dp,-1);
    cout<<func(1,1)<<endl;
    return 0;
}


USACO: Mother’s Milk

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=milk3


/*
PROG: milk3
LANG: C++
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

struct data
{
    int a,b,c;
    data()
    {
        ;
    }
    data(int x, int y, int z)
    {
        a=x,b=y,c=z;
    }
};

int A,B,C;

bool ans[30];

bool visited[30][30][30];
int first=100;

void BFS(data x)
{
    map<data,bool>mp;
    queue<data>Q;
    visited[x.a][x.b][x.c]=1;
    Q.push(x);
    while(!Q.empty())
    {
        data u=Q.front();
        Q.pop();
        if(u.a==0) ans[u.c]=1,first=min(first,u.c);
        int bb=B-u.b,aa=A-u.a,cc=C-u.c;
        if(u.c)
        {
            data c2b=u,c2a=u;
            if(bb>=u.c)
            {
                c2b.b+=u.c;
                c2b.c=0;
            }
            else if(bb<u.c)
            {
                c2b.b=B;
                c2b.c-=bb;
            }

            if(aa>=u.c)
            {
                c2a.a+=u.c;
                c2a.c=0;
            }
            else if(aa<u.c)
            {
                c2a.a=A;
                c2a.c-=aa;
            }
            if(!visited[c2b.a][c2b.b][c2b.c])
            {
                Q.push(c2b);
                visited[c2b.a][c2b.b][c2b.c]=1;
            }
            if(!visited[c2a.a][c2a.b][c2a.c])
            {
                Q.push(c2a);
                visited[c2b.a][c2b.b][c2b.c]=1;
            }
        }

        if(u.b)
        {
            data b2a=u,b2c=u;
            if(aa>=u.b)
            {
                b2a.a+=u.b;
                b2a.b=0;
            }
            else
            {
                b2a.a=A;
                b2a.b-=aa;
            }
            if(cc>=u.b)
            {
                b2c.c+=u.b;
                b2c.b=0;
            }
            else
            {
                b2c.c=C;
                b2c.b-=cc;
            }
            if(!visited[b2a.a][b2a.b][b2a.c])
            {
                visited[b2a.a][b2a.b][b2a.c]=1;
                Q.push(b2a);
            }
            if(!visited[b2c.a][b2c.b][b2c.c])
            {
                visited[b2c.a][b2c.b][b2c.c]=1;
                Q.push(b2c);
            }
        }

        if(u.a)
        {
            data a2b=u,a2c=u;
            if(bb>=u.a)
            {
                a2b.b+=u.a;
                a2b.a=0;
            }
            else
            {
                a2b.b=B;
                a2b.a-=bb;
            }
            if(cc>=u.a)
            {
                a2c.c+=u.a;
                a2c.a=0;
            }
            else
            {
                a2c.c=C;
                a2c.c-=cc;
            }
            if(!visited[a2c.a][a2c.b][a2c.c])
            {
                visited[a2c.a][a2c.b][a2c.c]=1;
                Q.push(a2c);
            }
            if(!visited[a2b.a][a2b.b][a2b.c])
            {
                visited[a2b.a][a2b.b][a2b.c]=1;
                Q.push(a2b);
            }
        }

    }
}

int main()
{
    freopen("milk3.in","r",stdin);
    freopen("milk3.out","w",stdout);
    cin>>A>>B>>C;
    BFS(data(0,0,C));

    cout<<first;
    for(int i=first+1; i<=C; i++) if(ans[i]) cout<<" "<<i;
    cout<<endl;
    return 0;
}