description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on. <image> Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland. The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed. Output Output a single number — the minimum complexity of the garland. Examples Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 Note In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) p = list(map(int, input().split())) inf = 10**12 dp = [[[inf, inf] for j in range(101)] for i in range(n)] if p[0] != 0: if p[0] % 2 == 0: dp[0][1][0] = 0 else: dp[0][0][1] = 0 else: dp[0][1][0] = 0 dp[0][0][1] = 0 for i in range(1, n): for j in range(101): for k in range(2): if dp[i - 1][j][k] == inf: continue if p[i] != 0: if p[i] % 2 == 0: if k == 0: dp[i][j + 1][0] = min(dp[i - 1][j][k], dp[i][j + 1][0]) else: dp[i][j + 1][0] = min(dp[i - 1][j][k] + 1, dp[i][j + 1][0]) elif k == 0: dp[i][j][1] = min(dp[i - 1][j][k] + 1, dp[i][j][1]) else: dp[i][j][1] = min(dp[i - 1][j][k], dp[i][j][1]) else: if k == 0: dp[i][j + 1][0] = min(dp[i - 1][j][k], dp[i][j + 1][0]) else: dp[i][j + 1][0] = min(dp[i - 1][j][k] + 1, dp[i][j + 1][0]) if k == 0: dp[i][j][1] = min(dp[i - 1][j][k] + 1, dp[i][j][1]) else: dp[i][j][1] = min(dp[i - 1][j][k], dp[i][j][1]) print(min(dp[-1][n // 2]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
INF = 9999999999999999999999 n = int(input()) a = list(map(int, input().split())) e = 0 o = 0 pref = [(0) for i in range(n)] for i in range(n): pref[i] = pref[i - 1] if a[i] == 0: pref[i] += 1 continue if a[i] % 2 == 0: e += 1 else: o += 1 ie = n // 2 io = ie if n % 2 == 1: io += 1 e = ie - e o = io - o dp = [ [[[INF for i in range(o + 1)] for j in range(e + 1)] for k in range(n)] for l in range(2) ] for i in range(n): jCap = min([pref[i], e]) for j in range(jCap + 1): k = pref[i] - j if k <= o: if a[i] != 0: if i == 0: if a[0] % 2 == 0: dp[0][i][j][k] = 0 else: dp[1][i][j][k] = 0 elif a[i] % 2 == 0: dp[0][i][j][k] = min([dp[0][i - 1][j][k], 1 + dp[1][i - 1][j][k]]) else: dp[1][i][j][k] = min([dp[1][i - 1][j][k], 1 + dp[0][i - 1][j][k]]) elif i == 0: if j == 1: dp[0][i][j][k] = 0 else: dp[1][i][j][k] = 0 else: if j > 0: dp[0][i][j][k] = 1 + dp[1][i - 1][j - 1][k] dp[0][i][j][k] = min([dp[0][i][j][k], dp[0][i - 1][j - 1][k]]) if k > 0: dp[1][i][j][k] = 1 + dp[0][i - 1][j][k - 1] dp[1][i][j][k] = min([dp[1][i][j][k], dp[1][i - 1][j][k - 1]]) print(min([dp[0][n - 1][e][o], dp[1][n - 1][e][o]]))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR LIST VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR LIST VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys N = int(input()) a = [int(x) for x in input().split(" ")] if all(x == 0 for x in a): print(0 if N == 1 else 1) sys.exit(0) sz = [[], []] sl, sr, pl, pr = None, None, None, None cnt = [N // 2, (N + 1) // 2] flips = 0 pp, pi = None, -1 for i, x in enumerate(a): if x != 0: if pp == None: sl = i - pi - 1 pl = x % 2 elif x % 2 != pp: flips += 1 else: sz[x % 2].append(i - pi - 1) pp = x % 2 pi = i cnt[x % 2] -= 1 sz[0].sort() sz[1].sort() sr = N - pi - 1 pr = pp best_ans = 2 * N for tl in [False, True]: for tr in [False, True]: f = cnt.copy() ans = flips if tl and f[pl] >= sl: f[pl] -= sl else: ans += 1 if tr and f[pr] >= sr: f[pr] -= sr else: ans += 1 for x in range(2): take = 0 for y in sz[x]: if f[x] >= y: f[x] -= y take += 1 else: break ans += 2 * (len(sz[x]) - take) best_ans = min(best_ans, ans) print(best_ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST LIST ASSIGN VAR VAR VAR VAR NONE NONE NONE NONE ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NONE NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR LIST NUMBER NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N = int(input()) P = list(map(int, input().split())) if N == 1: print(0) exit() if N == 2: print(1) exit() odd = (N + 1) // 2 even = N // 2 for p in P: if p: if p & 1: odd -= 1 else: even -= 1 inf = 10000 dp0 = [[([inf] * N) for _ in range(N)] for _ in range(N)] dp1 = [[([inf] * N) for _ in range(N)] for _ in range(N)] if P[0]: if P[0] & 1: dp1[0][odd][even] = 0 else: dp0[0][odd][even] = 0 else: dp1[0][odd - 1][even] = 0 dp0[0][odd][even - 1] = 0 for i in range(1, N): for o in range(odd + 1): for e in range(even + 1): if P[i]: if P[i] & 1: dp1[i][o][e] = min(dp1[i - 1][o][e], dp0[i - 1][o][e] + 1) else: dp0[i][o][e] = min(dp1[i - 1][o][e] + 1, dp0[i - 1][o][e]) else: if o: dp1[i][o - 1][e] = min(dp1[i - 1][o][e], dp0[i - 1][o][e] + 1) if e: dp0[i][o][e - 1] = min(dp1[i - 1][o][e] + 1, dp0[i - 1][o][e]) print(min(dp0[-1][0][0], dp1[-1][0][0])) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) pis = [int(pi) for pi in input().split()] prev, current_gap, odd, even = "d", 0, 0, 0 first_gap = "e", 0 even_gaps, odd_gaps, any_gaps = [], [], [] for pi in pis: if pi == 0: current_gap += 1 elif pi % 2 == 0: even += 1 if prev == "o": any_gaps.append(current_gap) elif prev == "e": even_gaps.append(current_gap) else: first_gap = "e", current_gap prev = "e" current_gap = 0 else: odd += 1 if prev == "e": any_gaps.append(current_gap) elif prev == "o": odd_gaps.append(current_gap) else: first_gap = "o", current_gap prev = "o" current_gap = 0 if prev == "d": first_gap = "o", current_gap ans = 0 odd_gaps.sort() even_gaps.sort() any_gaps.sort() total_even = n // 2 total_odd = n - total_even rem_odd = total_odd - odd rem_even = total_even - even for gap in odd_gaps: if rem_odd >= gap: rem_odd -= gap else: ans += 2 for gap in even_gaps: if rem_even >= gap: rem_even -= gap else: ans += 2 if current_gap > 0: if prev == "o": if current_gap > rem_odd: ans += 1 else: rem_odd -= current_gap elif prev == "e": if current_gap > rem_even: ans += 1 else: rem_even -= current_gap if first_gap[0] == "o" and first_gap[1] > rem_odd: ans += 1 if first_gap[0] == "e" and first_gap[1] > rem_even: ans += 1 for gap in any_gaps: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR STRING VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR STRING IF VAR VAR VAR NUMBER VAR VAR IF VAR STRING IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER STRING VAR NUMBER VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys def updiv(x, y): if x % y == 0: return x // y else: return x // y + 1 n = int(input()) p = list(map(int, input().split())) if n == 1: print(0) sys.exit() o = updiv(n, 2) e = n // 2 for i in range(n): if p[i] > 0 and p[i] % 2 == 0: e -= 1 p[i] = 2 elif p[i] > 0 and p[i] % 2 == 1: o -= 1 p[i] = 1 ans = 0 for i in range(n - 1): if p[i] == 1 and p[i + 1] == 2 or p[i] == 2 and p[i + 1] == 1: ans += 1 flag = False p.append(float("inf")) oddone = [] eveone = [] inodd = [] ineve = [] for i in range(n + 1): if not flag and p[i] == 0: l = i flag = True if flag and p[i] != 0: r = i if l == 0: if p[r] == 1: oddone.append(r - l) else: eveone.append(r - l) elif r == n: if p[l - 1] == 1: oddone.append(r - l) else: eveone.append(r - l) elif p[l - 1] == 1 and p[r] == 1: inodd.append(r - l) elif p[l - 1] == 2 and p[r] == 2: ineve.append(r - l) else: ans += 1 flag = False ableodd = len(inodd) * 2 + len(oddone) ableeve = len(ineve) * 2 + len(eveone) inodd.sort() ineve.sort() oddone.sort() eveone.sort() for i in range(len(inodd)): if o >= inodd[i]: o -= inodd[i] ableodd -= 2 else: break for i in range(len(oddone)): if o >= oddone[i]: o -= oddone[i] ableodd -= 1 else: break for i in range(len(ineve)): if e >= ineve[i]: e -= ineve[i] ableeve -= 2 else: break for i in range(len(eveone)): if e >= eveone[i]: e -= eveone[i] ableeve -= 1 else: break print(ans + ableodd + ableeve)
IMPORT FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) a = [int(x) for x in input().split()] imp, par = 0, 0 for i in range(n): if a[i] == 0: a[i] -= 1 else: a[i] %= 2 imp += a[i] % 2 == 1 par += a[i] % 2 == 0 ans = 0 for i in range(n - 1): if a[i] != -1 and a[i + 1] != -1 and a[i] + a[i + 1] == 1: ans += 1 b0, b1, b00, b11, irest = [], [], [], [], 0 if a[0] == -1: i = 0 while i < n: if a[i] != -1: b0.append(i) if a[i] == 0 else b1.append(i) break i += 1 if a[-1] == -1: i = n - 1 while i >= 0: if a[i] != -1: b0.append(n - 1 - i) if a[i] == 0 else b1.append(n - 1 - i) break i -= 1 lastfill = -1 for i in range(n): if a[i] != -1: if lastfill != -1 and i - lastfill > 1: if a[i] == 0 and a[lastfill] == 0: b00.append(i - lastfill - 1) elif a[i] == 1 and a[lastfill] == 1: b11.append(i - lastfill - 1) else: irest += 1 lastfill = i restpar = n // 2 - par restimpar = n - n // 2 - imp b0.sort(reverse=True) b00.sort(reverse=True) b1.sort(reverse=True) b11.sort(reverse=True) ans += irest while b00 and b00[-1] <= restpar: restpar -= b00.pop() while b11 and b11[-1] <= restimpar: restimpar -= b11.pop() while b0 and b0[-1] <= restpar: restpar -= b0.pop() while b1 and b1[-1] <= restimpar: restimpar -= b1.pop() ans += len(b0) + len(b1) + 2 * len(b11) + 2 * len(b00) print(1) if par + imp == 0 and n > 1 else print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR LIST LIST LIST LIST NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER EXPR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER EXPR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR BIN_OP VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys n = int(input()) arr = list(map(int, input().split())) nope = [] for i in range(1, n + 1): if i not in arr: nope.append(i) chet = 0 nechet = 0 for i in nope: if i % 2 == 0: chet += 1 else: nechet += 1 ans = 0 for i in range(n - 1): if arr[i] != 0 and arr[i + 1] != 0 and arr[i] % 2 != arr[i + 1] % 2: ans += 1 groups = [] left = -1 right = -1 for i in range(n): if arr[i] == 0: right = i if left == -1: left = i else: if right == -1: continue if left == 0 or right == n - 1: groups.append((-1, left, right)) elif arr[left - 1] % 2 == arr[right + 1] % 2: groups.append((arr[left - 1] % 2, left, right)) else: ans += 1 left = -1 right = -1 if right != -1: if left == 0 or right == n - 1: groups.append((-1, left, right)) elif arr[left - 1] % 2 == arr[right + 1] % 2: groups.append((arr[left - 1] % 2, left, right)) else: ans += 1 groups.sort(key=lambda x: (-x[0], x[2] - x[1])) for group in groups: key = group[0] left = group[1] right = group[2] cost = right - left + 1 if key == 1: if nechet >= cost: nechet -= cost else: ans += 2 elif key == 0: if chet >= cost: chet -= cost else: ans += 2 else: if left == 0 and right == n - 1: if left == right: print(0) else: print(1) sys.exit() if left == 0: x = arr[right + 1] if x % 2 == 0: if chet >= right + 1: chet -= right + 1 else: ans += 1 elif nechet >= right + 1: nechet -= right + 1 else: ans += 1 else: x = arr[left - 1] if x % 2 == 0: if chet >= n - left: chet -= n - left else: ans += 1 elif nechet >= n - left: nechet -= n - left else: ans += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys inf = 2**30 def solution(a): n = len(a) m = (n + 1 >> 1) - sum([(v & 1) for v in a]) f = [[[inf for k in range(2)] for j in range(m + 1)] for i in range(n + 1)] f[0][0][0], f[0][0][1] = 0, 0 for i in range(n): for j in range(min(m, i) + 1): for k in range(2): if a[i] != 0: t = a[i] & 1 w = 1 if t != k else 0 f[i + 1][j][t] = min(f[i][j][k] + w, f[i + 1][j][t]) continue if j < m: w = 1 if k != 1 else 0 f[i + 1][j + 1][1] = min(f[i][j][k] + w, f[i + 1][j + 1][1]) w = 1 if k != 0 else 0 f[i + 1][j][0] = min(f[i][j][k] + w, f[i + 1][j][0]) return min(f[n][m][0], f[n][m][1]) def main(): while True: try: n = int(input().strip()) a = [int(i) for i in input().strip().split()] sys.stdout.write("%d\n" % solution(a)) except ValueError: continue except EOFError: break main()
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) p = list(map(int, input().split())) odd = (n + 1) // 2 memo = {} def best(i, odd_count, last): if i == n: return 0 key = i, odd_count, last if key in memo: return memo[key] poss = range(2) if p[i] == 0 else [p[i] % 2] res = 10**9 for parity in poss: new_odd_count = odd_count - parity new_even_count = n - i - odd_count - (parity ^ 1) if new_odd_count < 0 or new_even_count < 0: continue res = min( res, best(i + 1, new_odd_count, parity) + (1 if i > 0 and last != parity else 0), ) memo[key] = res return res print(best(0, odd, 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 BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER LIST BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) bulb = list(map(int, input().split())) def complex(n, bulb): temp = 0 for x in range(n - 1): if ( bulb[x] % 2 == 0 and bulb[x + 1] % 2 != 0 and bulb[x] != 0 and bulb[x + 1] != 0 ): temp += 1 elif ( bulb[x] % 2 != 0 and bulb[x + 1] % 2 == 0 and bulb[x] != 0 and bulb[x + 1] != 0 ): temp += 1 return temp def compareEvenOrOdd(num, even, odd): if num % 2 == 0: even -= 1 else: odd -= 1 return even, odd def cal_complex(temp1, temp2, dic, complexs, k): if temp1 - dic >= 0: temp1 = temp1 - dic else: temp2 = temp2 - (dic - temp1) temp1 = 0 complexs = complexs + k return temp1, temp2, complexs def sorting(n, bulb): even = n // 2 odd = n - even dictsort = {} i = 0 complexs = complex(n, bulb) while i < n: if bulb[i] != 0 and i < n - 1: if bulb[i + 1] == 0: dictsort[i] = 0 even, odd = compareEvenOrOdd(bulb[i], even, odd) j = i i += 1 while i < n and bulb[i] == 0: dictsort[j] += 1 i += 1 else: even, odd = compareEvenOrOdd(bulb[i], even, odd) i += 1 elif bulb[i] != 0: even, odd = compareEvenOrOdd(bulb[i], even, odd) i += 1 elif bulb[0] == 0: dictsort[0] = 1 j = i i += 1 while i < n and bulb[i] == 0: dictsort[j] += 1 i += 1 dictsort = sorted(dictsort.items(), key=lambda x: x[1]) specialodd = [] specialeven = [] if len(dictsort) > 0 and dictsort[0][1] == n and n > 1: print(1) elif n <= 1: print(0) else: for i in range(len(dictsort)): first = dictsort[i][0] last = dictsort[i][0] + dictsort[i][1] + 1 if bulb[first] != 0 and last < n: if bulb[first] % 2 == 0 and bulb[last] % 2 == 0: if even - dictsort[i][1] < 0 and len(specialeven) > 0: even, odd, complexs = cal_complex( even, odd, specialeven[0][1], complexs, 1 ) specialeven.remove(specialeven[0]) even, odd, complexs = cal_complex( even, odd, dictsort[i][1], complexs, 2 ) elif bulb[first] % 2 != 0 and bulb[last] % 2 != 0: if odd - dictsort[i][1] < 0 and len(specialodd) > 0: odd, even, complexs = cal_complex( odd, even, specialodd[0][1], complexs, 1 ) specialodd.remove(specialodd[0]) odd, even, complexs = cal_complex( odd, even, dictsort[i][1], complexs, 2 ) else: complexs = complexs + 1 elif bulb[first] == 0: if bulb[last - 1] % 2 == 0: specialeven.append(dictsort[i]) else: specialodd.append(dictsort[i]) elif bulb[first] % 2 == 0: specialeven.append(dictsort[i]) else: specialodd.append(dictsort[i]) for x in specialodd: odd, even, complexs = cal_complex(odd, even, x[1], complexs, 1) for x in specialeven: even, odd, complexs = cal_complex(even, odd, x[1], complexs, 1) print(complexs) sorting(n, bulb)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys input = sys.stdin.readline n = int(input()) A = list(map(int, input().split())) DP0 = [([200] * (n + 1)) for i in range(n + 1)] DP1 = [([200] * (n + 1)) for i in range(n + 1)] DP0[0][0] = 0 DP1[0][0] = 0 for i in range(n): SUM = i + 1 if A[i] != 0: if A[i] % 2 == 1: for j in range(1, SUM + 1): DP1[j][SUM - j] = min(DP0[j - 1][SUM - j] + 1, DP1[j - 1][SUM - j]) else: for j in range(1, SUM + 1): DP0[SUM - j][j] = min(DP0[SUM - j][j - 1], DP1[SUM - j][j - 1] + 1) else: for j in range(1, SUM + 1): DP1[j][SUM - j] = min(DP0[j - 1][SUM - j] + 1, DP1[j - 1][SUM - j]) for j in range(1, SUM + 1): DP0[SUM - j][j] = min(DP0[SUM - j][j - 1], DP1[SUM - j][j - 1] + 1) ANS = min(DP0[n - n // 2][n // 2], DP1[n - n // 2][n // 2]) print(ANS)
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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() a = list(mints()) o_all = 0 for i in range(1, n + 1, 2): o_all += 1 e_all = 0 for i in range(2, n + 1, 2): e_all += 1 dp = [None] * n for i in range(n): z = [None] * (n + 1) for j in range(n + 1): z[j] = [1000000000.0, 1000000000.0] dp[i] = z if a[0] == 0: dp[0][1][0] = 0 dp[0][0][1] = 0 elif a[0] % 2 == 0: dp[0][1][0] = 0 else: dp[0][0][1] = 0 for i in range(0, n - 1): for e in range(0, i + 2): o = i + 1 - e el = e_all - e ol = o_all - o for jp in range(2): for j in range(2): if j == 0 and el > 0 or j == 1 and ol > 0: if a[i + 1] == 0: dp[i + 1][e + 1 - j][j] = min( dp[i + 1][e + 1 - j][j], dp[i][e][jp] + (jp != j) ) elif a[i + 1] % 2 == j: dp[i + 1][e + 1 - j][j] = min( dp[i + 1][e + 1 - j][j], dp[i][e][jp] + (jp != j) ) print(min(dp[n - 1][e_all][0], dp[n - 1][e_all][1])) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) x = list(map(int, input().split())) p1, p2, c = [0] * 101, [0] * 101, [0] * 2 c[1], c[0] = n // 2 + n % 2, n // 2 count, edge, ans, temp1, temp2 = 0, [[] for i in range(2)], 0, 0, 0 for i in range(n): if x[i] == 0: count += 1 if i == n - 1: edge[x[i - count] % 2].append(count) ans += 1 if count == n: if n == 1: ans = 0 break else: if count > 0 and i - count - 1 >= 0 and x[i] % 2 == x[i - count - 1] % 2 == 1: p1[count] += 1 elif count > 0 and i - count - 1 >= 0 and x[i] % 2 == x[i - count - 1] % 2 == 0: p2[count] += 1 elif count > 0 and i - count == 0: edge[x[i] % 2].append(count) ans += 1 elif i > 0 and (count > 0 or x[i] % 2 != x[i - 1] % 2): ans += 1 count = 0 c[x[i] % 2] -= 1 for i in range(1, 100): temp1 = min(p1[i], c[1] // i) p1[i] -= temp1 c[1] -= i * temp1 temp2 = min(p2[i], c[0] // i) p2[i] -= temp2 c[0] -= i * temp2 ans += p1[i] * 2 + p2[i] * 2 for i in range(2): for i2 in range(len(edge[i])): if c[i] >= edge[i][i2]: ans -= 1 c[i] -= edge[i][i2] 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 VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER LIST VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") sys.setrecursionlimit(10**9) INF = 10**18 MOD = 10**9 + 7 N = INT() A = LIST() A = [(a % 2 if a != 0 else -1) for a in A] M = ceil(N, 2) dp = list3d(N + 1, M + 1, 2, INF) dp[0][0][0] = dp[0][0][1] = 0 for i in range(N): a = A[i] for j in range(M + 1): for k in range(2): if (a == 1 or a == -1) and j + 1 <= M: dp[i + 1][j + 1][1] = min(dp[i + 1][j + 1][1], dp[i][j][k] + (1 ^ k)) if a == 0 or a == -1: dp[i + 1][j][0] = min(dp[i + 1][j][0], dp[i][j][k] + (0 ^ k)) ans = min(dp[N][M][0], dp[N][M][1]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) arr = list(map(lambda x: int(x), input().split(" "))) count_ans = 0 arr_1 = [] for i in range(n - 1): if (arr[i] + arr[i + 1]) % 2 == 1 and arr[i] != 0 and arr[i + 1] != 0: count_ans += 1 prev = -1 count = 0 count_odd = 0 count_even = 0 for i in range(n): if arr[i] % 2 == 1: count_odd += 1 if arr[i] % 2 == 0 and arr[i] != 0: count_even += 1 if arr[i] == 0: count += 1 elif arr[i] != 0: if count != 0: arr_1.append([count, prev, arr[i]]) prev = arr[i] count = 0 flag = False if count_odd == 0 and count_even == 0: flag = True if count != 0: arr_1.append([count, prev, -1]) t_c_odd = n - n // 2 t_c_even = n // 2 a_count_odd = t_c_odd - count_odd a_count_even = t_c_even - count_even arr_odd = [] arr_even = [] arr_later = [] for i in arr_1: if (i[1] + i[2]) % 2 == 0 and i[1] != -1 and i[2] != -1 and i[1] % 2 == 1: arr_odd.append(i) elif (i[1] + i[2]) % 2 == 0 and i[1] != -1 and i[2] != -1 and i[1] % 2 == 0: arr_even.append(i) if (i[1] + i[2]) % 2 == 1 and i[1] != -1 and i[2] != -1: count_ans += 1 if i[1] == -1 or i[2] == -1: arr_later.append(i) arr_odd.sort() arr_even.sort() pre_odd = [] pre_even = [] for i in arr_odd: if len(pre_odd) == 0: pre_odd.append(i[0]) else: pre_odd.append(pre_odd[-1] + i[0]) for i in arr_even: if len(pre_even) == 0: pre_even.append(i[0]) else: pre_even.append(pre_even[-1] + i[0]) if len(pre_even) > 0: if pre_even[-1] <= a_count_even: a_count_even = a_count_even - pre_even[-1] elif pre_even[0] > a_count_even: a_count_even = a_count_even count_ans += 2 * len(pre_even) else: for i in range(len(pre_even) - 1): if pre_even[i] <= a_count_even and a_count_even < pre_even[i + 1]: count_ans += 2 * (len(pre_even) - i - 1) a_count_even = a_count_even - pre_even[i] if len(pre_odd) > 0: if pre_odd[-1] <= a_count_odd: a_count_odd = a_count_odd - pre_odd[-1] elif pre_odd[0] > a_count_odd: a_count_odd = a_count_odd count_ans += 2 * len(pre_odd) else: for i in range(len(pre_odd) - 1): if pre_odd[i] <= a_count_odd and a_count_odd < pre_odd[i + 1]: count_ans += 2 * (len(pre_odd) - i - 1) a_count_odd = a_count_odd - pre_odd[i] for i in arr_later: if i[1] == -1: if i[2] % 2 == 0 and a_count_even < i[0]: count_ans += 1 elif i[2] % 2 == 1 and a_count_odd < i[0]: count_ans += 1 if i[2] == -1: if i[1] % 2 == 0 and a_count_even < i[0]: count_ans += 1 elif i[1] % 2 == 1 and a_count_odd < i[0]: count_ans += 1 if flag == True and n >= 2: print(1) else: print(count_ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys n = int(input()) ar = list(map(int, input().split())) no = set([i for i in range(1, n + 1)]) ars = set(ar) if 0 in ars: ars.remove(0) dif = no - ars odds = 0 even = 0 for i in dif: if i % 2 == 0: even += 1 odds = len(dif) - even n1 = len(dif) dp = {} def gar(i, odds, typ, n1): global dp if i == n or n == 1: return 0 if (i, odds, typ, n1) in dp: return dp[i, odds, typ, n1] val1, val2, val = sys.maxsize, sys.maxsize, sys.maxsize if ar[i] == 0 and odds > 0: val1 = gar(i + 1, odds - 1, 1, n1 - 1) if typ == 0: val1 += 1 if ar[i] == 0 and n1 - odds > 0: val2 = gar(i + 1, odds, 0, n1 - 1) if typ == 1: val2 += 1 if ar[i] != 0: val = 0 if typ != ar[i] % 2: val = gar(i + 1, odds, ar[i] % 2, n1) + 1 else: val = gar(i + 1, odds, ar[i] % 2, n1) if val >= sys.maxsize: dp[i, odds, typ, n1] = min(val1, val2) else: dp[i, odds, typ, n1] = val return dp[i, odds, typ, n1] if ar[0] == 0: if odds != 0: val = min(gar(1, odds, 0, n1 - 1), gar(1, odds - 1, 1, n1)) else: val = gar(1, odds, 0, n1) elif ar[0] % 2 == 0: val = gar(1, odds, 0, n1) else: val = gar(1, odds, 1, n1) print(val)
IMPORT 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 VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) l = list(map(int, input().split())) dp = [] ans = 0 sp1 = [0, 0] sp2 = [0, 0] if n % 2 == 0: ev = n // 2 od = n // 2 else: od = 1 + n // 2 ev = n // 2 a = -1 b = -1 c = 0 for i in range(n - 1): if l[i] != 0 and l[i + 1] != 0: if l[i] % 2 != l[i + 1] % 2: ans += 1 for i in range(n + 1): if i == n: if l[i - 1] == 0: sp2[0] = c sp2[1] = a break if l[i] == 0: c += 1 elif l[i] % 2 == 0: b = 0 ev -= 1 if c: x = [c, a, b] if a == -1: sp1[0] = c sp1[1] = b elif b == -1: sp2[0] = c sp2[1] = a else: dp.append(x) a = 0 c = 0 b = -1 else: b = 1 od -= 1 if c: if a == -1: sp1[0] = c sp1[1] = b elif b == -1: sp2[0] = c sp2[1] = a else: x = [c, a, b] dp.append(x) a = 1 c = 0 b = -1 dp = sorted(dp, key=lambda x: x[0]) if n == 1: print(0) exit() if od + ev == n: print(1) exit() for i in range(len(dp)): if dp[i][1] == dp[i][2]: if dp[i][1] == 1: if od >= dp[i][0]: od -= dp[i][0] else: ans += 2 elif ev >= dp[i][0]: ev -= dp[i][0] else: ans += 2 else: ans += 1 if sp1[0]: if sp1[1] == 1: if od >= sp1[0]: od -= sp1[0] else: ans += 1 elif ev >= sp1[0]: ev -= sp1[0] else: ans += 1 if sp2[0]: if sp2[1] == 1: if od >= sp2[0]: od -= sp2[0] else: ans += 1 elif ev >= sp2[0]: ev -= sp2[0] else: ans += 1 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 LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR LIST VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys def main(): n = int(input()) a = readIntArr() arr = [] items = set(range(1, n + 1)) for x in a: if x == 0: arr.append(-1) else: arr.append(x % 2) items.remove(x) itemModCnts = [0, 0] for x in items: itemModCnts[x % 2] += 1 dp = makeArr(inf, [n, n + 1, 2]) cumuUnknown = 0 if arr[0] == -1: dp[0][0][1] = 0 dp[0][1][0] = 0 cumuUnknown += 1 else: dp[0][0][arr[0] % 2] = 0 for i in range(1, n): if arr[i] == -1: cumuUnknown += 1 for j in range(cumuUnknown + 1): if j > 0: dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j - 1][0]) dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j - 1][1] + 1) usedOddCnts = cumuUnknown - j if usedOddCnts > 0: dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + 1) dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1]) else: for j in range(n): dp[i][j][arr[i]] = min(dp[i][j][arr[i]], dp[i - 1][j][arr[i]]) dp[i][j][arr[i]] = min(dp[i][j][arr[i]], 1 + dp[i - 1][j][arr[i] ^ 1]) ans = min(dp[n - 1][itemModCnts[0]][0], dp[n - 1][itemModCnts[0]][1]) print(ans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n = int(input()) p = list(map(int, input().split())) odd = (n + 1) // 2 even = n - odd for i in p: if i: if i % 2 == 0: even -= 1 else: odd -= 1 if even == 0: p = [(i if i else 1) for i in p] if odd * even == 0: ans = 0 prev = p[0] for pi in p: ans += (pi + prev) % 2 prev = pi print(ans) exit() DP = [([float("inf")] * (even + 1)) for i in range(2)] for i in range(n): nxt = [([float("inf")] * (even + 1)) for i in range(2)] if i == 0: if p[i]: if p[i] % 2 == 0: nxt[0][0] = 0 else: nxt[1][0] = 0 else: nxt[0][1] = 0 nxt[1][0] = 0 elif p[i]: if p[i] % 2 == 0: for j in range(even + 1): nxt[0][j] = min(DP[0][j], DP[1][j] + 1) else: for j in range(even + 1): nxt[1][j] = min(DP[0][j] + 1, DP[1][j]) else: for j in range(1, even + 1): nxt[0][j] = min(DP[0][j - 1], DP[1][j - 1] + 1) for j in range(even + 1): nxt[1][j] = min(DP[0][j] + 1, DP[1][j]) DP = nxt print(min(DP[0][-1], DP[1][-1]))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR 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 ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) z = float("INF") a = [int(x) for x in input().split()] dp = [[[([z] * 2) for k in range(n + 1)] for j in range(n + 1)] for i in range(n + 1)] dp[0][0][0][0] = 0 dp[0][0][0][1] = 0 for i in range(1, n + 1): for j in range(n + 1): if a[i - 1] & 1: dp[i][j][i - j][1] = min( dp[i - 1][j][i - j - 1][0] + 1, dp[i - 1][j][i - j - 1][1] ) elif a[i - 1] == 0: dp[i][j][i - j][1] = min( dp[i - 1][j][i - j - 1][0] + 1, dp[i - 1][j][i - j - 1][1] ) dp[i][j][i - j][0] = min( dp[i - 1][j - 1][i - j][0], dp[i - 1][j - 1][i - j][1] + 1 ) else: dp[i][j][i - j][0] = min( dp[i - 1][j - 1][i - j][0], dp[i - 1][j - 1][i - j][1] + 1 ) e = (n - (n & 1)) // 2 o = (n + (n & 1)) // 2 ans = [dp[n][e][o][0], dp[n][e][o][1]] if a[n - 1] == 0: print(min(ans)) else: print(ans[a[n - 1] & 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) x = y = 0 a = list(map(int, input().split())) pref = [0] for q in range(n): if a[q] != 0 and a[q] % 2 == 0: x += 1 elif a[q] != 0: y += 1 if a[q] % 2 == 0 and a[q] != 0: a[q] = 2 pref.append(pref[-1]) elif a[q] % 2 == 1: a[q] = 1 pref.append(pref[-1]) else: pref.append(pref[-1] + 1) k, m = n // 2 - x, (n + 1) // 2 - y d = [[[0, 0] for q in range(n + 1)]] for q in range(n): d.append([]) for q1 in range(pref[q + 1] + 1): if a[q] == 1 or a[q] == 0 and q1 == 0: d[-1].append([float("inf"), min(d[-2][q1][0] + 1, d[-2][q1][1])]) elif a[q] == 2: d[-1].append([min(d[-2][q1][0], d[-2][q1][1] + 1), float("inf")]) else: if a[q] == 0 and pref[q + 1] - q1 == 0: d[-1].append( [min(d[-2][q1 - 1][0], d[-2][q1 - 1][1] + 1), float("inf")] ) d[-1].append( [ min(d[-2][q1 - 1][0], d[-2][q1 - 1][1] + 1), min(d[-2][q1][0] + 1, d[-2][q1][1]), ] ) for q1 in range(pref[q + 1] + 1, n + 1): d[-1].append([0, 0]) print(min(d[-1][k][0], d[-1][k][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR STRING IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input().strip()) even, odd = n // 2, n // 2 + n % 2 ints = list(map(int, input().split())) prev, previ, count = None, -1, 0 complexity = 0 groups = [] for i, x in enumerate(ints): if x == 0: count += 1 else: if not prev: if count: groups.append((1, -count, x % 2)) elif prev % 2 == x % 2: if count: groups.append((2, -count, x % 2)) else: complexity += 1 if x % 2: odd -= 1 else: even -= 1 count = 0 prev = x else: if count: if prev: groups.append((1, -count, prev % 2)) elif n % 2 == 0: complexity = 1 if groups: groups.sort(reverse=True) for g in groups: if g[2]: if odd + g[1] < 0: complexity += g[0] else: odd += g[1] elif even + g[1] < 0: complexity += g[0] else: even += g[1] print(complexity)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NONE NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) p = list(map(int, input().split())) o = (n + 1) // 2 e = n // 2 for i in range(n): if p[i]: p[i] %= 2 if p[i]: o -= 1 else: e -= 1 else: p[i] = -1 l = [] if 0 not in p and 1 not in p: if n > 1: print(1) else: print(0) else: ans = 0 for i in range(1, n): if p[i] != -1 and p[i - 1] != -1: ans += p[i] != p[i - 1] while o: l = -1 r = -1 fl = -1 fr = n + 1 for i in range(1, n - 1): l, r = -1, -1 if p[i] == -1 and p[i - 1] == 1: l = i for j in range(i, n): if p[j] != -1: if p[j] == 1: r = j - 1 break if r != -1 and r - l <= fr - fl: fl = l fr = r if fl != -1: k = min(fr - fl + 1, o) if fr - fl + 1 - k: break for j in range(fl, fr + 1): p[j] = 0 o -= k else: break while e: l = -1 r = -1 fl = -1 fr = n + 1 for i in range(1, n): l, r = -1, -1 if p[i] == -1 and p[i - 1] == 0: l = i for j in range(i, n): if p[j] != -1: if p[j] == 0: r = j - 1 break if r != -1 and r - l <= fr - fl: fl = l fr = r if fl != -1: k = min(fr - fl + 1, e) if fr - fl + 1 - k: break for i in range(fl, fr + 1): p[i] = 0 e -= k else: break if p[0] == -1: i = 0 while i < n and p[i] == -1: p[i] = 0 i += 1 if p[i] == 0 and e >= i or p[i] == 1 and o >= i: if p[i] == 0: e -= i if p[i]: o -= i else: ans += 1 if p[-1] == -1: i = n - 1 while i and p[i] == -1: p[i] = 0 i -= 1 if p[i] == 0 and e >= n - i - 1 or p[i] == 1 and o >= n - i - 1: if p[i] == 0: e -= n - i - 1 if p[i]: o -= n - i - 1 else: ans += 1 for i in range(1, n - 1): r = -1 if p[i] == -1 and p[i - 1] != -1: for j in range(i, n): if p[j] != -1: r = p[j] break if r != -1: if r == p[i - 1]: ans += 2 else: ans += 1 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 BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF NUMBER VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) a = list(map(int, input().split())) INF = 10**5 dp = [[([INF] * (n + 1)) for i in range(n + 1)] for i in range(2)] dp[0][0][0] = 0 dp[1][0][0] = 0 for i in range(n): if a[i] != 0: parity = a[i] % 2 if parity % 2 == 0: for j in range(n + 1): dp[0][i + 1][j] = min(dp[0][i][j], dp[0][i + 1][j]) dp[0][i + 1][j] = min(dp[1][i][j] + 1, dp[0][i + 1][j]) if parity % 2 == 1: for j in range(n): dp[1][i + 1][j + 1] = min(dp[1][i][j], dp[1][i + 1][j + 1]) dp[1][i + 1][j + 1] = min(dp[0][i][j] + 1, dp[1][i + 1][j + 1]) else: for j in range(n + 1): dp[0][i + 1][j] = min(dp[0][i][j], dp[0][i + 1][j]) dp[0][i + 1][j] = min(dp[1][i][j] + 1, dp[0][i + 1][j]) for j in range(n): dp[1][i + 1][j + 1] = min(dp[1][i][j], dp[1][i + 1][j + 1]) dp[1][i + 1][j + 1] = min(dp[0][i][j] + 1, dp[1][i + 1][j + 1]) odd_cnt = (n + 1) // 2 even_cnt = n // 2 print(min(dp[1][n][odd_cnt], dp[0][n][odd_cnt]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(0) exit(0) dp = [[[10**9, 10**9] for i in range(n + 1)] for j in range(n + 1)] dp[0][0][0] = 0 dp[0][0][1] = 0 even, odd = 0, 0 for i in range(1, n + 1): for j in range(1, n + 1): if a[i - 1] == 0 or a[i - 1] % 2: dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + 1) for j in range(n + 1): if a[i - 1] == 0 or not a[i - 1] % 2: dp[i][j][1] = min(dp[i - 1][j][0] + 1, dp[i - 1][j][1]) if a[-1] == 0: print(min(dp[n][n - n // 2][0], dp[n][n - n // 2][1])) elif a[-1] % 2: print(dp[n][n - n // 2][0]) else: print(dp[n][n - n // 2][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) nums = [int(x) for x in input().split()] missing = set([x for x in range(1, n + 1)]) if nums.count(0) == n: if n == 1: print(0) else: print(1) else: for x in nums: if x != 0: missing.remove(x) odds = 0 evens = 0 for x in missing: if x % 2 == 0: evens += 1 else: odds += 1 gaps = [] gaps2 = [] ind = 0 if nums[0] == 0: left = -1 leftInd = 0 while nums[ind] == 0: ind += 1 rightInd = ind right = nums[ind] % 2 gaps2.append([rightInd - leftInd, left, right]) if nums[-1] == 0: right = -1 rightInd = n while nums[n - 1] == 0: n -= 1 leftInd = n - 1 left = nums[n - 1] % 2 gaps2.append([rightInd - leftInd, left, right]) while ind < n: if nums[ind] == 0: left = nums[ind - 1] % 2 leftInd = ind while nums[ind] == 0: ind += 1 rightInd = ind right = nums[ind] % 2 gaps.append([rightInd - leftInd, left, right]) else: ind += 1 gaps.sort() total = 0 for x in range(n - 1): if nums[x] != 0 and nums[x + 1] != 0 and nums[x] % 2 != nums[x + 1] % 2: total += 1 for x in range(len(gaps) - 1, -1, -1): if gaps[x][1] + gaps[x][2] == 1: gaps.pop(x) total += 1 def best(gaps, odd, even): total = 0 for l, ty, ty2 in gaps: if ty == 1: if odd >= l: odd -= l else: total += 2 elif even >= l: even -= l else: total += 2 return total first = total + best(gaps, odds, evens) + len(gaps2) for x in gaps2: if x[1] == 1 or x[2] == 1: if odds >= x[0]: first = min( first, total + best(gaps, odds - x[0], evens) + len(gaps2) - 1 ) elif evens >= x[0]: first = min(first, total + best(gaps, odds, evens - x[0]) + len(gaps2) - 1) if len(gaps2) == 2: newOdd = odds newEven = evens if gaps2[0][2] == 1: newOdd -= gaps2[0][0] else: newEven -= gaps2[0][0] if gaps2[1][1] == 1: newOdd -= gaps2[1][0] else: newEven -= gaps2[1][0] if newOdd >= 0 and newEven >= 0: first = min(first, total + best(gaps, newOdd, newEven)) print(first)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys input = sys.stdin.readline n = int(input()) b = list(map(int, input().split())) dum = n // 2 + 1 nxtd_arr = [[[101, 101] for _ in range(dum)] for _ in range(n + 1)] nxtd_arr[0][0][0] = nxtd_arr[0][0][1] = 0 for i in range(1, n + 1): if b[i - 1] == 0: for j in range(1, dum): nxtd_arr[i][j][0] = min( nxtd_arr[i - 1][j - 1][0], nxtd_arr[i - 1][j - 1][1] + 1 ) for j in range(dum): nxtd_arr[i][j][1] = min(nxtd_arr[i - 1][j][0] + 1, nxtd_arr[i - 1][j][1]) elif b[i - 1] % 2 == 0: for j in range(1, dum): nxtd_arr[i][j][0] = min( nxtd_arr[i - 1][j - 1][0], nxtd_arr[i - 1][j - 1][1] + 1 ) else: for j in range(dum): nxtd_arr[i][j][1] = min(nxtd_arr[i - 1][j][0] + 1, nxtd_arr[i - 1][j][1]) print(min(nxtd_arr[-1][-1]))
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 ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
def main(): n = int(input()) a = input() even_count = int(n / 2) odd_count = n - even_count result = 0 arr = list(map(int, a.split())) left = -1 right = -1 odd = {} even = {} diff = 0 zero = 0 special_odd = {} special_even = {} for x in arr: if x == 0: zero += 1 continue if x % 2 == 1: odd_count -= 1 else: even_count -= 1 if left == -1: if zero != 0: if x % 2 == 1: special_odd[zero] = ( special_odd[zero] + 1 if zero in special_odd else 1 ) else: special_even[zero] = ( special_even[zero] + 1 if zero in special_even else 1 ) left = x zero = 0 continue if x != 0 and zero == 0: if left % 2 != x % 2: result += 1 left = x continue right = x if left % 2 == right % 2: if left % 2 == 1: odd[zero] = odd[zero] + 1 if zero in odd else 1 else: even[zero] = even[zero] + 1 if zero in even else 1 else: diff += 1 zero = 0 left = x right = -1 if right == -1 and zero != 0: if left % 2 == 1: special_odd[zero] = special_odd[zero] + 1 if zero in special_odd else 1 else: special_even[zero] = special_even[zero] + 1 if zero in special_even else 1 for key, value in sorted(odd.items()): counter = value while counter > 0 and odd_count >= key: odd_count -= key counter -= 1 if counter == 0: continue if odd_count < key: result += 2 * counter for key, value in sorted(even.items()): counter = value while counter > 0 and even_count >= key: even_count -= key counter -= 1 if counter == 0: continue if even_count < key: result += 2 * counter for key, value in sorted(special_odd.items()): counter = value while counter > 0 and odd_count >= key: odd_count -= key counter -= 1 if counter == 0: continue if odd_count < key: result += 1 * counter for key, value in sorted(special_even.items()): counter = value while counter > 0 and even_count >= key: even_count -= key counter -= 1 if counter == 0: continue if even_count < key: result += 1 * counter result += diff print(result) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
def abc(l, odd, even, i, x): try: return dp[i, odd, even, x] except: pass if i == len(l): return 0 if l[i]: c = 0 if x != l[i] % 2: c += 1 return c + abc(l, odd, even, i + 1, l[i] % 2) ans = n if odd: c = 0 if not x % 2: c += 1 ans = min(ans, c + abc(l, odd - 1, even, i + 1, 1)) if even: c = 0 if x % 2: c += 1 ans = min(ans, c + abc(l, odd, even - 1, i + 1, 0)) dp[i, odd, even, x] = ans return ans n = int(input()) l = list(map(int, input().split())) dp = {} odd = 0 even = 0 d = [0] * (n + 1) for i in l: d[i] += 1 for i in range(1, n + 1): if i % 2 and not d[i]: odd += 1 if not i % 2 and not d[i]: even += 1 if l[0]: print(abc(l, odd, even, 1, l[0] % 2)) else: ans = n if odd: ans = min(ans, abc(l, odd - 1, even, 1, 1)) if even: ans = min(ans, abc(l, odd, even - 1, 1, 0)) print(ans)
FUNC_DEF RETURN VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) a = list(map(int, input().split())) s = set(a) u, v = 0, 0 for i in range(1, n + 1): if i not in s: if i & 1: u += 1 else: v += 1 dp = [[[([n] * (v + 1)) for _ in range(u + 1)] for _ in range(2)] for _ in range(n + 1)] dp[0][0][u][v] = 0 for i in range(1, n + 1): ai = a[i - 1] if ai == 0: for pp in range(2): for qp in range(2): w = int(pp != qp) if i > 1 else 0 for p in range(u + 1): for q in range(v + 1): if qp == 1 and p + 1 <= u: dp[i][qp][p][q] = min( dp[i][qp][p][q], dp[i - 1][pp][p + 1][q] + w ) elif qp == 0 and q + 1 <= v: dp[i][qp][p][q] = min( dp[i][qp][p][q], dp[i - 1][pp][p][q + 1] + w ) elif ai & 1: w = int(i > 1) for p in range(u + 1): for q in range(v + 1): dp[i][1][p][q] = min(dp[i - 1][0][p][q] + w, dp[i - 1][1][p][q]) else: w = int(i > 1) for p in range(u + 1): for q in range(v + 1): dp[i][0][p][q] = min(dp[i - 1][1][p][q] + w, dp[i - 1][0][p][q]) print(min(dp[n][0][0][0], dp[n][1][0][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 VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) nums = list(map(int, input().split(" "))) if n == 1: print(0) exit() memory = {} def dp(i, j, k): if i < k - 1: return 100 if i < 0: if j == 1 and k != 0 or j == 0 and k != -1: return 100 else: return 0 if nums[i] != 0: if nums[i] % 2 != j: return 100 if j == 0: return min(dp(i - 1, 0, k - 1), dp(i - 1, 1, k) + 1) else: return min(dp(i - 1, 0, k - 1) + 1, dp(i - 1, 1, k)) if (i, j, k) in memory: return memory[i, j, k] if j == 0: res = min(dp(i - 1, 0, k - 1), dp(i - 1, 1, k) + 1) else: res = min(dp(i - 1, 0, k - 1) + 1, dp(i - 1, 1, k)) memory.setdefault((i, j, k), res) return res res = min(dp(n - 1, 0, (n >> 1) - 1), dp(n - 1, 1, n >> 1)) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
N = int(input()) A = [int(a) for a in input().split()] M = A.count(0) A2 = [(a % 2) for a in A] O = A2.count(1) E = N - M - O O = (N + 1) // 2 - O E = N // 2 - E C = [M] for a in A: C.append(C[-1] - (a == 0)) X = [([999] * 102) for _ in range(N)] Y = [([999] * 102) for _ in range(N)] if A[0] % 2: X[0][O] = 0 elif A[0]: Y[0][O] = 0 else: if O: X[0][O - 1] = 0 if E: Y[0][O] = 0 for i in range(1, N): for j in range(N + 1): if A[i] % 2: X[i][j] = min(X[i - 1][j], Y[i - 1][j] + 1) elif A[i]: Y[i][j] = min(X[i - 1][j] + 1, Y[i - 1][j]) else: X[i][j] = min(X[i - 1][j + 1], Y[i - 1][j + 1] + 1) Y[i][j] = min(X[i - 1][j] + 1, Y[i - 1][j]) print(min(X[N - 1][0], Y[N - 1][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
import sys readline = sys.stdin.readline N = int(readline()) S = list(map(int, readline().split())) S = [(0 if s == 0 else 1 if s & 1 else -1) for s in S] odd = -(-N // 2) even = N // 2 for s in S: if s: if s == 1: odd -= 1 else: even -= 1 inf = 10**9 dpe = [([inf] * (odd + 1)) for _ in range(even + 1)] dpo = [([inf] * (odd + 1)) for _ in range(even + 1)] dpe[0][0] = 0 dpo[0][0] = 0 for i in range(N): dp2e = [([inf] * (odd + 1)) for _ in range(even + 1)] dp2o = [([inf] * (odd + 1)) for _ in range(even + 1)] s = S[i] for e in range(even + 1): for o in range(odd + 1): if s == 1: dp2o[e][o] = min(dp2o[e][o], dpo[e][o], 1 + dpe[e][o]) elif s == -1: dp2e[e][o] = min(dp2e[e][o], dpe[e][o], 1 + dpo[e][o]) else: if o < odd: dp2o[e][o + 1] = min(dp2o[e][o + 1], dpo[e][o], 1 + dpe[e][o]) if e < even: dp2e[e + 1][o] = min(dp2e[e + 1][o], dpe[e][o], 1 + dpo[e][o]) dpe = [d[:] for d in dp2e] dpo = [d[:] for d in dp2o] print(min(dpe[even][odd], dpo[even][odd]))
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 ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) l = list(map(int, input().split())) a = [0, 0] for i in range(1, n + 1): if i not in l: if i % 2 == 0: a[0] += 1 else: a[1] += 1 s = -1 e = -1 ind = -1 ans = 0 ind1 = -1 f = n - 1 end = 0 for i in range(n): if l[i] != 0: f = i break for i in range(n): if l[n - 1 - i] != 0: end = n - 1 - i break i = f + 1 fsd = [[], []] while i < end + 1: if l[i] == 0 and l[i - 1] == 0: i += 1 continue if l[i] != 0 and l[i - 1] != 0: i += 1 continue if l[i] == 0 and l[i - 1] != 0: s = l[i - 1] ind = i - 1 if l[i] != 0 and l[i - 1] == 0: e = l[i] ind1 = i if ind >= ind1: i += 1 continue if s != -1 and e != -1: if s % 2 != e % 2: ans += 1 if s % 2 == e % 2: fsd[s % 2].append(ind1 - ind - 1) i += 1 fsd1 = [[], []] if f != 0: e = l[f] fsd1[e % 2].append(f) if end != n - 1: s = l[end] fsd1[s % 2].append(n - end - 1) fsd[0].sort() fsd[1].sort() fsd1[0].sort() fsd1[1].sort() s = 0 t = 0 for i in range(len(fsd[0])): s = s + fsd[0][i] if s > a[0]: a[0] -= s - fsd[0][i] t = 1 ans += 2 * (len(fsd[0]) - i) break if t == 0: a[0] -= sum(fsd[0]) s = 0 t = 0 for i in range(len(fsd[1])): s = s + fsd[1][i] if s > a[1]: a[1] -= s - fsd[1][i] t = 1 ans += 2 * (len(fsd[1]) - i) break if t == 0: a[1] -= sum(fsd[1]) s = 0 for i in range(len(fsd1[0])): s = s + fsd1[0][i] if s > a[0]: a[0] -= s - fsd1[0][i] t = 1 ans += 1 * (len(fsd1[0]) - i) break s = 0 for i in range(len(fsd1[1])): s = s + fsd1[1][i] if s > a[1]: a[1] -= s - fsd1[1][i] t = 1 ans += 1 * (len(fsd1[1]) - i) break for i in range(1, n): if l[i] != 0 and l[i - 1] != 0 and l[i - 1] % 2 != l[i] % 2: ans += 1 if sum(l) == 0 and n > 1: ans = 1 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 LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST LIST WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST LIST IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) p = [int(x) for x in input().split()] pi = [i for i in range(len(p)) if p[i] != 0] even, odd = 0, 0 ans = 0 ee, oo = [], [] oe = 0 for x in p: if x != 0: if x & 1: odd += 1 else: even += 1 st = [-1, -1] en = [-1, -1] if len(pi) > 0 and pi[0] > 0: st = [p[pi[0]] & 1, pi[0]] if len(pi) > 0 and pi[-1] < n - 1: en = [p[pi[-1]] & 1, n - 1 - pi[-1]] for i in range(1, len(pi)): t = pi[i] - pi[i - 1] - 1 if p[pi[i]] & 1 == p[pi[i - 1]] & 1: if p[pi[i]] & 1: oo.append(t) else: ee.append(t) else: oe += 1 odd = (n + (n & 1)) // 2 - odd even = (n - (n & 1)) // 2 - even oo.sort() ee.sort() s, se, so = 0, len(ee), len(oo) le, lo = even, odd for i in range(len(ee)): s += ee[i] le = even - s if le < 0: le += ee[i] se = i break s = 0 for i in range(len(oo)): s += oo[i] lo = odd - s if lo < 0: lo += oo[i] so = i break ans += 2 * (len(ee) - se) + 2 * (len(oo) - so) + oe if st[0] != -1 and en[0] != -1: if st[1] > en[1]: st, en = en, st if st[0] != -1: if st[0]: if lo < st[1]: ans += 1 else: lo -= st[1] elif le < st[1]: ans += 1 else: le -= st[1] if en[0] != -1: if en[0]: if lo < en[1]: ans += 1 else: lo = 0 elif le < en[1]: ans += 1 else: le = 0 if len(pi) == 0 and len(p) > 1: ans = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) cou = [n // 2, n // 2] if n % 2 != 0: cou[1] += 1 a = [int(o) for o in input().split()] for i in range(n): if a[i] == 0: a[i] = -1 else: a[i] %= 2 cou[a[i]] -= 1 curr = 0 ps = [] ans = 0 ns = [] fl = [] for i in range(1, n): if a[i] != -1: if a[i] != a[curr]: if a[curr] == -1: fl.append([i, a[i], curr - 1, i - 1]) else: ns.append([i - curr - 1, a[i], curr, i - 1]) else: ps.append([i - curr - 1, a[i], curr, i - 1]) curr = i if a[n - 1] == -1: fl.append([n - 1 - curr, a[curr], curr + 1, n]) ps.sort() ns.sort() fl.sort() flag = 0 for i in ps: if flag == 1 or i[0] <= cou[i[1]]: for j in range(i[3], i[2], -1): if cou[i[1]] > 0: cou[i[1]] -= 1 a[j] = i[1] else: cou[(i[1] + 1) % 2] -= 1 a[j] = (i[1] + 1) % 2 else: for k in fl: if k[3] == n: for j in range(k[2], k[3]): if cou[k[1]] > 0: cou[k[1]] -= 1 a[j] = k[1] else: cou[(k[1] + 1) % 2] -= 1 a[j] = (k[1] + 1) % 2 else: for j in range(k[3], k[2], -1): if cou[k[1]] > 0: cou[k[1]] -= 1 a[j] = k[1] else: cou[(k[1] + 1) % 2] -= 1 a[j] = (k[1] + 1) % 2 for j in range(i[3], i[2], -1): if cou[i[1]] > 0: cou[i[1]] -= 1 a[j] = i[1] else: cou[(i[1] + 1) % 2] -= 1 a[j] = (i[1] + 1) % 2 flag = 1 if fl and flag == 0: if 1: for k in fl: if k[3] == n: for j in range(k[2], k[3]): if cou[k[1]] > 0: cou[k[1]] -= 1 a[j] = k[1] else: cou[(k[1] + 1) % 2] -= 1 a[j] = (k[1] + 1) % 2 else: for j in range(k[3], k[2], -1): if cou[k[1]] > 0: cou[k[1]] -= 1 a[j] = k[1] else: cou[(k[1] + 1) % 2] -= 1 a[j] = (k[1] + 1) % 2 for i in ns: for j in range(i[3], i[2], -1): if cou[i[1]] > 0: cou[i[1]] -= 1 a[j] = i[1] else: cou[(i[1] + 1) % 2] -= 1 a[j] = (i[1] + 1) % 2 if a[0] == -1 and len(a) > 1: a[0] = a[1] ans = 0 for i in range(1, n): if a[i] != a[i - 1]: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF NUMBER FOR VAR VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2.
n = int(input()) ps0 = [int(x) for x in input().split()] to_add = [x for x in range(1, n + 1) if x not in ps0] if all(x == 0 for x in ps0): if n == 1: print(0) exit() print(1) exit() ANS = 1000 for I in range(2): ps = ps0[:] if I == 0: to_add.sort(key=lambda x: x % 2) else: to_add.sort(key=lambda x: x % 2, reverse=True) odds = [(x % 2) for x in to_add].count(1) evens = len(to_add) - odds for t in to_add: so = 101, None se = 101, None so2 = 101, None, None se2 = 101, None, None mixed = None left = None zero_count = 0 for i, p in enumerate(ps): if p == 0: zero_count += 1 else: if zero_count > 0: if left is None: if p % 2 == 0: se2 = zero_count, i - 1, 0 else: so2 = zero_count, i - 1, 0 elif p % 2 == 0: if ps[left] % 2 == 0: se = min(se, (zero_count, left + 1)) else: mixed = i - 1, left + 1 elif ps[left] % 2 == 0: mixed = left + 1, i - 1 else: so = min(so, (zero_count, left + 1)) zero_count = 0 left = i if zero_count > 0: if ps[left] % 2 == 0: se2 = min(se2, (zero_count, left + 1, -1)) else: so2 = min(so2, (zero_count, left + 1, -1)) if t % 2 == 0: if se[1] is not None and evens >= se[0]: ps[se[1]] = t elif se2[1] is not None: ps[se2[1]] = t elif se[1] is not None and evens < se[0]: ps[se[1]] = t elif mixed is not None: ps[mixed[0]] = t elif so2[1] is not None: ps[so2[2]] = t else: ps[so[1]] = t evens -= 1 else: if so[1] is not None and odds >= so[0]: ps[so[1]] = t elif so2[1] is not None: ps[so2[1]] = t elif so[1] is not None and odds < so[1]: ps[so[1]] = t elif mixed is not None: ps[mixed[1]] = t elif se2[1] is not None: ps[se2[2]] = t else: ps[se[1]] = t odds -= 1 ans = 0 l = None for i in range(len(ps)): if i == 0: continue if ps[i] % 2 != ps[i - 1] % 2: ans += 1 ANS = min(ANS, ans) print(ANS)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE NONE ASSIGN VAR NUMBER NONE NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NONE IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NONE ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NONE ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a) ruiseki = [0] * (n + 1) for i in range(n): ruiseki[i + 1] = ruiseki[i] + a[i] ans = [0] * n for i in range(n): if i - m >= 0: ans[i] = ruiseki[i + 1] + ans[i - m] else: ans[i] = ruiseki[i + 1] print(*ans)
ASSIGN 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l1 = list(map(int, input().split())) l1.sort() dp = [0] * m sum = 0 for i in range(n): dp[i % m] = dp[i % m] + l1[i] sum = sum + dp[i % m] print(sum, end=" ")
ASSIGN 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 LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) arr.sort() ans = list() prefix = list() curr = 0 for i in range(0, n): curr += arr[i] prefix.append(curr) for i in range(n): if i + 1 <= m: ans.append(prefix[i]) else: ans.append(prefix[i] + ans[i - m]) for i in range(n): print(ans[i], end=" ")
ASSIGN 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() pref = [a[0]] tab = [0] * n for i in range(1, n): pref.append(pref[-1] + a[i]) dp = [0] * n for i in range(n): if i < m: dp[i] = pref[i] else: dp[i] = dp[i - m] + pref[i] print(*dp)
ASSIGN 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 LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = [0] + sorted(map(int, input().split())) ma = [0] * m total = 0 ans = [0] * n for i in range(1, n + 1): ma[i % m] += a[i] total += ma[i % m] ans[i - 1] = total print(*ans)
ASSIGN VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") n, m = INL() X = sorted(INL()) ANS = [] req = [] for _ in range(n): if _ == 0: ANS.append(X[_]) req.append(X[_]) elif _ < m: ANS.append(ANS[-1] + X[_]) req.append(X[_]) else: ANS.append(ANS[-1] + req[_ - m] + X[_]) req.append(req[_ - m] + X[_]) OPL(ANS)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from sys import stdin def input(): return stdin.readline()[:-1] def intput(): return int(input()) def sinput(): return input().split() def intsput(): return map(int, sinput()) n, m = intsput() a = list(intsput()) a.sort() cumsum = [a[0]] for i in range(1, n): cumsum.append(cumsum[-1] + a[i]) cumcumsum = [cumsum[0]] for i in range(1, n): attempt = i - m if attempt >= 0: prev = cumcumsum[attempt] else: prev = 0 cumcumsum.append(prev + cumsum[i]) ans = [] for k in range(1, n + 1): ans.append(str(cumcumsum[k - 1])) print(" ".join(ans))
FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def numbersListInput(): aux = list(map(int, input().strip().split())) return aux a = numbersListInput() n = a[0] m = a[1] a = numbersListInput() a.sort() penalty = a[:] sweet = 0 for k in range(n): sweet += a[k] penalty[k] = sweet if k >= m: penalty[k] += penalty[k - m] print(*penalty)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from itertools import accumulate n, m = map(int, input().split()) l = list(map(int, input().split())) l.sort() S = 0 temp = [(0) for i in range(m)] for k in range(n): temp[k % m] += l[k] S += temp[k % m] print(S, end=" ") print()
ASSIGN 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = list(map(lambda x: int(x), input().split())) a = list(map(lambda x: int(x), input().split())) a.sort() ans = [0] trail = [] for i in range(m): trail.append(a[i]) ans.append(ans[-1] + trail[i]) for i in range(m, n): trail.append(a[i] + trail[i - m]) ans.append(ans[-1] + trail[i]) print(" ".join(str(x) for x in ans[1:]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] ans = [0] * n a.sort() cur = [0] * m sum = 0 for i in range(0, n): sum = sum + a[i] + cur[i % m] cur[i % m] = cur[i % m] + a[i] print(sum, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l = [int(x) for x in input().split()] l.sort() j = 0 ans = [] for i in range(m, n): l[i] += l[i - m] prev = 0 total = 0 for k in range(len(l)): total += l[k] ans.append(total) print(*ans)
ASSIGN 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 ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = sorted(list(map(int, input().split()))) p = [(0) for i in range(n)] dp = [(0) for i in range(n)] p[0] = a[0] dp[0] = a[0] print(dp[0], end=" ") for i in range(1, n): p[i] = p[i - 1] + a[i] dp[i] = p[i] if i >= m: dp[i] += dp[i - m] print(dp[i], end=" ") print("")
ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) li = list(map(int, input().split())) ans = [0] * (n + 1) new_li = [0] * (n + 1) li.sort() for i in range(1, n + 1): new_li[i] = li[i - 1] cur_sum = 0 for i in range(1, n + 1): cur_sum += new_li[i] ans[i] = cur_sum if i > m: ans[i] += ans[i - m] print(ans[i], end=" ") print()
ASSIGN 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 LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = list(map(lambda x: int(x), input().split())) a = list(map(lambda x: int(x), input().split())) dp = [(0) for _ in range(m)] a.sort() ans = 0 final = [] for i in range(n): ans += a[i] + dp[i % m] dp[i % m] += a[i] final.append(ans) print(*final)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l = list(map(int, input().split())) l.sort() k = [l[0]] for i in range(1, n): k.append(k[-1] + l[i]) ans = [] for i in range(n): if i < m: ans.append(k[i]) print(ans[-1], end=" ") else: ans.append(ans[i - m] + k[i]) print(ans[-1], end=" ")
ASSIGN 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 LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) lst = [*map(int, input().split())] lst.sort() item, res, s = lst[:m], [], 0 app = res.append for i, x in enumerate(item): s += x res.append(s) append = item.append for i, x in enumerate(lst[m:]): append(item[i] + x) app(res[-1] + item[-1]) print(*res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR LIST NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
N, M = map(int, input().split()) candy = list(map(int, input().split())) ans = [0] * (N + 1) candy.sort() for i in range(N): if i > 0: candy[i] += candy[i - 1] ans[i] = candy[i] if i - M >= 0: ans[i] += ans[i - M] print(ans[i], end=" ")
ASSIGN 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 LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = tuple(map(int, input().split())) lis = list(map(int, input().split())) lis.sort(reverse=True) acc = [sum(lis)] for i in range(1, n): acc.append(acc[-1] - lis[i - 1]) if n == m: print(*acc[::-1]) exit() ans = acc[n - 1 : n - m - 1 : -1] for i in range(m, n): ans.append(acc[-i - 1] + ans[i - m]) print(*ans)
ASSIGN 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 NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
import sys from itertools import accumulate input = sys.stdin.readline n, m = map(int, input().split()) A = list(map(int, input().split())) A.sort() S = list(accumulate(A)) ANS = S[:m] for i in range(m, n): ANS.append(ANS[i - m] + S[i]) print(" ".join(map(str, ANS)))
IMPORT ASSIGN VAR VAR ASSIGN 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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = sorted(map(int, input().split())) l = [a[0]] y = [0] * m y[0] = a[0] for i in range(1, m): l.append(l[-1] + a[i]) y[i] = a[i] for i in range(m, n): x = l[-1] y[i % m] += a[i] if i - m >= 0: x += y[i % m] l.append(x) print(*l)
ASSIGN 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 LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() b = list() b.append(a[0]) try: a[m] += a[0] except: c = 0 sn = a[0] for i in range(1, n): r = a[i] try: a[i + m] += r except: c = 0 sn += r b.append(sn) for i in range(n): print(b[i], end=" ")
ASSIGN 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 EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def sol(): n, m = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a) sum = [] ts = 0 for i in range(n): ts += a[i] sum.append(ts) dp = sum[:m] for i in range(m, n): dp.append(dp[i - m] + sum[i]) for i in range(n - 1): print(dp[i], end=" ") print(dp[n - 1]) sol()
FUNC_DEF ASSIGN 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split(" ")) a = list(map(int, input().split(" "))) a.sort() a_cs = [0] * n a_cs[0] = a[0] for i in range(1, n): a_cs[i] = a_cs[i - 1] + a[i] dp = [0] * (n + 1) for i in range(1, n + 1): dp[i] = a_cs[i - 1] + dp[max(i - m, 0)] print(" ".join(map(str, dp[1:])))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from sys import stdin input = stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() ans = [0] * 200005 sum = [0] * 200005 for i in range(1, n + 1): sum[i] = sum[i - 1] + a[i - 1] ans[i] = ans[max(i - m, 0)] + sum[i] print(*ans[1 : n + 1])
ASSIGN VAR VAR ASSIGN 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 LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() g = [a[i::m] for i in range(m)] for i in range(m): for j in range(1, len(g[i])): g[i][j] += g[i][j - 1] ans = [0] for i in range(n): ans.append(ans[-1] + g[i % m][i // m]) print(*ans[1:])
ASSIGN 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 VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
import sys nm = sys.stdin.readline().split(" ") m = int(nm[1]) sweets = sorted(map(int, sys.stdin.readline().split(" "))) print(sweets[0], end=" ") toAdd = [0] * m toAdd[0] = last = sweets[0] for k in range(1, len(sweets)): toAdd[k % m] += sweets[k] last += toAdd[k % m] print(last, end=" ")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
a = input().split(" ") n = int(a[0]) m = int(a[1]) s = [] l = input().split(" ") for i in range(n): x = int(l[i]) s.append(x) s.sort() d = [0] * m sum = 0 ans = [] for i in range(n): sum = sum + s[i] + d[i % m] ans.append(sum) d[i % m] += s[i] print(" ".join([str(i) for i in ans]))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = [int(i) for i in input().split()] d = [int(i) for i in input().split()] d.sort() ans = [] tot = 0 psum = [0] * m for i in range(n): md = i % m psum[md] += d[i] tot += psum[md] ans.append(tot) print(" ".join(map(str, ans)))
ASSIGN 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 LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def get_sums(n, a): sums = [0] * n sums[0] = a[0] for index in range(1, n): sums[index] = sums[index - 1] + a[index] return sums def get_partial_sums(n, a, m): partial_sums = [0] * n partial_sums[0] = a[0] for i in range(1, n): if i < m: partial_sums[i] = partial_sums[i - 1] + a[i] else: partial_sums[i] = partial_sums[i - 1] + a[i] - a[i - m] return partial_sums def get_penalties(num_candies, candies, max_candies): candies.sort() sums = get_sums(num_candies, candies) partial_sums = get_partial_sums(num_candies, candies, max_candies) penalties = [0] * num_candies for i in range(0, num_candies): if i < max_candies: penalties[i] = sums[i] else: penalties[i] = ( partial_sums[i] + penalties[i - max_candies] + sums[i - max_candies] ) return penalties def solve(): first_line = input() num_candies, max_candies = [int(num) for num in first_line.split()] second_line = input() candies = [int(num) for num in second_line.split()] penalties = get_penalties(num_candies, candies, max_candies) solution = " ".join([str(penalty) for penalty in penalties]) print(solution) solve()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = [int(s) for s in input().split()] a.sort() b = [(0) for i in range(m)] res = [] x = 0 for k in range(n): b[k % m] += a[k] x += b[k % m] res.append(str(x)) print(" ".join(res))
ASSIGN 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = sorted(list(map(int, input().split()))) pref_sum = [(0) for i in range(n + 1)] for i in range(1, n + 1): pref_sum[i] += a[i - 1] + pref_sum[i - 1] pr_s_m = [(0) for i in range(n + 1)] pr_s_m1 = [(0) for i in range(n + 1)] for i in range(0, n): pr_s_m[i] = pr_s_m[i - m] pr_s_m[i] += a[i] pref_sum.append(0) ans = [pref_sum[i + 1] for i in range(m)] d = 1 for k in range(m, n + 1): ans.append(0) if d * m >= k: ans[-1] = ans[-2] + pr_s_m[k] else: d += 1 ans[-1] = ans[-2] + pr_s_m[k] print(*ans[:-1])
ASSIGN 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l = list(map(int, input().split())) d = dict() for i in range(m): d[i] = 0 l.sort() ls = [] ss = 0 for i in range(n): y = i % m xs = d[y] ss += xs + l[i] ls.append(ss) d[y] += l[i] print(*ls)
ASSIGN 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = [0] + list(map(int, input().split())) a.sort() price = 0 increase = [0] * (m + 1) p = 1 for i in range(1, n + 1): price += a[i] if i > m: foc = i % m if foc == 0: foc = m increase[foc] += a[i - m] price += increase[foc] print(price, end=" ") print()
ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def f(a, k): ans = [0] * len(a) a = sorted(a) tl = 0 pref = [0] * (len(a) + 1) for i in range(0, len(a)): pref[i] = pref[i - 1] + a[i] tl += 1 if tl > k: ans[i] = pref[i] + ans[i - k] else: ans[i] = pref[i] return ans n, m = map(int, input().strip().split()) l = list(map(int, input().strip().split())) print(*f(l, m))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = list(map(int, input().split())) arr = list(map(int, input().split())) arr.sort() ans = [] for i in range(n): if i == 0: ans.append(arr[0]) else: ans.append(ans[-1] + arr[i]) final = [(0) for i in range(n)] for i in range(n): if i < m: final[i] += ans[i] else: final[i] = final[i - m] + ans[i] print(*final)
ASSIGN 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
import sys input = sys.stdin.buffer.readline n, m = map(int, input().split()) ls = list(map(int, input().split())) ls.sort() sm = [] for i in range(n): sm.append(sm[-1] + ls[i] if sm else ls[i]) sw = [0] * n for i in range(n): sw[i] = (sw[i - m] if i - m >= 0 else 0) + sm[i] print(" ".join(map(str, sw)) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def main(): n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() p = [0] * n b = [0] * n p[0] = a[0] for i in range(1, n): p[i] = a[i] + p[i - 1] for i in range(n): b[i] = p[i] if i >= m: b[i] += b[i - m] print(*b) return main()
FUNC_DEF ASSIGN 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 LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = [int(x) for x in input().split()] a = list(sorted([int(x) for x in input().split()])) res = [0] * n ma = [0] * m res[0] = a[0] ma[0] = a[0] for k in range(1, n): ma[k % m] += a[k] res[k] = res[k - 1] + ma[k % m] print(*res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) n, m = f() l = sorted(il()) dpm = [0] * n dpsm = [0] * n dpm[0] = l[0] dpsm[0] = l[0] for i in range(1, m): dpm[i] = l[i] dpsm[i] = dpsm[i - 1] + l[i] for i in range(m, n): dpm[i] = l[i] + dpm[i - m] dpsm[i] = dpsm[i - 1] + dpm[i] print(*dpsm)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN 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 FUNC_CALL VAR ASSIGN VAR 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 NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
def solve(n, m, ar): ar = sorted(ar) ans = [0] * n cursum = 0 for i in range(n): cursum += ar[i] ans[i] = cursum if i >= m: ans[i] += ans[i - m] print(" ".join(map(str, ans))) n, m = map(int, input().split()) ar = list(map(int, input().split())) solve(n, m, ar)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN 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 VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l = [int(x) for x in input().split()] l.sort() dp = [0] * n for i in range(n): dp[i] = l[i] if i >= m: dp[i] += dp[i - m] t = 0 ans = [] for i in dp: t += i ans.append(t) print(*ans)
ASSIGN 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 ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n_m = list(map(int, input().split(" "))) n, m = n_m arr = list(map(int, input().split(" "))) arr.sort() dic = {} dic[1] = arr[0] prev_sum = 0 for i in range(1, n): if i + 1 <= m: dic[i + 1] = dic[i] + arr[i] elif i + 1 > m and i + 1 <= 2 * m: if prev_sum: prev_sum -= arr[i - m] prev_sum += arr[i] dic[i + 1] = prev_sum + 2 * dic[i + 1 - m] else: prev_sum = sum(arr[i - m + 1 : i + 1]) dic[i + 1] = prev_sum + 2 * dic[i + 1 - m] else: prev_sum -= arr[i - m] prev_sum += arr[i] dic[i + 1] = prev_sum + 2 * dic[i + 1 - m] - dic[i + 1 - 2 * m] print(*dic.values())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from sys import stdin def input(): return stdin.readline().strip() n, m = map(int, input().split()) sweets = list(map(int, input().split())) sweets.sort() K = [0] * (n + 1) add = 0 for i in range(1, n + 1): K[i] += K[i - 1] + sweets[i - 1] for i in range(m, n + 1): K[i] += K[i - m] print(*K[1:])
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN 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 LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = tuple(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) t = 1 prefix = [0] * n result = [0] * n for i in range(m): for j in range(i, n, m): if j < m: prefix[j] = a[i] else: prefix[j] = prefix[j - m] + a[j] for i in range(n): if i == 0: result[i] = a[0] elif i < m: result[i] = result[i - 1] + a[i] else: result[i] = result[i - 1] + a[i] + prefix[i - m] for i in range(n): print(result[i], end=" ") print()
ASSIGN 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
import sys n, m = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) a.sort() val = list(a) for i in range(1, n): val[i] += val[i - 1] ans = list(val) for i in range(m, n): ans[i] += ans[i - m] print(*ans)
IMPORT ASSIGN 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
t = lambda: map(int, input().split()) n, m = t() a = sorted(t()) x, s = [0] * n, 0 for i in range(n): s += a[i] x[i] = s if i < m else s + x[i - m] print(*x)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
s = input() s1 = s.split() k = int(s1[1]) s = input() s1 = s.split() l = [int(i) for i in s1] l.sort() dp = [(0) for i in range(len(l))] dp[0] = l[0] pre = [(0) for i in range(len(l))] pre[0] = l[0] for i in range(1, len(l)): pre[i] = pre[i - 1] + l[i] for i in range(1, len(l)): if i >= k: dp[i] = dp[i - k] + pre[i - k] + pre[i] - pre[i - k] else: dp[i] = dp[i - 1] + l[i] print(*dp)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = [int(i) for i in input().split()] lst = [int(i) for i in input().split()] lst.sort() arr1, arr2 = [0] * 200001, [-1] * 200001 arr2[0], s = 0, 0 for i in range(len(lst)): s += lst[i] arr1[i + 1] = s def eat(n): if arr2[n] > 0: return arr2[n] if n < m: arr2[n] = arr1[n] return arr2[n] arr2[n] = eat(n - m) + arr1[n] return arr2[n] print(" ".join(map(str, [eat(n) for n in range(1, n + 1)])))
ASSIGN 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 VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) lt = [int(x) for x in input().split()] lt.sort() pref = [(0) for _ in range(n)] pref[0] = lt[0] for i in range(1, n): pref[i] = lt[i] + pref[i - 1] pref = [0] + pref ans = [(0) for _ in range(n + 1)] for i in range(1, m + 1): ans[i] = pref[i] for i in range(m + 1, n + 1): ans[i] = pref[i] + ans[i - m] print(*ans[1:])
ASSIGN 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) aa = [int(i) for i in input().split()] aa = sorted(aa) k = [0] * m ans = [] ss = 0 for i in range(0, n): k[i % m] += aa[i] ss += k[i % m] ans.append(ss) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
numbers = list(map(int, input().split())) n = numbers[0] m = numbers[1] sweets = list(map(int, input().split())) sweets.sort() sweets = [0] + sweets rez = [0] * n t = 0 penalty = [0] * m for i in range(1, n + 1): x = i % m penalty[x] = penalty[x] + sweets[i] t = t + penalty[x] rez[i - 1] = t for i in range(n): print(rez[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
ints = lambda: [int(x) for x in input().split()] def main(): while 1: try: n, m = ints() except EOFError: break a = sorted(ints()) z = [0] * n for k in range(n): z[k] = a[k] + (0 if k < m else z[k - m]) x = [0] * (n + 1) for k in range(n): x[k + 1] = x[k] + a[k] + (z[k - m] if k - m >= 0 else 0) print(*x[1:]) return main()
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
R = lambda: map(int, input().split()) n, m = R() nums = sorted(R()) dp = [0] * (n + 1) acc = 0 for i, num in enumerate(nums): acc += num dp[i] = acc + (dp[i - m] if i >= m else 0) print(dp[i], end=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) l = [*map(int, input().split())] array = l.copy() array.sort() array1 = [(0) for i in range(n + 10)] array2 = array1.copy() ans = 0 for i in range(n): array1[i] += array[i] array1[i] += array2[i % m] array2[i % m] += array[i] ans += array1[i] print(ans, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) a = [int(x) for x in input().split()] a.append(0) a.sort() for i in range(m, n + 1): a[i] += a[i - m] for i in range(1, n + 1): a[i] += a[i - 1] print(a[i], end=" ")
ASSIGN 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 EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split(" ")) a = sorted(list(map(int, input().split()))) pref = [0] * n pref[0] = a[0] for i in range(1, n): pref[i] = pref[i - 1] + a[i] for i in range(n): if i >= m: pref[i] += pref[i - m] print(*pref)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) x = list(map(int, input().split())) dp, ans = list([0] * 200000), 0 x.sort() for i in range(n): if i >= m: dp[i] += dp[i - m] if i > 0: dp[i] += x[i] else: dp[0] = x[0] ans += dp[i] print(ans)
ASSIGN 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 VAR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
from itertools import accumulate n, m = map(int, input().split()) xs = sorted([int(x) for x in input().split()]) ms = [list(accumulate(xs[i::m])) for i in range(m)] ans = 0 i, j = 0, 0 for k in range(1, n + 1): ans += ms[i][j] i += 1 if i == m: i = 0 j += 1 print(ans, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = [int(i) for i in input().split(" ")] sweets = [int(i) for i in input().split(" ")] sweets.sort() track = [(0) for i in range(m)] penalty = 0 for i in range(n): j = i % m track[j] += sweets[i] penalty += track[j] print(penalty, end=" ")
ASSIGN 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() for i in range(m, n): arr[i] += arr[i - m] for i in range(1, n): arr[i] += arr[i - 1] print(*arr)
ASSIGN 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$. Yui loves sweets, but she can eat at most $m$ sweets each day for health reasons. Days are $1$-indexed (numbered $1, 2, 3, \ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 200\ 000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\ 000$). -----Output----- You have to output $n$ integers $x_1, x_2, \ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets. -----Examples----- Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Input 1 1 7 Output 7 -----Note----- Let's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ Total penalty is $1 \cdot a_1 + 1 \cdot a_4 + 2 \cdot a_5 + 2 \cdot a_3 + 3 \cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$.
n, m = map(int, input().split()) arr = sorted(list(map(int, input().split()))) R = [0] * n P = [0] * n for i in range(m): R[i] = arr[i] + (0 if i == 0 else R[i - 1]) P[i] = arr[i] for i in range(m, n): P[i] = arr[i] + P[i - m] R[i] = R[i - 1] + P[i - m] + arr[i] print(*R)
ASSIGN 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR