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

AC For 1005,ask for quicker solution
Posted by snowfly 13 Jul 2009 15:44
er......It is much easier to use violence than DP
data so weak......
-----------------------------------------------------------
ask for quicker solution
-----------------------------------------------------------
#include<iostream>
#include<climits>
using namespace std;
int _ans,w[20],n;
int abs(int x){
    if (x<0)
        x=-x;
    return x;
}
void dfs_try(int depth,int a,int b){
    if (depth==n){
        if (abs(a-b)<_ans)
            _ans=abs(a-b);
        return ;
    }
    dfs_try(depth+1,a+w[depth],b);
    dfs_try(depth+1,a,b+w[depth]);
}
int main (void){
    cin>>n;
    for (int i=0;i<n;++i)
        cin>>w[i];
    _ans=INT_MAX;
    dfs_try(0,0,0);
    cout<<_ans<<endl;
    return 0;
}