-
Notifications
You must be signed in to change notification settings - Fork 0
/
_160_a_twins.go
66 lines (52 loc) · 3.01 KB
/
_160_a_twins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* https://codeforces.com/problemset/problem/160/A
A. Twins
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like.
Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, ..., an. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally.
As you woke up, you found Mom's coins and read her note. "But why split the money equally?" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the coins' values. All numbers are separated with spaces.
Output
In the single line print the single number — the minimum needed number of coins.
Examples
inputCopy
2
3 3
outputCopy
2
inputCopy
3
2 1 2
outputCopy
2
Note
In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins' sum.
In the second sample one coin isn't enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.
*/
package main
import (
"fmt"
"sort"
)
func main() {
var num_coins, coin_total, tabulating int
fmt.Scanln(&num_coins)
coin_values := make([]int, num_coins)
for i := 0; i < num_coins; i++ {
fmt.Scan(&coin_values[i])
}
for _, coin_value := range coin_values {
coin_total += coin_value
}
sort.Sort(sort.Reverse(sort.IntSlice(coin_values)))
for keepers, coin := range coin_values {
tabulating += coin
if tabulating > coin_total/2 {
fmt.Println(keepers + 1)
return
}
}
}