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 doesn't it work?
Posted by Khelleos 6 Feb 2012 17:38
//took wa5, anybody know test5?

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{

#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int N, W[20] = {0};
    int s = 0, r = 0;

    cin >> N;
    for(int i = 0; i < N; i++)
    {
        cin >> W[i];
        s += W[i];
    }
    sort(W, W + N);
    r = W[N-1]; s -= r;
    for(int i = N - 2; i >= 0; i--)
    {
        if ((r - W[i] >= 0) || ((s + r - 2 * W[i]) >= 0))
            r -= W[i];
        else
             r += W[i];
        s -= W[i];
    }

    cout << r;

    return 0;
}