1070F.md (2957B)
1 Source: https://codeforces.com/problemset/problem/1070/F 2 3 F. Debate 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Elections in Berland are coming. There are only two candidates — Alice and Bob. 22 23 The main Berland TV channel plans to show political debates. There are $n$ people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views: 24 25 * supporting none of candidates (this kind is denoted as "00"), 26 * supporting Alice but not Bob (this kind is denoted as "10"), 27 * supporting Bob but not Alice (this kind is denoted as "01"), 28 * supporting both candidates (this kind is denoted as "11"). 29 30 The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions: 31 32 * at least half of spectators support Alice (i.e. $2 \cdot a \ge m$, where $a$ is number of spectators supporting Alice and $m$ is the total number of spectators), 33 * at least half of spectators support Bob (i.e. $2 \cdot b \ge m$, where $b$ is number of spectators supporting Bob and $m$ is the total number of spectators), 34 * the total influence of spectators is maximal possible. 35 36 Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible. 37 38 Input 39 40 The first line contains integer $n$ ($1 \le n \le 4\cdot10^5$) — the number of people who want to take part in the debate as a spectator. 41 42 These people are described on the next $n$ lines. Each line describes a single person and contains the string $s_i$ and integer $a_i$ separated by space ($1 \le a_i \le 5000$), where $s_i$ denotes person's political views (possible values — "00", "10", "01", "11") and $a_i$ — the influence of the $i$-th person. 43 44 Output 45 46 Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead. 47 48 Examples 49 50 Input 51 52 ``` 53 6 54 11 6 55 10 4 56 01 3 57 00 3 58 00 7 59 00 9 60 ``` 61 62 Output 63 64 ``` 65 22 66 ``` 67 68 Input 69 70 ``` 71 5 72 11 1 73 01 1 74 00 100 75 10 1 76 01 1 77 ``` 78 79 Output 80 81 ``` 82 103 83 ``` 84 85 Input 86 87 ``` 88 6 89 11 19 90 10 22 91 00 18 92 00 29 93 11 29 94 10 28 95 ``` 96 97 Output 98 99 ``` 100 105 101 ``` 102 103 Input 104 105 ``` 106 3 107 00 5000 108 00 5000 109 00 5000 110 ``` 111 112 Output 113 114 ``` 115 0 116 ``` 117 118 Note 119 120 In the first example $4$ spectators can be invited to maximize total influence: $1$, $2$, $3$ and $6$. Their political views are: "11", "10", "01" and "00". So in total $2$ out of $4$ spectators support Alice and $2$ out of $4$ spectators support Bob. The total influence is $6+4+3+9=22$. 121 122 In the second example the direction can select all the people except the $5$-th person. 123 124 In the third example the direction can select people with indices: $1$, $4$, $5$ and $6$. 125 126 In the fourth example it is impossible to select any non-empty set of spectators.