ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1005. Куча камней

1005 - stone pile
Послано gogreen 25 мар 2013 14:58
hello,
I wrote the code for this problem in python. I sure my logic is right, but do not know why i get a wrong answer. I have pasted my code below. If someone could help me find where the bug is .. it would be of great use

#timus 1005
import sys,math
class Timus1005:
    def __init__(self):
        pass

    def minimumDiffrence(self,weights):
        weights.sort()
        if (len(weights) == 1):
            return weights[0]

        if (len(weights) == 2):
            return (weights[1] - weights[0])

        weights.reverse()

        pile1 = 0
        pile2 = 0

        for stone in weights:
            added = False
            if (pile1 < pile2) and added == False:
                pile1 = pile1 + stone
                added = True

            if (pile1 > pile2) and added == False:
                pile2 = pile2 + stone
                added = True

            if (pile1 == pile2) and added == False:
                pile1 = pile1 + stone
                added = True

        return int(math.fabs(pile1 - pile2))

if __name__ == "__main__":
    w = []
    for line in sys.stdin:
        for word in line.split(' '):
            w.append(int(word))

    p = Timus1005()
    print p.minimumDiffrence(w[1:])