Tuesday, 5 May 2015

UVA 562 Dividing coins (UVALive 5583, Regionals 1996 >> Europe - Northwestern)

///     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 arr[105], dp[105][50005], n, tot;

int func(int pos, int taken)
{
    if(pos>=n) return abs(taken - (tot-taken));

    int &ret=dp[pos][taken];
    if(ret!=-1) return ret;

    ret=min(func(pos+1, taken), func(pos+1, taken+arr[pos]));
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        tot=0;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
            tot+=arr[i];
        }
        SET(dp);
        cout<<func(0, 0)<<"\n";
    }
return 0;
}

1 comment:

  1. " ios_base::sync_with_stdio(0);cin.tie(0); " - ভাইয়া এইখানে এই লাইনটা কেনো দিয়েছেন ???

    ReplyDelete