timus online judge 1005. Stone Pile




Problem link




A solution in c++

#include<bits/stdc++.h>

using namespace std;

/// Typedef
typedef long long ll;

#define sc1(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld %lld",&a,&b)

#define pf1(a) printf("%lld\n",a)
#define pf2(a,b) printf("%lld %lld\n",a,b)

#define mx 1000010
#define mod 10000007
#define PI acos(-1.0)


int dr[] = {-2,-2,-1,-1,1,1,2,2};
int dc[] = {-1,1,-2,2,-2,2,-1,1};

ll arr[22];

ll binaryRep(ll num, ll pos)
{
    ll checkarray[pos];

    memset(checkarray, 0, sizeof(checkarray));
    ll i =0;
    while (num > 0){
        checkarray[i++] = num % 2;
        num /= 2;
    }

    ll ans1 = 0, ans2 = 0;
    for(ll j = pos-1; j >=0; j--){
        if(checkarray[j] == 1) ans1 += arr[j];
        else ans2 += arr[j];
    }
    return abs(ans1 - ans2);
}


int main() {

    ll tc, k, t = 1;

    // freopen("C:\\Users\\morol\\Desktop\\Clion\\input.txt", "r", stdin);

    sc1(tc);

    for(ll i = 0; i < tc; i++) {
        sc1(arr[i]);
    }

    ll totalnum = pow(2, tc);

    ll ans = INT_MAX;
    for(ll i = 0; i < totalnum; i++){
         ans = min(ans, binaryRep(i, tc));
    }

    pf1(ans);

    return 0;
}

Post a Comment

0 Comments