ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

why wa #1??
Posted by huangchen 14 Jun 2011 16:52
#include <stdio.h>
#include <math.h>

int n;// the number of stones
int w[21];//store the weight of stones
int total;//the sum of all of the stones
int ans = 100000000;

//now is the sum of first piles
//index is index of w
void fun(int now,int index)
{
    if (index == n)// the end
        if (ans > abs(total - 2 * now))
            ans = abs(total - 2 * now);
    for (int i = index; i < n; i ++)
        if (now + w[i] < (total >> 1))
            fun(now + w[i],i + 1);
}

int main(void)
{
#ifndef ONLINE_JUDGE
   freopen("input.txt", "rt", stdin);
#endif
    scanf("%d",&n);
    total = 0;
    for (int i = 0; i < n; i ++)
    {
        scanf("%d",&w[i]);
        total += w[i];
    }
    fun(0,0);
    printf("%d\n",ans);
    return 0;
}