description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
p = [0, 0] + list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
g = [[] for _ in range(n + 1)]
slots = [0] * (n + 1)
cap = [0] * (n + 1)
leaves = [0] * (n + 1)
for u in range(n, 0, -1):
g[p[u]].append(u)
if not g[u]:
cap[u] = a[u]
leaves[u] = 1
continue
maxcap = cap[max(g[u], key=lambda v: cap[v])]
slots_sum = 0
for v in g[u]:
slots_sum += slots[v] + leaves[v] * (maxcap - cap[v])
leaves[u] += leaves[v]
mn = min(slots_sum, a[u])
slots_sum -= mn
a[u] -= mn
ausplit = (a[u] + leaves[u] - 1) // leaves[u]
cap[u] = maxcap + ausplit
slots[u] = slots_sum + ausplit * leaves[u] - a[u]
print(cap[1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
p = list(map(int, input().split()))
a = list(map(int, input().split()))
tree = {}
for i, pp in enumerate(p, start=2):
tree.setdefault(pp, []).append(i)
cache = {}
for s in range(n, 0, -1):
children = tree.get(s, [])
if len(children) == 0:
cache[s] = a[s - 1], a[s - 1], 1
continue
ch_p = [cache[c] for c in children]
ch_max = max(pp[0] for pp in ch_p)
ch_sum = sum(pp[1] for pp in ch_p)
ch_l = sum(pp[2] for pp in ch_p)
sm = ch_sum + a[s - 1]
m = sm // ch_l
if sm % ch_l != 0:
m += 1
cache[s] = max(ch_max, m), sm, ch_l
print(cache[1][0])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LI1():
return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
n = II()
pp = LI1()
aa = LI()
to = [[] for _ in range(n)]
for u, p in enumerate(pp, 1):
to[p].append(u)
mm = [-1] * n
gg = [0] * n
ss = [-1] * n
stack = [0]
while stack:
u = stack.pop()
if ss[u] != -1:
g = gg[u]
cur = (ss[u] + g - 1) // g
mm[u] = max(mm[u], cur)
if u > 0:
p = pp[u - 1]
ss[p] += ss[u]
gg[p] += gg[u]
mm[p] = max(mm[p], mm[u])
else:
ss[u] = aa[u]
if not to[u]:
mm[u] = aa[u]
gg[u] = 1
stack.append(u)
for v in to[u]:
stack.append(v)
print(mm[0])
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
g = [[] for i in range(n + 1)]
p = list(map(int, input().split()))
for i in range(n - 1):
a = i + 2
b = p[i]
g[a].append(b)
g[b].append(a)
dp = [0] + list(map(int, input().split()))
count = [0] * (n + 1)
maxi = [0] * (n + 1)
for i in range(2, n + 1):
if len(g[i]) == 1:
count[i] = 1
stack = []
stack.append(~1)
stack.append(1)
visit1 = [False] * (n + 1)
visit2 = [False] * (n + 1)
visit1[1] = True
while stack:
now = stack.pop()
if now < 0:
now = ~now
for next in g[now]:
if visit2[next]:
dp[now] += dp[next]
count[now] += count[next]
maxi[now] = max(maxi[now], maxi[next])
maxi[now] = max(maxi[now], (dp[now] - 1) // count[now] + 1)
visit2[now] = True
else:
for next in g[now]:
if not visit1[next]:
visit1[next] = True
stack.append(~next)
stack.append(next)
print(maxi[1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
import sys
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
p.insert(0, 0)
for i in range(1, n):
p[i] -= 1
a = list(map(int, input().split()))
data = [0] * n
for i in range(n):
data[i] = [0, a[i], True]
i = n - 1
biggest = 0
while i > 0:
prev = p[i]
if data[i][2]:
data[i][0] += 1
data[prev][0] += data[i][0]
data[prev][1] += data[i][1]
data[prev][2] = False
biggest = max(biggest, 1 + (data[i][1] - 1) // data[i][0])
i -= 1
biggest = max(biggest, 1 + (data[0][1] - 1) // data[0][0])
print(biggest)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
P = [-1] + list(map(lambda x: int(x) - 1, input().split()))
A = list(map(int, input().split()))
ans = 0
C = [[] for i in range(n)]
for i in range(1, n):
C[P[i]] += [i]
que = [0]
B = [0] * n
for i in range(n):
if not C[i]:
B[i] = 1
while que:
q = que.pop()
if C[q]:
que += [C[q].pop()]
else:
ans = max(ans, (A[q] - 1) // B[q] + 1)
if q == 0:
break
B[P[q]] += B[q]
A[P[q]] += A[q]
que += [P[q]]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR LIST VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
from itertools import repeat
from sys import stdin
def main():
n = int(stdin.readline())
p = list(map(int, stdin.readline().split(), repeat(10, n - 1)))
a = list(map(int, stdin.readline().split(), repeat(10, n)))
l = [1] * n
b = [0] * n
ans = 0
for i in range(n - 1, -1, -1):
b[i] += l[i]
x, y = divmod(a[i], b[i])
if y:
x += 1
if ans < x:
ans = x
if i:
x = p[i - 1] - 1
a[x] += a[i]
l[x] = 0
b[x] += b[i]
print(ans)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
a = [int(x) for x in input().split()]
g = [0] * n
for i in range(n - 1):
if type(g[a[i] - 1]) == int:
g[a[i] - 1] = []
g[a[i] - 1].append(i + 1)
a = [int(x) for x in input().split()]
b = [0] * n
now = [0] * n
for i in range(n - 1, -1, -1):
if g[i] == 0:
b[i] = 1
now[i] = a[i]
continue
maxi = 0
for x in g[i]:
b[i] += b[x]
if a[x] < 0:
a[i] += a[x]
a[x] = 0
maxi = max(maxi, now[x])
for x in g[i]:
a[i] -= (maxi - now[x]) * b[x]
now[x] = maxi
now[i] = maxi
if a[i] < 0:
continue
now[i] += (a[i] + b[i] - 1) // b[i]
a[i] -= (a[i] + b[i] - 1) // b[i] * b[i]
print(now[0])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Bandits appeared in the city! One of them is trying to catch as many citizens as he can.
The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square.
After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.
At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.
The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?
-----Input-----
The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$).
The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$).
The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$).
-----Output-----
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.
-----Examples-----
Input
3
1 1
3 1 2
Output
3
Input
3
1 1
3 1 3
Output
4
-----Note-----
In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each.
In the second example no matter how citizens act the bandit can catch at least $4$ citizens.
|
n = int(input())
P = list(map(int, input().split()))
A = list(map(int, input().split()))
g = [[] for _ in range(n)]
P = [(p - 1) for p in P]
parent = [-1] * n
for i, p in enumerate(P):
g[p].append(i + 1)
parent[i + 1] = p
s = []
s.append(0)
order = []
while s:
v = s.pop()
order.append(v)
for u in g[v]:
if u != parent[v]:
s.append(u)
C = [0] * n
L = [0] * n
order.reverse()
for v in order:
C[v] += A[v]
if len(g[v]) == 0:
L[v] += 1
if parent[v] != -1:
C[parent[v]] += C[v]
L[parent[v]] += L[v]
ans = -1
for v in range(n):
ans = max(ans, (C[v] + L[v] - 1) // L[v])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Treeland consists of $n$ cities and $n-1$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $k$ and the number of companies taking part in the privatization is minimal.
Choose the number of companies $r$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $k$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $r$ that there is such assignment to companies from $1$ to $r$ that the number of cities which are not good doesn't exceed $k$. [Image] The picture illustrates the first example ($n=6, k=2$). The answer contains $r=2$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $3$) is not good. The number of such vertices (just one) doesn't exceed $k=2$. It is impossible to have at most $k=2$ not good cities in case of one company.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \le n \le 200000, 0 \le k \le n - 1$) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following $n-1$ lines contain roads, one road per line. Each line contains a pair of integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$, $y_i$ are cities connected with the $i$-th road.
-----Output-----
In the first line print the required $r$ ($1 \le r \le n - 1$). In the second line print $n-1$ numbers $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le r$), where $c_i$ is the company to own the $i$-th road. If there are multiple answers, print any of them.
-----Examples-----
Input
6 2
1 4
4 3
3 5
3 6
5 2
Output
2
1 2 1 1 2
Input
4 2
3 1
1 4
1 2
Output
1
1 1 1
Input
10 2
10 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
Output
3
1 1 2 3 2 3 1 3 1
|
def strelizia():
oh_pie = 0
kaguya = 0
owe_pie = [(0, 0, 0)]
while kaguya <= oh_pie:
o_pie = oh_pie
while kaguya <= o_pie:
x = owe_pie[kaguya][0]
p = owe_pie[kaguya][1]
c = 1
l = len(stamen[x])
if l > virm:
for to, ed in stamen[x]:
if to != p:
darling[ed - 1] = c
owe_pie.append((to, x, c))
oh_pie += 1
else:
for to, ed in stamen[x]:
if c == owe_pie[kaguya][2]:
c += 1
if to != p:
darling[ed - 1] = c
owe_pie.append((to, x, c))
oh_pie += 1
c += 1
kaguya += 1
darling = []
franxx = input().split()
pistil = []
stamen = []
for i in range(0, int(franxx[0])):
pistil.append(0)
stamen.append([])
for i in range(1, int(franxx[0])):
darling.append(0)
edge = input().split()
stamen[int(edge[0]) - 1].append((int(edge[1]) - 1, i))
stamen[int(edge[1]) - 1].append((int(edge[0]) - 1, i))
pistil[int(edge[0]) - 1] += 1
pistil[int(edge[1]) - 1] += 1
pistil.sort()
virm = pistil[int(franxx[0]) - int(franxx[1]) - 1]
print(virm)
strelizia()
for i in range(1, int(franxx[0])):
print(darling[i - 1], end=" ")
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING
|
Treeland consists of $n$ cities and $n-1$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $k$ and the number of companies taking part in the privatization is minimal.
Choose the number of companies $r$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $k$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $r$ that there is such assignment to companies from $1$ to $r$ that the number of cities which are not good doesn't exceed $k$. [Image] The picture illustrates the first example ($n=6, k=2$). The answer contains $r=2$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $3$) is not good. The number of such vertices (just one) doesn't exceed $k=2$. It is impossible to have at most $k=2$ not good cities in case of one company.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \le n \le 200000, 0 \le k \le n - 1$) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following $n-1$ lines contain roads, one road per line. Each line contains a pair of integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$, $y_i$ are cities connected with the $i$-th road.
-----Output-----
In the first line print the required $r$ ($1 \le r \le n - 1$). In the second line print $n-1$ numbers $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le r$), where $c_i$ is the company to own the $i$-th road. If there are multiple answers, print any of them.
-----Examples-----
Input
6 2
1 4
4 3
3 5
3 6
5 2
Output
2
1 2 1 1 2
Input
4 2
3 1
1 4
1 2
Output
1
1 1 1
Input
10 2
10 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
Output
3
1 1 2 3 2 3 1 3 1
|
def set_color(edge, colors, color):
colors[edge] = color
colors[edge[1], edge[0]] = color
def set_node_color(node, parent, parent_color, index, bad_nodes, colors):
color = 1
for child_node in (child for child in index[node] if child != parent):
if color == parent_color:
color += 1
new_color = parent_color if node in bad_nodes else color
color += 1
set_color((node, child_node), colors, new_color)
set_node_color(child_node, node, new_color, index, bad_nodes, colors)
def solve(n, k, edges):
colors = {edge: None for edge in edges}
index = {i: set() for i in range(1, n + 1)}
for a, b in edges:
index[a].add(b)
index[b].add(a)
nodes = sorted(list(range(1, n + 1)), key=lambda x: len(index[x]), reverse=True)
bad_nodes = set(nodes[:k])
frontier = [(nodes[k], None, None)]
while frontier:
next_, parent_node, parent_color = frontier.pop()
color = 1
for child_node in (ele for ele in index[next_] if ele != parent_node):
if color == parent_color:
color += 1
new_color = parent_color if next_ in bad_nodes else color
set_color((next_, child_node), colors, new_color)
color += 1
frontier.append((child_node, next_, new_color))
return [colors[edge] for edge in edges]
def main():
n, k = map(int, input().split())
edges = []
for i in range(n - 1):
edges.append(tuple(map(int, input().split())))
result = solve(n, k, edges)
print(len(set(result)))
print(" ".join(map(str, result)))
main()
|
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NONE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR NONE NONE WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Treeland consists of $n$ cities and $n-1$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $k$ and the number of companies taking part in the privatization is minimal.
Choose the number of companies $r$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $k$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $r$ that there is such assignment to companies from $1$ to $r$ that the number of cities which are not good doesn't exceed $k$. [Image] The picture illustrates the first example ($n=6, k=2$). The answer contains $r=2$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $3$) is not good. The number of such vertices (just one) doesn't exceed $k=2$. It is impossible to have at most $k=2$ not good cities in case of one company.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \le n \le 200000, 0 \le k \le n - 1$) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following $n-1$ lines contain roads, one road per line. Each line contains a pair of integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$, $y_i$ are cities connected with the $i$-th road.
-----Output-----
In the first line print the required $r$ ($1 \le r \le n - 1$). In the second line print $n-1$ numbers $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le r$), where $c_i$ is the company to own the $i$-th road. If there are multiple answers, print any of them.
-----Examples-----
Input
6 2
1 4
4 3
3 5
3 6
5 2
Output
2
1 2 1 1 2
Input
4 2
3 1
1 4
1 2
Output
1
1 1 1
Input
10 2
10 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
Output
3
1 1 2 3 2 3 1 3 1
|
import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def comp(a, b):
if a > b:
return 2
return 2 if a == b else 0
def gi():
return [xx for xx in input().split()]
def gtc(tc, ans):
print("Case #" + str(tc) + ":", ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = 1
uu = t
def dfs(i, p=-1, c=-1):
st = [(i, p, c)]
vis = {}
while st:
i, p, c = st[-1]
if i in vis:
st.pop()
continue
vis[i] = 1
r = (c + 1) % ans
for j in a[i]:
if j != p:
col[edge[i, j]] = r + 1
st.append((j, i, r))
r = (r + 1) % ans
while t > 0:
t -= 1
d = {}
n, k = mi()
col = [0] * (n - 1)
d = [0] * (n + 1)
a = [[] for i in range(n + 1)]
edge = {}
for i in range(n - 1):
x, y = mi()
d[x] += 1
d[y] += 1
edge[x, y] = i
edge[y, x] = i
a[x].append(y)
a[y].append(x)
p = {}
for i in range(1, n + 1):
inc(p, d[i])
r = []
for i in p:
r.append([i, p[i]])
r.sort(reverse=True)
ans = 1
for i in range(len(r)):
if k < r[i][1]:
ans = r[i][0]
break
k -= r[i][1]
print(ans)
dfs(1)
print(*col)
|
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for _ in range(t):
n = int(input())
playList = []
for index in range(n):
band, songLen = map(int, input().split())
playList.append([band, songLen])
playList.sort(reverse=False)
smallestSongs = []
total = 0
for index in range(n):
if index == 0 or playList[index - 1][0] < playList[index][0]:
smallestSongs.append(playList[index][1])
else:
total += playList[index][1]
M = len(smallestSongs)
smallestSongs.sort(reverse=False)
answer = 0
for index in range(M):
answer += (index + 1) * smallestSongs[index]
answer += M * total
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for _ in range(t):
hash = {}
n = int(input())
rem = []
for i in range(n):
a, b = map(int, input().split())
if a in hash:
if hash[a] > b:
rem.append(hash[a])
hash[a] = b
else:
rem.append(b)
else:
hash[a] = b
rem.sort()
c = [hash[i] for i in hash.keys()]
c.sort()
ans = 0
for i in range(len(c)):
ans += (i + 1) * c[i]
m = len(c)
ans += m * sum(rem)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for _ in range(t):
songs = []
n = int(input())
for _ in range(n):
b, l = map(int, input().split())
songs.append((b, l))
songs.sort()
total = 0
js = []
for i in range(n):
if i == 0 or songs[i - 1][0] < songs[i][0]:
js.append(songs[i][1])
else:
total = total + songs[i][1]
js.sort()
ans = 0
for i in range(1, len(js) + 1):
ans += i * js[i - 1]
ans += len(js) * total
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
band = {}
for i in range(n):
b, l = map(int, input().split())
if b in band:
band[b].append(l)
else:
band[b] = [l]
out = 0
arr = []
k = len(band)
for i in band:
band[i].sort()
arr.append(band[i].pop(0))
arr.sort()
for i in range(k):
out += (i + 1) * arr[i]
for i in band:
for j in band[i]:
out += k * j
print(out)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
def maxBandLength(bands, n):
bands.sort(key=lambda x: x[1])
list2 = []
len_set = set()
count = 0
for item in bands:
if item[0] not in len_set:
len_set.add(item[0])
count += len(len_set) * item[1]
else:
list2.append(item[1])
for length in list2:
count += length * len(len_set)
return count
t = int(input())
for i in range(t):
bands = []
n = int(input())
for j in range(n):
bands.append(tuple(map(int, input().split())))
print(maxBandLength(bands, n))
|
FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for T in range(int(input())):
N = int(input())
dic = {}
no_band = 0
for i in range(N):
B, L = map(int, input().split())
try:
if L < dic[B][0]:
dic[B].insert(0, L)
else:
dic[B].append(L)
except KeyError:
no_band += 1
dic[B] = [L]
li_min = [li.pop(0) for li in dic.values()]
li_min.sort()
sumi = 0
for i in range(len(li_min)):
sumi += (i + 1) * li_min[i]
total_sumi = 0
for li in dic.values():
total_sumi += sum(li)
print(sumi + total_sumi * no_band)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
a, b = map(int, input().split())
arr.append([b, a])
arr.sort()
total = 0
j = 1
ans = 0
su = {}
for i in arr:
if i[1] not in su:
su[i[1]] = 1
ans += i[0] * j
j += 1
else:
total += i[0]
ans += total * (j - 1)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
l = [0] * n
b = [0] * n
q = []
for i in range(n):
q.append(list(map(int, input().split())))
q = sorted(q, key=lambda x: x[1])
prod = 0
count = 0
dict = {}
t = 0
for i in q:
if i[0] not in dict.keys():
dict[i[0]] = 1
count += 1
prod += count * i[1]
else:
t += i[1]
prod += t * count
print(prod)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
while t:
songs = []
res = 0
n = int(input())
unique_bands = {}
for _ in range(n):
b_l = [int(i) for i in input().split()]
songs.append({"band": b_l[0], "length": b_l[1], "visited": 0})
songs = sorted(songs, key=lambda k: k["length"])
for song in range(len(songs)):
if (
songs[song]["band"] not in unique_bands.keys()
and not songs[song]["visited"]
):
unique_bands[songs[song]["band"]] = songs[song]["length"]
i = len(unique_bands)
res += songs[song]["length"] * i
songs[song]["visited"] = 1
for song in range(len(songs)):
if not songs[song]["visited"]:
res += songs[song]["length"] * i
print(res)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR DICT STRING STRING STRING VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING VAR ASSIGN VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
a, b = map(int, input().split())
arr.append((b, a))
arr.sort()
band = set()
play_last = []
res = 0
for l, b in arr:
if b not in band:
band.add(b)
res += l * len(band)
else:
play_last.append((l, b))
for l, b in play_last:
res += l * len(band)
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
s = set()
ls = list()
arr = list()
for i in range(n):
b, l = map(int, input().split())
ls.append([l, b])
ans = Count = 0
ls.sort()
for i in range(n):
if ls[i][1] not in s:
Count += 1
ans += Count * ls[i][0]
s.add(ls[i][1])
else:
arr.append(ls[i][0])
for i in range(len(arr)):
ans += Count * arr[i]
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
ls = []
s = set()
ans = 0
temp = []
cnt = 0
for i in range(int(input())):
b, l = list(map(int, input().split()))
ls.append([l, b])
ls.sort()
for i in range(len(ls)):
if ls[i][1] not in s:
s.add(ls[i][1])
cnt += 1
ans += cnt * ls[i][0]
else:
temp.append(ls[i][0])
for i in range(len(temp)):
ans += cnt * temp[i]
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
nl = []
for i in range(n):
nl.append([int(x) for x in input().split()])
nl.sort(key=lambda x: x[1])
ans = 0
vis = [False] * len(nl)
bp = {}
bb = 0
for i in range(n):
if nl[i][0] not in bp:
bp[nl[i][0]] = True
bb += 1
ans += bb * nl[i][1]
vis[i] = True
for i in range(n):
if not vis[i]:
ans += bb * nl[i][1]
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
s = []
dp = {}
cnt = 0
sweetness = 0
visited = [False] * n
for _ in range(n):
s.append([int(x) for x in input().split()])
s.sort(key=lambda x: x[1])
for i in range(n):
if s[i][0] not in dp:
dp[s[i][0]] = True
cnt += 1
sweetness += cnt * s[i][1]
visited[i] = True
for i in range(n):
if not visited[i]:
sweetness += cnt * s[i][1]
print(sweetness)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
T = int(input())
for z in range(T):
N = int(input())
dct = {}
min_dct = {}
for i in range(N):
band, song = map(int, input().split())
if band in dct:
if song < min_dct[band]:
dct[band].append(min_dct[band])
min_dct[band] = song
else:
dct[band].append(song)
else:
min_dct[band] = song
dct[band] = []
min_lst = []
lst = []
for elem in min_dct.values():
min_lst.append(elem)
for elem in dct.values():
lst += elem
min_lst.sort()
lst.sort()
l = len(min_lst)
ans = 0
for i in range(l):
ans += min_lst[i] * (i + 1)
ans += l * sum(lst)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
num_songs = int(input())
songs_list = []
for i in range(num_songs):
songs_list.append(tuple(map(int, input().split())))
songs_list.sort(key=lambda x: x[1])
played_list = [False] * num_songs
previously_palyed = {}
sweetness = 0
for i in range(len(songs_list)):
song = songs_list[i]
if song[0] not in previously_palyed:
previously_palyed[song[0]] = True
sweetness += song[1] * len(previously_palyed)
played_list[i] = True
for i in range(len(songs_list)):
song = songs_list[i]
if not played_list[i]:
sweetness += song[1] * len(previously_palyed)
print(sweetness)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
from sys import *
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
l = []
d = set()
ans = 0
new = []
for _ in range(n):
a, b = map(int, stdin.readline().split())
l.append([b, a])
l.sort()
for j in l:
if j[1] not in d:
d.add(j[1])
ans += j[0] * len(d)
else:
new.append(j[0])
print(ans + len(d) * sum(new))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
T = int(input())
for _ in range(T):
N = int(input())
songs = []
for _ in range(N):
songs.append(list(map(int, input().split())))
sweetness = 0
played = set()
play_later = []
songs.sort(key=lambda x: x[1])
for i in songs:
if i[0] not in played:
played.add(i[0])
sweetness += len(played) * i[1]
else:
play_later.append(i)
for i in play_later:
sweetness += len(played) * i[1]
print(sweetness)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for T in range(int(input())):
n = int(input())
a = []
b = []
d = {}
ind = {}
visited = [(True) for x in range(n)]
for i in range(n):
x, y = map(int, input().split())
a.append(x)
b.append(y)
if x not in d:
d[x] = []
d[x].append(y)
if x not in ind:
ind[x] = {}
if y not in ind[x]:
ind[x][y] = []
ind[x][y].append(i)
mins = []
for keys in d:
y = min(d[keys])
mins.append(y)
visited[ind[keys][y][0]] = False
mins.sort()
sweetness = 0
unique = 0
for i in range(len(mins)):
sweetness += mins[i] * (i + 1)
unique += 1
for i in range(n):
if visited[i]:
sweetness += b[i] * unique
print(sweetness)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
tc = int(input())
for _ in range(tc):
n = int(input())
mp = {}
for i in range(n):
b, l = map(int, input().split())
if b in mp:
mp[b].append(l)
else:
mp[b] = [l]
lst = []
for b in mp.values():
b.sort()
lst.append(b[0])
bands = len(lst)
lst.sort()
ans = 0
for i in range(bands):
ans += (i + 1) * lst[i]
for b in mp.values():
l = len(b)
for i in range(1, l):
ans += bands * b[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
a = int(input())
yu = []
pussy = set()
count = 0
ans = 0
temp = 0
for _ in range(a):
yu.append(list(map(int, input().split())))
ty = sorted(yu, key=lambda x: x[1])
for i in ty:
if i[0] not in pussy:
pussy.add(i[0])
count += 1
ans += count * i[1]
else:
temp += i[1]
ans += temp * count
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for i in range(0, t):
n = int(input())
li = []
for j in range(0, n):
b, l = map(int, input().split())
li.append([l, b])
li.sort()
m = {}
total = 0
cn = 1
ans = 0
for x in li:
if x[1] not in m:
m[x[1]] = 1
ans = ans + x[0] * cn
cn += 1
else:
total = total + x[0]
print(ans + (cn - 1) * total)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
def solve(arr):
bands = {}
for b, l in arr:
if b not in bands:
bands[b] = l
bands[b] = min(l, bands[b])
t = sum(l * i for i, l in enumerate(sorted(bands.values()), 1))
for b, l in arr:
if l == bands[b]:
bands[b] = -1
continue
t += l * len(bands)
return t
def main():
for _ in range(int(input())):
N = int(input())
arr = [[int(val) for val in input().split()] for _ in range(N)]
result = solve(arr)
print(result)
main()
|
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
def sweet(sweet, n):
n_keys = 0
first_m = []
for k, v in sweet.items():
sweet[k] = sorted(v, reverse=True)
n_keys += 1
first_m.append(sweet[k][-1])
sweet[k].pop()
first_m = sorted(first_m)
total = 0
for i in range(n_keys):
total += (i + 1) * first_m[i]
for k in sweet.keys():
total += sum(sweet[k]) * n_keys
return total
for _ in range(int(input())):
songs = dict()
n = int(input())
for i in range(n):
b, l = map(int, input().split())
if b in songs:
songs[b].append(l)
else:
songs[b] = [l]
out = sweet(songs, n)
print(out)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
def calc_sweetness(l, sweetness):
length = 0
songs.append(l[0][1])
for i in range(len(l) - 1):
if l[i][0] < l[i + 1][0]:
songs.append(l[i + 1][1])
else:
length = length + l[i + 1][1]
songs.sort()
for i in range(1, len(songs) + 1):
sweetness = sweetness + i * songs[i - 1]
return sweetness, length
no_test_cases = int(input())
while no_test_cases > 0:
songs = []
li = []
no_songs = int(input())
sweetness = 0
while no_songs > 0:
s = input()
b, l = s.split()
t = int(b), int(l)
li.append(t)
no_songs = no_songs - 1
li.sort()
sweetness, length = calc_sweetness(li, sweetness)
sweetness = sweetness + len(songs) * length
print(sweetness)
no_test_cases = no_test_cases - 1
|
FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
songs = []
counted = {}
bands_done = 0
for _ in range(n):
songs.append(tuple(map(int, input().split())))
songs.sort(key=lambda x: x[1])
ans = 0
left_length = 0
while len(songs) > 0:
current = songs.pop(0)
if current[0] not in counted:
counted[current[0]] = 1
bands_done += 1
ans += bands_done * current[1]
else:
left_length += current[1]
ans += bands_done * left_length
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
T = int(input())
for i in range(T):
N = int(input())
BL = []
for j in range(N):
BL.append(list(map(int, input().split())))
BL = sorted(BL)
BL_new = [0] * len(BL)
BL_new[0] = BL[0]
count1 = 0
count2 = 0
for k in range(len(BL) - 1):
if BL[k][0] == BL[k + 1][0]:
count1 -= 1
BL_new[count1] = BL[k + 1]
else:
count2 += 1
BL_new[count2] = BL[k + 1]
temp = []
for i in range(len(BL_new)):
temp.append(BL_new[i][0])
temp_len = len(set(temp))
temp2 = []
for i in range(temp_len):
temp2.append(BL_new[i][1])
temp2.sort()
sweetness = 0
for j in range(1, temp_len + 1):
sweetness += j * temp2[j - 1]
for k in range(temp_len, len(BL_new)):
sweetness += temp_len * BL_new[k][1]
print(sweetness)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
b = []
l = []
for i in range(n):
bi, li = map(int, input().split())
b.append(bi)
l.append(li)
d = dict()
e = dict()
arr = []
count = 0
sm = 0
for i in range(n):
if b[i] in d:
cur = d[b[i]]
if cur > l[i]:
sm += cur
d[b[i]] = l[i]
arr[e[b[i]]] = l[i]
else:
sm += l[i]
else:
d[b[i]] = l[i]
arr.append(l[i])
e[b[i]] = count
count += 1
arr.sort()
ans = 0
for i in range(1, count + 1):
ans += arr[i - 1] * i
if sm > 0:
ans += sm * count
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
from sys import stdin
data = stdin.readlines()
d = 0
T = int(data[d])
d += 1
for t in range(T):
pairs = {}
rem = 0
N = int(data[d])
d += 1
length = 0
for n in range(N):
b, l = map(int, data[d].split(" "))
d += 1
if b not in pairs:
pairs[b] = l
length += 1
elif pairs[b] < l:
rem += l
else:
rem += pairs[b]
pairs[b] = l
a = []
a = pairs.values()
a = list(a)
a.sort()
total = 0
j = 1
for i in a:
total += j * i
j += 1
total += length * rem
print(total)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
from itertools import permutations as p
t = int(input())
for _ in range(t):
n = int(input())
l1 = []
l2 = []
for i in range(n):
a, b = map(int, input().split())
l1.append([b, a])
l2.append([a, b])
d = {}
for i in l1:
try:
d[i[0]].append(i[1])
except:
d[i[0]] = [i[1]]
x = list(d.keys())
x.sort()
ans = 0
e = {}
f = 0
for i in x:
for j in d[i]:
try:
m = e[j]
f += i
except:
e[j] = 0
ans += len(e) * i
ans += f * len(e)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
from sys import stdin
tc = int(stdin.readline())
for i in range(tc):
n = int(stdin.readline())
list1 = []
for j in range(n):
b, l = map(int, stdin.readline().split())
list1.append((l, b))
list1.sort()
distinct = set()
total = 0
to_add = []
for each in list1:
if each[1] not in distinct:
distinct.add(each[1])
sweet = len(distinct)
total += sweet * each[0]
else:
to_add.append(each[0])
print(total + len(distinct) * sum(to_add))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
l = []
for i in range(n):
bi, li = map(int, input().split())
l.append((bi, li))
l.sort()
d = dict()
co = 0
cb = 0
ms = []
for i, j in l:
try:
d[i]
co += j
except KeyError:
d[i] = 1
cb += 1
ms.append(j)
ms.sort()
co *= cb
for i in range(cb):
co += (i + 1) * ms[i]
print(co)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input().strip())):
n = int(input().strip())
lst = []
for idx in range(n):
lst.append(list(map(int, input().strip().split())))
lst.sort()
distinct_song_dict = {}
first_songs = []
for song in lst:
band_no, song_length = song[0], song[1]
if band_no not in distinct_song_dict:
distinct_song_dict[band_no] = []
first_songs.append(song_length)
else:
distinct_song_dict[band_no].append(song_length)
first_songs.sort()
total_sum = 0
idx = 0
while idx < len(first_songs):
idx += 1
total_sum += idx * first_songs[idx - 1]
for band_no in distinct_song_dict:
total_sum += idx * sum(distinct_song_dict[band_no])
print(total_sum)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
try:
for _ in range(int(input())):
N = int(input())
cell = {}
variables = []
space = []
score = 0
variable = 0
for __ in range(N):
a, b = map(int, input().split())
if a not in cell:
cell[a] = b
elif cell[a] >= b:
cache = cell[a]
cell[a] = b
space.append(cache)
else:
space.append(b)
cc = list(cell.values())
cc.sort()
for items in cc:
variable += 1
score += variable * items
if len(space) > 0:
score += variable * sum(space)
print(score)
except:
pass
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
a = int(input())
for _ in range(a):
aa = int(input())
kk = []
table = {}
for i in range(aa):
bb = list(map(int, input().split()))
kk.append((bb[0], bb[1]))
kk = sorted(kk, key=lambda x: x[1])
count = 0
l = 0
j = 0
for i in kk:
if i[0] not in table:
l += 1
table[i[0]] = 1
count += l * i[1]
else:
j += i[1]
print(count + l * j)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
from sys import stdin
t = int(stdin.readline())
for i in range(t):
n = int(stdin.readline())
count = 0
uniq = set()
final = []
sum1 = 0
list1 = []
for j in range(n):
b, l = list(map(int, stdin.readline().split()))
list1.append((b, l))
list1.sort(key=lambda x: x[1])
for j in range(n):
if list1[j][0] not in uniq:
count += 1
uniq.add(list1[j][0])
sum1 += count * list1[j][1]
else:
final.append(list1[j][1])
len1 = len(uniq)
print(sum1 + len1 * sum(final))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for i in range(t):
n = int(input())
l = [list(map(int, input().split())) for j in range(n)]
l = sorted(l, key=lambda x: x[1])
k = set()
f = 0
prev = 0
sweetness = 0
for i in l:
if i[0] in k:
continue
sweetness += (f + 1) * i[1]
k.add(i[0])
f += 1
l = sorted(l)
for i in l:
if i[0] in k:
if prev == i[0]:
sweetness += f * i[1]
prev = i[0]
print(sweetness)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for _ in range(t):
n = int(input())
l = {}
ld = 0
for i in range(n):
x, y = map(int, input().split())
if x not in l:
l[x] = y
elif l.get(x) > y:
ld += l.get(x)
l[x] = y
else:
ld += y
sorted_l = sorted(l.items(), key=lambda x: x[1])
s = 0
k = 0
for i in sorted_l:
k += 1
s = s + i[1] * k
s = s + ld * k
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
for _ in range(int(input())):
n = int(input())
songs = []
for _ in range(n):
songs.append(tuple(map(int, input().split())))
songs.sort(key=lambda x: x[1])
d = dict()
unique = []
common = []
for i, j in songs:
if i not in d:
d[i] = 1
unique.append((i, j))
else:
common.append((i, j))
songs = unique + common
d = dict()
dt = 0
ans = 0
for i, j in songs:
if i not in d:
d[i] = 1
dt += 1
ans += j * dt
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
import time
start = time.time()
t = int(input())
for i in range(t):
n = int(input())
a = []
a1 = {}
sum1 = 0
lt = 0
for j in range(n):
b, l = map(int, input().split())
a.append([l, b])
a.sort()
c = 0
for k in a:
if k[1] in a1:
lt += k[0]
else:
c += 1
sum1 += c * k[0]
a1[k[1]] = 1
sum1 += c * lt
print(sum1)
end = time.time()
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for T in range(t):
n = int(input())
val = []
s = set()
for i in range(n):
val.append([int(x) for x in input().split()][::-1])
val.sort()
c = 0
c1 = 0
ans = 0
for i in range(n):
if val[i][1] not in s:
s.add(val[i][1])
c += 1
ans += c * val[i][0]
else:
c1 += val[i][0]
ans += c * c1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = []
for i in range(n):
b, l = [int(x) for x in input().strip().split()]
arr.append((b, l))
arr = sorted(arr, key=lambda x: x[1])
band = {}
for i in range(n):
band[arr[i][0]] = 0
k = 0
uniqueval = 0
repeatedval = 0
for i in range(n):
if band[arr[i][0]] == 0:
k += 1
band[arr[i][0]] = 1
uniqueval += arr[i][1] * k
else:
repeatedval += arr[i][1]
print(uniqueval + repeatedval * k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
for _ in range(int(input())):
a, b, ab, ba = map(int, input().split())
s = input()
if s.count("A") != a + ab + ba:
print("NO")
continue
stack = [[1, s[0]]]
for i in range(1, len(s)):
if stack[-1][1] != s[i]:
x = stack.pop()
stack.append([x[0] + 1, s[i]])
else:
stack.append([1, s[i]])
stack.sort()
trash = 0
for val, ele in stack:
if not val % 2:
if ele == "A" and ba >= val // 2:
ba -= val // 2
elif ele == "B" and ab >= val // 2:
ab -= val // 2
else:
trash += val // 2 - 1
else:
trash += val // 2
print("YES" if trash >= ab + ba else "NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING STRING
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
import sys
def solve():
inp = sys.stdin.readline
a, b, c, d = map(int, inp().split())
s = inp()
if s.count("A") != a + c + d or s.count("B") != b + c + d:
print("NO")
return
cc = {"A": [], "B": []}
f = 0
e = 0
for i in range(1, len(s) - 1):
if s[i] == s[i - 1]:
l = i - f
if l % 2 == 0:
cc[s[f]].append(l // 2)
else:
e += l // 2
f = i
l = len(s) - 1 - f
if l % 2 == 0:
cc[s[f]].append(l // 2)
else:
e += l // 2
ca = cc["A"]
ca.sort(reverse=True)
while ca and c > 0:
w = min(ca[-1], c)
ca[-1] -= w
c -= w
if ca[-1] == 0:
ca.pop()
cb = cc["B"]
cb.sort(reverse=True)
while cb and d > 0:
w = min(cb[-1], d)
cb[-1] -= w
d -= w
if cb[-1] == 0:
cb.pop()
for i in ca:
e += i - 1
for i in cb:
e += i - 1
if c + d <= e:
print("YES")
else:
print("NO")
def main():
for i in range(int(sys.stdin.readline())):
solve()
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR DICT STRING STRING LIST LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
def solve():
a, b, ab, ba = list(map(int, input().split()))
s = input().strip()
a_n = s.count("A")
b_n = s.count("B")
if a + ab + ba != a_n:
return False
elif b + ab + ba != b_n:
return False
abab_lens = []
baba_lens = []
ab_or_ba_count = 0
prev_symbol = None
start_symbol = s[0]
start_index = 0
for i, c in enumerate(list(s) + [s[-1]]):
if c == prev_symbol:
part_len = i - start_index
if part_len > 1:
if part_len % 2 == 1:
ab_or_ba_count += part_len // 2
elif c == "B":
abab_lens += [part_len // 2]
else:
baba_lens += [part_len // 2]
start_index = i
start_symbol = c
prev_symbol = c
for l in sorted(abab_lens):
if ab >= l:
ab -= l
else:
ab_or_ba_count += l - 1
for l in sorted(baba_lens):
if ba >= l:
ba -= l
else:
ab_or_ba_count += l - 1
return ab + ba <= ab_or_ba_count
n = int(input())
for i in range(n):
if solve():
print("YES")
else:
print("NO")
|
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR LIST VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR STRING VAR LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
tc = int(input())
for _ in range(tc):
a, b, c, d = list(map(int, input().split()))
s = input()
if s.count("A") != a + c + d:
print("NO")
continue
x = "X"
k = 0
z = []
for i in s:
if i == x:
z.append((k, x))
k = 1
else:
x = i
k += 1
z.append((k, x))
r = 0
z.sort()
for k, v in z:
if k % 2 == 0:
if v == "A" and d >= k // 2:
d -= k // 2
elif v == "B" and c >= k // 2:
c -= k // 2
else:
r += k // 2 - 1
else:
r += k // 2
if r >= c + d:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
import sys
input = sys.stdin.readline
def solve():
a, b, c, d = map(int, input().split())
s = input().strip()
n = len(s)
cnt = 0
for i in range(n):
if s[i] == "A":
cnt += 1
if cnt != a + c + d:
return "NO"
prev = -1
cnt = 0
r = []
for i in range(n):
if prev == s[i]:
r.append((cnt, prev))
cnt = 1
else:
cnt += 1
prev = s[i]
r.append((cnt, prev))
r.sort()
e = 0
for length, lc in r:
if length % 2 == 0:
if lc == "B" and length // 2 <= c:
c -= length // 2
elif lc == "A" and length // 2 <= d:
d -= length // 2
else:
e += length // 2 - 1
else:
e += length // 2
return "YES" if e >= c + d else "NO"
for _ in range(int(input())):
print(solve())
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
def solve():
cnt_a, cnt_b, cnt_ab, cnt_ba = map(int, input().strip().split())
s = input()
if s.count("A") != cnt_a + cnt_ba + cnt_ab:
print("NO")
return
stk = [[1, s[0]]]
for i in range(1, len(s)):
if i == 0:
continue
c = s[i]
if c != stk[-1][1]:
x = stk.pop()
stk.append([x[0] + 1, c])
else:
stk.append([1, c])
stk.sort()
rest = 0
for cnt, last in stk:
if not cnt % 2:
if last == "A" and cnt_ba >= cnt >> 1:
cnt_ba -= cnt >> 1
elif last == "B" and cnt_ab >= cnt >> 1:
cnt_ab -= cnt >> 1
else:
rest += (cnt >> 1) - 1
else:
rest += cnt >> 1
if rest >= cnt_ab + cnt_ba:
print("YES")
else:
print("NO")
t = int(input())
for _ in range(t):
solve()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
I = input
for _ in range(int(I())):
a, b, c, d = map(int, I().split())
s = I()
if s.count("A") != a + c + d:
print("NO")
continue
i, n, segs = 0, len(s), []
while i < n:
j = i
while i + 1 < n and s[i + 1] != s[i]:
i += 1
segs.append((i - j + 1, s[i]))
i += 1
segs.sort()
trash = 0
for siz, ch in segs:
cnt = siz // 2
if siz & 1 == 0:
if ch == "A" and d >= cnt:
d -= cnt
elif ch == "B" and c >= cnt:
c -= cnt
else:
trash += cnt - 1
else:
trash += cnt
print("YES" if trash >= c + d else "NO")
|
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR VAR VAR VAR IF VAR STRING VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING STRING
|
Alina has discovered a weird language, which contains only $4$ words: ${A}$, ${B}$, ${AB}$, ${BA}$. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.
Alina has found one such sentence $s$ and she is curious: is it possible that it consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$?
In other words, determine, if it's possible to concatenate these $a+b+c+d$ words in some order so that the resulting string is $s$. Each of the $a+b+c+d$ words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0\le a,b,c,d\le 2\cdot 10^5$) — the number of times that words ${A}$, ${B}$, ${AB}$, ${BA}$ respectively must be used in the sentence.
The second line contains the string $s$ ($s$ consists only of the characters ${A}$ and ${B}$, $1\le |s| \le 2\cdot 10^5$, $|s|=a+b+2c+2d$) — the sentence. Notice that the condition $|s|=a+b+2c+2d$ (here $|s|$ denotes the length of the string $s$) is equivalent to the fact that $s$ is as long as the concatenation of the $a+b+c+d$ words.
The sum of the lengths of $s$ over all test cases doesn't exceed $2\cdot 10^5$.
-----Output-----
For each test case output ${YES}$ if it is possible that the sentence $s$ consists of precisely $a$ words ${A}$, $b$ words ${B}$, $c$ words ${AB}$, and $d$ words ${BA}$, and ${NO}$ otherwise. You can output each letter in any case.
-----Examples-----
Input
8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA
Output
NO
YES
YES
YES
YES
YES
NO
YES
-----Note-----
In the first test case, the sentence $s$ is ${B}$. Clearly, it can't consist of a single word ${A}$, so the answer is ${NO}$.
In the second test case, the sentence $s$ is ${AB}$, and it's possible that it consists of a single word ${AB}$, so the answer is ${YES}$.
In the third test case, the sentence $s$ is ${ABAB}$, and it's possible that it consists of one word ${A}$, one word ${B}$, and one word ${BA}$, as ${A} + {BA} + {B} = {ABAB}$.
In the fourth test case, the sentence $s$ is ${ABAAB}$, and it's possible that it consists of one word ${A}$, one word ${AB}$, and one word ${BA}$, as ${A} + {BA} + {AB} = {ABAAB}$.
In the fifth test case, the sentence $s$ is ${BAABBABBAA}$, and it's possible that it consists of one word ${A}$, one word ${B}$, two words ${AB}$, and two words ${BA}$, as ${BA} + {AB} + {B} + {AB} + {BA} + {A}= {BAABBABBAA}$.
|
t = int(input())
pp = 0
for _ in range(t):
a, b, c, d = [int(i) for i in input().split()]
s = input()
if s.count("A") != a + c + d or s.count("B") != b + c + d:
print("NO")
continue
l = len(s)
ult = "X"
k = 0
z = []
for i in range(l):
if s[i] == ult:
z.append((k, ult))
k = 1
else:
ult = s[i]
k += 1
z.append((k, ult))
r = 0
z.sort()
for i in z:
if i[0] % 2 == 0:
if i[1] == "A" and d >= i[0] // 2:
d -= i[0] // 2
elif i[1] == "B" and c >= i[0] // 2:
c -= i[0] // 2
else:
r += i[0] // 2 - 1
else:
r += i[0] // 2
print("YES" if r >= c + d else "NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING STRING
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
clear = [0]
clear.extend(sorted(list(map(int, input().split()))))
count = 0
while n - 1 >= 0:
if clear[n] + clear[n - 1] > x and k != 0:
count += x
k -= 1
n -= 2
else:
count += clear[n]
n -= 1
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for i in range(int(input())):
n, k, x = map(int, input().split())
a = [int(j) for j in input().split()]
a.sort(reverse=True)
ans, j = 0, 0
while j < len(a):
if j + 1 < len(a) and a[j] + a[j + 1] >= x and k > 0:
ans += x
j += 2
k -= 1
else:
ans += a[j]
j += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
d = dict()
l = [int(i) for i in input().split()]
l = sorted(l)
ans = 0
while len(l) > 1:
f = 0
if len(l) > 1 and l[-1] + l[-2] > x and k:
ans += x
f = 1
l = l[:-2]
k -= 1
if f == 0:
ans += l[-1]
l = l[:-1]
print(ans + sum(l))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def solveF(N, K, X, A):
A.sort()
index = 0
single_remove = len(A) - 2 * K
cost = 0
while index < single_remove:
cost += A[index]
index += 1
if (len(A) - index) % 2:
cost += A[index]
index += 1
while index <= N - 2 and A[index] + A[index + 1] < X:
cost += A[index] + A[index + 1]
index += 2
cost += X * (len(A) - index) // 2
return cost
for _ in range(int(input())):
N, K, X = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
print(solveF(N, K, X, A))
|
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
j = n - 1
a.sort()
j = n - 1
ans = 0
for i in range(k):
q = a[j] + a[j - 1]
if q < x:
break
else:
ans += x
j -= 2
while j != -1:
ans += a[j]
j -= 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = sorted(list(map(int, input().split())))
s = 0
for i in range(n):
if i == n - 2 * k:
break
s += arr[i]
else:
i += 1
print(sum([min(x, arr[i] + arr[i + 1]) for i in range(i, n, 2)]) + s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for i in range(t):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
ans = 0
while k and len(a) > 1 and a[-1] + a[-2] > x:
ans += x
k -= 1
a.pop(-1)
a.pop(-1)
ans += sum(a)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
i = len(arr) - 1
j = len(arr) - 2
cost = 0
if k == 0:
print(sum(arr))
else:
while k > 0:
if arr[i] + arr[j] > x:
cost += x
else:
break
i -= 2
j -= 2
k -= 1
for i in range(i + 1):
cost += arr[i]
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
l.reverse()
i, j = 0, 1
cs, ans = sum(l), sum(l)
while k > 0:
p = l[i] + l[i + 1]
cs -= p
if j * x + cs < ans:
ans = j * x + cs
i += 2
j += 1
k -= 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
cost = 0
length = n
while length:
if length >= 2 and arr[-1] + arr[-2] >= x and k > 0:
length -= 2
cost += x
arr = arr[0 : len(arr) - 2]
k -= 1
elif length >= 1:
length -= 1
cost += arr[-1]
arr = arr[0 : len(arr) - 1]
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cost = 0
i = n - 1
while k > 0 and i > 0:
if a[i] + a[i - 1] >= x:
k -= 1
cost += x
else:
break
i -= 2
for j in range(0, i + 1):
cost += a[j]
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
import sys
for _ in range(int(sys.stdin.readline())):
n, k, x = map(int, sys.stdin.readline().split())
l = list(map(int, sys.stdin.readline().split()))
new = sorted(l, reverse=True)
ans = 0
f = 0
if n % 2:
f = 1
n -= 1
for i in range(0, n, 2):
if k == 0:
for i in range(i, n):
ans += new[i]
break
if new[i] + new[i + 1] <= x:
ans += new[i] + new[i + 1]
else:
ans += x
k -= 1
if f == 1:
ans += new[-1]
print(ans)
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
ans = 0
a = sorted(arr)
ind = len(a) - 1
while k > 0 and len(a) > 1:
m = len(a)
sum1 = a[m - 1] + a[m - 2]
if x <= sum1:
a.pop(m - 1)
a.pop(m - 2)
ans += x
k -= 1
else:
break
ans += sum(a[:])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
l.sort(reverse=True)
i, s = 0, 0
while i < n - 1 and k != 0:
if l[i] > x:
s += x
k -= 1
i += 2
elif l[i] + l[i + 1] > x:
s += x
k -= 1
i += 2
else:
break
s += sum(l[i:])
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
s = 0
sss = 0
for i in range(0, 2 * k, 2):
sss += l[i] + l[i + 1]
if l[i] + l[i + 1] > x:
s += x
else:
sss -= l[i] + l[i + 1]
break
ss = sum(l)
print(ss - sss + s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for i in range(int(input())):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=1)
i = 0
ans = 0
while i + 1 < n and a[i] + a[i + 1] > x and k > 0:
ans += x
i += 2
k -= 1
if k == 0:
break
if i == n:
print(ans)
else:
print(ans + sum(a[i:]))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
if k == 0:
print(sum(l))
continue
l.sort()
ll = l[-2 * k :]
total = sum(l[: -2 * k])
for i in range(0, len(ll) - 1, 2):
if ll[i] + ll[i + 1] < x:
total += ll[i] + ll[i + 1]
else:
total += x
print(total)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def solution(arr, k, x):
if k == 0:
return sum(arr)
min_cost = 0
for i in range(k):
if len(arr) < 2:
return sum(arr) + min_cost
S = arr[-1]
arr.pop()
S += arr[-1]
arr.pop()
if S < x:
return min_cost + S + sum(arr)
min_cost += x
return min_cost + sum(arr)
for i in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
print(solution(arr, k, x))
|
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
cost = sum(arr[: n - 2 * k])
arr = arr[n - 2 * k :]
for i in range(k):
cost += min(arr[2 * i] + arr[2 * i + 1], x)
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def solve():
n, k, x = [int(i) for i in input().split(" ")]
arr = [int(i) for i in input().split(" ")]
arr.sort(reverse=True)
sol = 0
for i in range(0, n, 2):
if k <= 0:
ind = i
break
if arr[i] + arr[i + 1] > x:
sol += x
k -= 1
ind = i + 2
else:
ind = i
break
sol += sum(arr[ind:])
print(sol)
return
t = int(input())
for tests in range(t):
solve()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
s = 0
for i in range(n):
if i == n - 2 * k:
break
s += arr[i]
else:
i += 1
pairDel = 0
for i in range(i, n, 2):
if arr[i] + arr[i + 1] > x:
pairDel += x
else:
pairDel += arr[i] + arr[i + 1]
print(pairDel + s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = map(int, input().split())
l = list(map(int, input().split()))
if k == 0:
print(sum(l))
continue
l = sorted(l, reverse=True)
ans = 0
i = 0
while i < len(l) - 1 and k > 0:
if l[i] + l[i + 1] > x:
ans += x
l[i] = 0
l[i + 1] = 0
i += 2
k -= 1
else:
break
ans += sum(l)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cost = 0
i = 0
while k > 0 and len(a) > 1:
i = len(a)
if a[i - 1] + a[i - 2] >= x:
cost += x
a.pop(-1)
a.pop(-1)
k -= 1
else:
break
cost += sum(a)
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
flag = 1
i = 0
ans = 0
while i < n:
if i + 1 < n and k > 0 and flag:
if a[i] + a[i + 1] > x:
ans += x
i += 2
k -= 1
else:
flag = 0
ans += a[i]
i += 1
else:
ans += a[i]
i += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
N, K, X = [int(x) for x in input().split()]
sequence = [int(x) for x in input().split()]
sequence.sort()
cost = sum(sequence[0 : N - 2 * K])
if K > 0:
for i in range(N - 2 * K, N, 2):
total = sequence[i] + sequence[i + 1]
cost += min(total, X)
else:
cost += sum(sequence[N - 2 * K :])
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
import sys
def yn(val):
re = ["Yes", "No"]
return re[val]
def Ajay():
aj = chr(65) + chr(74) + chr(64) + chr(165)
return aj
def main():
a, b, c = map(int, sys.stdin.readline().split())
arr = list(map(int, input().split()))
if b == 0:
return sum(arr)
arr.sort(reverse=True)
ans = 0
for i in range(0, b * 2, 2):
if i + 1 < a:
ans += min(c, arr[i] + arr[i + 1])
ans += sum(arr[b * 2 :])
return ans
t = int(sys.stdin.readline())
for _ in range(t):
print(main())
|
IMPORT FUNC_DEF ASSIGN VAR LIST STRING STRING RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort()
cost = 0
a = len(l)
d = a - 2 * k
i = 0
while i < a:
if i < d:
cost += l[i]
i += 1
elif l[i] + l[i + 1] < x:
cost += l[i] + l[i + 1]
i += 2
k -= 1
else:
cost += x * k
break
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def prog_name():
n, k, const = map(int, input().split())
l = list(map(int, input().split()))
if k == 0:
print(sum(l))
else:
l.sort()
cost = 0
l.reverse()
ind = 0
while k > 0:
k -= 1
cost += min(l[ind] + l[ind + 1], const)
ind += 2
cost += sum(l[ind:])
print(cost)
T = int(input())
for unique in range(T):
prog_name()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
change = 0
remove = 0
for i in range(0, n, 2):
if k > 0:
if arr[i] + arr[i + 1] > x:
change += x
remove += arr[i] + arr[i + 1]
k -= 1
else:
break
else:
break
print(sum(arr) - remove + change)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def solve(array, k, sub):
array.sort()
cost = 0
index = len(array) - 1
while index >= 0:
if k > 0 and index > 0 and sub <= array[index] + array[index - 1]:
cost += sub
index -= 2
k -= 1
else:
cost += sum(array[: index + 1])
break
return cost
test = int(input())
for _ in range(test):
n, k, xx = [int(x) for x in input().split()]
array = [int(x) for x in input().split()]
print(solve(array, k, xx))
|
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
N, K, X = map(int, input().split())
A = list(map(int, input().split()))
cost = 0
done = 1
A.sort()
while len(A) > 2 * K:
cost += A[0]
A.pop(0)
if K == 0:
print(cost)
else:
while A:
if A[0] + A[1] < X:
cost += A[0] + A[1]
A.pop(0)
A.pop(0)
else:
cost += X
A.pop(0)
A.pop(0)
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
T = int(input())
for t in range(T):
n, k, x = [int(i) for i in str(input()).split()]
arr = [int(i) for i in str(input()).split()]
arr = sorted(arr, reverse=True)
final = 0
idx = 0
while True:
if arr[idx] + arr[idx + 1] > x and k > 0:
final += x
k -= 1
idx += 2
else:
final += arr[idx] + arr[idx + 1]
idx += 2
if idx == len(arr):
break
if idx == len(arr) - 1:
final += arr[idx]
break
print(final)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
T = int(input())
for t in range(T):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
pair = 0
ts = 0
s = 0
i = n - 1
while pair < k and i >= 0:
if arr[i] + arr[i - 1] >= x:
s += x
pair += 1
i -= 2
else:
break
rest = sum(arr[: i + 1])
print(rest + s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for i in range(t):
n, k, x = map(int, input().split())
lis = list(map(int, input().split()))
lis.sort(reverse=True)
j = 0
sumi = 0
while j < n:
if k == 0:
break
if j + 1 < n:
temp = lis[j] + lis[j + 1]
if temp > x:
sumi += x
j += 2
k = k - 1
else:
break
z = lis[j:]
sumi += sum(z)
print(sumi)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
cost = 0
while k > 0 and len(arr) > 1:
sumc = arr[-1] + arr[-2]
if sumc >= x:
arr.pop(-1)
arr.pop(-1)
cost += x
k -= 1
else:
break
cost += sum(arr)
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
t = int(input())
for _ in range(t):
n, k, x = map(int, input().split())
arr = list(map(int, input().split()))
sum = 0
arr.sort(reverse=True)
pos = -1
for j in range(0, n - 1, 2):
if k == 0:
break
s = arr[j] + arr[j + 1]
sum += min(s, x)
k -= 1
pos = j + 1
for j in range(pos + 1, n):
sum += arr[j]
print(sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
def mincost(k, l):
n = k[0]
t = k[1]
x = k[2]
cost = 0
l = sorted(l)
c = 0
i = n - 1
while c == 0 and i >= 1:
if l[i] + l[i - 1] >= x and t != 0:
cost += x
t = t - 1
i = i - 2
else:
c += 1
if i == -1:
return cost
for m in range(i + 1):
cost += l[m]
return cost
t = int(input())
for i in range(t):
k = list(map(int, input().split()))
l = list(map(int, input().split()))
print(mincost(k, l))
|
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array A of N elements. You can do the following operations on that array:
Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element).
Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left.
If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost.
You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above).
NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language.
------ Input Format ------
- The first line of the input contains T - the number of test cases. Then the test cases follow.
- Each test case contains 2 lines of input.
- The first line of each test case contains three integers: N, K, and X separated by spaces.
- The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}.
------ Output Format ------
For each test case, output on one line the minimum cost to clear the array.
------ Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 5000$
$0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
5 2 7
9 10 11 12 13
5 0 7
9 9 9 9 9
5 2 7
9 1 2 3 10
----- Sample Output 1 ------
23
45
13
----- explanation 1 ------
For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array.
The total cost is therefore $23$, which is the minimum cost possible.
|
for _ in range(int(input())):
n, k, x = map(int, input().split())
l = sorted(list(map(int, input().split())), reverse=True)
cost = i = 0
while True:
if i == len(l) - 1:
cost += l[i]
break
elif i >= len(l):
break
elif l[i] + l[i + 1] > x and k > 0:
cost += x
k -= 1
elif l[i] + l[i + 1] <= x or k == 0:
cost += l[i] + l[i + 1]
i += 2
print(cost)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.