Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Friday, January 9, 2015

TRT-Treats for the Cows

Treats for the Cows

Given below code is for TRT spoj or trears for the cows spoj.


It an easy problem based on DP.If you want to read about this you can read from HERE.


#include <bits/stdc++.h>
using namespace std;
int dp[2001][2001];
int arr[2009];
int calculate(int start , int end , int year)
{
    if(start > end)
        return 0;
    if(dp[start][end] != -1)
        return dp[start][end];
    return (dp[start][end] = max(calculate(start + 1 , end , year+1) + year * arr[start] , calculate(start, end - 1 , year+1) + arr[end]*year));
}
int main()
{
    int n;
    scanf("%d",&n);
    memset(dp , -1 , sizeof dp);
    for(int i = 0 ; i < n ; i++)
        scanf("%d",&arr[i]);
    printf("%d\n",calculate(0 , n-1 , 1));
    return 0;
}