시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 142 | 49 | 41 | 31.538% |
You and your friends have just returned from a beautiful vacation in the mountains of the Netherlands. When on vacation, it’s annoying to split the bill on every expense every time, so you just kept all the receipts from the vacation, and wrote down who paid how much for who. Now, it is time to settle the bill.
You could each take all the receipts showing that someone paid something for you, and then pay that person back. But then you would need a lot of transactions, and you want to keep up the lazy spirit from your trip. In the end, it does not matter who transfers money to whom; as long as in the end, everyone’s balance is 0.
Can you figure out the least number of transactions needed to settle the score? Assume everyone has enough spare cash to transfer an arbitrary amount of money to another person.
Input consists of
Output a single line containing a single integer, the least number of transactions necessary to settle all bills.
4 2 0 1 1 2 3 1
2
5 5 0 1 3 1 2 3 2 3 3 3 4 3 4 0 3
0
5 4 0 1 1 0 2 1 0 3 1 0 4 1
4