description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | MOD = 1000000007
OBRAT = [-1] * 2000
CC = {}
FACT = [-1] * 2000
OBRAT_FACT = [-1] * 2000
def obrat(k):
if OBRAT[k] == -1:
OBRAT[k] = fast_pow(k, MOD - 2)
return OBRAT[k]
def c(n, k):
if (n, k) in CC:
return CC[n, k]
res = fact(n)
res = res * obrat_fact(k) % MOD
res = res * obrat_fact(n - k) % MOD
CC[n, k] = res
return res
def obrat_fact(k):
if OBRAT_FACT[k] != -1:
return OBRAT_FACT[k]
res = 1
for i in range(2, k + 1):
res = res * obrat(i) % MOD
OBRAT_FACT[k] = res
return res
def fact(n):
if FACT[n] != -1:
return FACT[n]
res = 1
for i in range(2, n + 1):
res = res * i % MOD
FACT[n] = res
return res
def fast_pow(x, y):
if y == 0:
return 1
p = fast_pow(x, y // 2) % MOD
p = p * p % MOD
if y % 2:
p = p * x % MOD
return p
s = input()
k = int(input())
n = len(s)
if k == 1:
print(n - 1)
exit()
if k == 0:
print(1)
exit()
moves = [0] * 1025
moves[1] = 0
for i in range(2, 1024):
ii = i
ones = 0
while ii > 0:
if ii % 2 == 1:
ones += 1
ii //= 2
moves[i] = moves[ones] + 1
if n < 10:
d = 1
a = 0
for i in range(n - 1, -1, -1):
a += d * int(s[i])
d *= 2
res = 0
for i in range(2, a + 1):
if moves[i] == k:
res += 1
print(res)
exit()
res = 0
if moves[s.count("1")] == k - 1:
res += 1
for i in range(1, 1024):
ki = moves[i]
if ki != k - 1:
continue
m = n - 1
j = 0
while m >= i and i >= 0 and m >= 0:
res = (res + c(m, i)) % MOD
i -= 1
j += 1
while j < n and s[j] != "1":
j += 1
m = n - 1 - j
print(res) | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | mod = 10**9 + 7
def solve(n: str, k: int):
out = 0
if k == 0:
out = 1
elif k == 1:
out = len(n) - 1
elif k == 2:
for i in range(1, min(len(n), 10)):
x = 2**i
cnt = 0
for j in range(len(n)):
if n[j] == "1":
out += C(len(n) - j - 1, x - cnt)
out %= mod
cnt += 1
if n.count("1") == x:
out += 1
elif 3 <= k <= 5:
d = dict()
cnt = 0
for i in range(len(n)):
if n[i] == "1":
m = 1
for j in range(len(n) - i):
assertadd(d, j + cnt, m)
m *= len(n) - i - j - 1
m //= j + 1
cnt += 1
assertadd(d, n.count("1"), 1)
for key in d:
out += d[key] * recur(key, k - 1)
out %= mod
elif k >= 6:
out = 0
return out
def assertadd(d: dict, k, v):
try:
d[k] += v
except:
d[k] = v
def recur(k, level):
for i in range(level):
k = bin(k)[2:].count("1")
if k == 1 and i != level - 1:
return 0
return 1 if k == 1 else 0
def C(n, k):
if k < 0 or k > n:
return 0
out = 1
for i in range(k):
out *= n - i
out //= i + 1
return out % mod
n = input()
k = int(input())
print(solve(n, k)) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING VAR VAR NUMBER IF NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | n = input()
k = int(input())
MOD = int(1000000000.0 + 7)
a = {(0): 0, (1): 0, (2): 1}
for i in range(3, 1001):
s = bin(i)[2:]
ones_count = 0
for x in s:
if x == "1":
ones_count += 1
a[i] = a[ones_count] + 1
answer = 0
table = {}
def bin_coeff(N, K):
if K > N:
raise Exception
if K > N // 2:
K = N - K
if K == 0:
table[N, 0] = 1
return 1
if (N, K) not in table:
table[N, K] = N * bin_coeff(N - 1, K - 1) // K
return table[N, K]
ones = 0
for i, x in enumerate(n):
if x == "1":
good = []
N = len(n) - i - 1
min = 0 if i > 0 else 1
for j in range(min, N + 1):
if a[j + ones] == k - 1:
good.append(j)
for x in good:
answer = (answer + bin_coeff(N, x)) % MOD
if i == 0 and x == 1:
answer -= 1
ones += 1
if a[ones] == k - 1:
answer += 1
if k == 0:
answer = 1
if k == 1:
answer = len(n) - 1
answer = int(answer)
print(answer) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | dpc = [1]
dpci = []
for i in range(1, 2000):
dpc.append(dpc[i - 1] * i % 1000000007)
def modInverse(a, m):
m0 = m
y = 0
x = 1
if m == 1:
return 0
while a > 1:
q = a // m
t = m
m = a % m
a = t
t = y
y = x - q * y
x = t
if x < 0:
x = x + m0
return x
for i in dpc:
dpci.append(modInverse(i, 1000000007))
def c(n, r):
if r < 0:
return 0
if n < r:
return 0
if n < 0:
return 0
return dpc[n] * dpci[r] * dpci[n - r]
dp = {(1): 0}
def f(n):
x = n
b = 0
while x != 0:
x = x & x - 1
b += 1
dp[n] = dp[b] + 1
for i in range(2, 1001):
f(i)
a = [[], [], [], [], []]
for i in dp:
a[dp[i]].append(i)
n = input()
l = len(n)
x = []
for i in range(l):
if n[i] == "1":
x.append(i)
k = int(input())
if k > 5:
print(0)
elif k == 0:
print(1)
else:
ans = 0
for i in a[k - 1]:
r = 0
for j in x:
if i - r < 0:
break
xy = c(l - j - 1, i - r)
ans = (ans + xy) % 1000000007
r += 1
if len(x) in a[k - 1] and len(x) != 1:
ans += 1
if len(x) > 1 and k == 1:
ans -= 1
print(ans) | ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST LIST LIST LIST LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | def main():
s, k = input(), int(input())
n = len(s)
if k < 2:
print((1, n - 1)[k])
return
tais = [(n - i - 1) for i, c in enumerate(s) if c == "1"]
tlen, mod, res = len(tais), 1000000007, 0
fac, ifac, f = [1], [1], 1
for i in range(1, n + 1):
f = f * i % mod
fac.append(f)
x = v = 0
y = u = 1
a, b = f, mod
while a:
q, r = divmod(b, a)
a, b, x, y, u, v = r, a, u, v, x - u * q, y - v * q
ifac.append(x)
def c(n, m):
return fac[n] * ifac[m] * ifac[n - m] % mod if 0 <= m <= n else 0
for m in range(n, 0, -1):
x, t = m, k
while x != 1:
y = 0
t -= 1
while x:
y += x & 1
x //= 2
x = y
if t == 1:
if len(tais) > m:
del tais[m:]
res += sum(c(t, m - i) for i, t in enumerate(tais)) + (m <= tlen)
print(res % mod)
def __starting_point():
main()
__starting_point() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
mod = 1000000007
ncrtable = [[(0) for i in range(1001)] for j in range(1001)]
ncrtable[0][0] = 1
for row in range(1, 1001):
for col in range(row + 1):
if col == 0:
ncrtable[row][col] = 1
else:
ncrtable[row][col] = ncrtable[row - 1][col] + ncrtable[row - 1][col - 1]
def ncr(n, r):
if n < r:
return 0
if r < 0:
return 0
return ncrtable[n][r]
def compute(n, x):
tot = 0
y = x
for i in range(1000, -1, -1):
if 1 << i & n:
tot += ncr(i, x) % mod
tot %= mod
x -= 1
if x == 0:
return (tot + 1) % mod
return tot % mod
arr = [(0) for i in range(1002)]
arr[1] = 1
for i in range(2, 1001):
ct = 0
for j in range(32, -1, -1):
if 1 << j & i:
ct += 1
arr[i] = arr[ct] + 1
n = int(input(), 2)
k = int(input())
ans = 0
for i in range(0, 1001):
if arr[i] == k:
ans = compute(n, i) + ans
ans %= mod
if k == 1:
ans -= 1
print(ans)
exit(0)
if n >= 1001:
for i in range(0, 1001):
if arr[i] == k - 1:
ans = compute(n, i) + ans
ans %= mod
if k != 1:
print(ans)
else:
print(ans - 1) | FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains integer n (1 ≤ n < 2^1000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
-----Output-----
Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7.
-----Examples-----
Input
110
2
Output
3
Input
111111011
2
Output
169
-----Note-----
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | def Numb(a, k):
if a == 0:
return 0
m = len(bin(a)) - 3
if m + 1 < k:
return 0
if k == 1:
return m + 1
if m + 1 == k:
return Numb(a & (1 << m) - 1, k - 1)
return C[m][k] + Numb(a & (1 << m) - 1, k - 1)
s = input()
nDec = int(s, 2)
n = len(s)
k = int(input())
C = [[1], [1, 1]]
for i in range(n):
tmp = [1]
for j in range(1, i + 2):
tmp.append(C[-1][j - 1] + C[-1][j])
tmp.append(1)
C.append(tmp)
if k == 0:
print(1)
else:
NumOfOp = [(0) for i in range(n + 1)]
for i in range(2, n + 1):
NumOfOp[i] = NumOfOp[bin(i).count("1")] + 1
res = 0
for i in range(1, n + 1):
if NumOfOp[i] == k - 1:
res += Numb(nDec, i)
if k == 1:
res -= 1
print(res % (10**9 + 7)) | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input())
a = []
a1 = [int(x) for x in input().split()]
a2 = [int(x) for x in input().split()]
a3 = [int(x) for x in input().split()]
a = [[0], a1, a2, a3]
adj = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = list(map(int, input().split()))
adj[u].append(v)
adj[v].append(u)
for i in adj:
if len(i) > 2:
print(-1)
return
sumi = [(0) for i in range(7)]
g = [[(0) for j in range(n + 1)] for i in range(6)]
for i in range(n):
if len(adj[i]) == 1:
p = i
break
g[0][p] = 1
g[0][adj[p][0]] = 2
flag = 3
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[0][adj[i][0]] == 0:
g[0][adj[i][0]] = flag
flag = 6 - flag - g[0][i]
i = adj[i][0]
else:
g[0][adj[i][1]] = flag
flag = 6 - flag - g[0][i]
i = adj[i][1]
g[1][p] = 2
g[1][adj[p][0]] = 3
flag = 1
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[1][adj[i][0]] == 0:
g[1][adj[i][0]] = flag
flag = 6 - flag - g[1][i]
i = adj[i][0]
else:
g[1][adj[i][1]] = flag
flag = 6 - flag - g[1][i]
i = adj[i][1]
g[2][p] = 1
g[2][adj[p][0]] = 3
flag = 2
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[2][adj[i][0]] == 0:
g[2][adj[i][0]] = flag
flag = 6 - flag - g[2][i]
i = adj[i][0]
else:
g[2][adj[i][1]] = flag
flag = 6 - flag - g[2][i]
i = adj[i][1]
g[3][p] = 3
g[3][adj[p][0]] = 2
flag = 1
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[3][adj[i][0]] == 0:
g[3][adj[i][0]] = flag
flag = 6 - flag - g[3][i]
i = adj[i][0]
else:
g[3][adj[i][1]] = flag
flag = 6 - flag - g[3][i]
i = adj[i][1]
g[4][p] = 3
g[4][adj[p][0]] = 1
flag = 2
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[4][adj[i][0]] == 0:
g[4][adj[i][0]] = flag
flag = 6 - flag - g[4][i]
i = adj[i][0]
else:
g[4][adj[i][1]] = flag
flag = 6 - flag - g[4][i]
i = adj[i][1]
g[5][p] = 2
g[5][adj[p][0]] = 1
flag = 3
i = adj[p][0]
for _ in range(n - 1):
if len(adj[i]) == 1:
break
if g[5][adj[i][0]] == 0:
g[5][adj[i][0]] = flag
flag = 6 - flag - g[5][i]
i = adj[i][0]
else:
g[5][adj[i][1]] = flag
flag = 6 - flag - g[5][i]
i = adj[i][1]
maxi = 10**9 * n + 5
ans = 0
i = 1
for j in range(6):
sumi = 0
for i in range(1, n + 1):
sumi += a[g[j][i]][i - 1]
if maxi > sumi:
maxi = sumi
ans = j
print(maxi)
print(*g[ans][1:]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = [0] * 3
c[0] = list(map(int, input().split()))
c[1] = list(map(int, input().split()))
c[2] = list(map(int, input().split()))
deg = [0] * n
g = [0] * n
for i in range(n):
g[i] = []
for i in range(n - 1):
a, b = map(int, input().split())
g[a - 1].append(b - 1)
deg[a - 1] += 1
g[b - 1].append(a - 1)
deg[b - 1] += 1
flag = 1
k = 0
for i in range(n):
if deg[i] == 1:
k = i
if deg[i] > 2:
flag = 0
if flag == 0:
print(-1)
else:
s = [0] * 6
for i in range(6):
ver = k
vep = -1
col = i // 2
const = (i // 2 + i % 2) % 2 + 1
s[i] += c[col][ver]
for j in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + const) % 3
s[i] += c[col][ver]
print(min(s))
colours = [0] * n
ver = k
vep = -1
if s[0] == min(s):
col = 0
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 1) % 3
colours[ver] = col + 1
elif s[1] == min(s):
col = 0
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 2) % 3
colours[ver] = col + 1
elif s[2] == min(s):
col = 1
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 2) % 3
colours[ver] = col + 1
elif s[3] == min(s):
col = 1
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 1) % 3
colours[ver] = col + 1
elif s[4] == min(s):
col = 2
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 1) % 3
colours[ver] = col + 1
else:
col = 2
colours[ver] = col + 1
for i in range(n - 1):
if g[ver][0] == vep:
vep = ver
ver = g[ver][1]
else:
vep = ver
ver = g[ver][0]
col = (col + 2) % 3
colours[ver] = col + 1
print(*colours) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER 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 LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n = int(input())
Color_info = [list(map(int, input().split())) for i in range(3)]
ans = [-1] * n
G = [[] for i in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
G[u].append(v)
G[v].append(u)
if any(len(G[i]) > 2 for i in range(n)):
print(-1)
return
S = -1
for i in range(n):
if len(G[i]) == 1:
S = i
break
Path = []
prev_S = -1
for i in range(n):
Path.append(S)
for adj in G[S]:
if adj != prev_S:
prev_S = S
S = adj
break
COLOR = [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
mincost = 1 << 128
for c1, c2, c3 in COLOR:
c1 -= 1
c2 -= 1
c3 -= 1
temp_cost = 0
for i in range(n):
if i % 3 == 0:
temp_cost += Color_info[c1][Path[i]]
elif i % 3 == 1:
temp_cost += Color_info[c2][Path[i]]
else:
temp_cost += Color_info[c3][Path[i]]
if temp_cost < mincost:
mincost = temp_cost
for i in range(n):
if i % 3 == 0:
ans[Path[i]] = c1 + 1
elif i % 3 == 1:
ans[Path[i]] = c2 + 1
else:
ans[Path[i]] = c3 + 1
print(mincost)
print(*ans)
return
def main():
t = 1
for i in range(t):
slv()
return
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = [
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
]
graph = []
for i in range(n):
graph.append([])
sorted_graph = []
for i in range(n - 1):
a, b = list(map(int, input().split()))
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
if len(graph[a]) > 2 or len(graph[b]) > 2:
print(-1)
exit(0)
for j in range(n):
if len(graph[j]) == 1:
sorted_graph.append(j)
sorted_graph.append(graph[j][0])
for _ in range(n - 2):
for connection in graph[sorted_graph[-1]]:
if connection != sorted_graph[-2]:
sorted_graph.append(connection)
break
break
sorted_graph_indexes = [-1] * len(sorted_graph)
for i in range(len(sorted_graph_indexes)):
sorted_graph_indexes[sorted_graph[i]] = i
sum = -1
signsum = 0
addsum = 0
for sign in range(-1, 2, 2):
for add in range(3):
lsum = 0
for i in range(n):
lsum += c[(add + sorted_graph_indexes[i] * sign) % 3][i]
if sum == -1 or sum > lsum:
sum = lsum
signsum = sign
addsum = add
print(sum)
for i in range(n):
print((addsum + sorted_graph_indexes[i] * signsum) % 3 + 1, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER STRING |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = [list(map(int, input().split())) for _ in range(3)]
g = [[] for _ in range(n)]
l = [0] * n
for i in range(n - 1):
a, b = map(int, input().split())
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
l[a - 1] += 1
l[b - 1] += 1
if max(l) > 2:
print(-1)
return
for i in range(n):
if l[i] == 1:
s = [i]
break
dist = [-1] * n
dist[i] = 0
while s:
d = s.pop()
dist[d] %= 3
for node in g[d]:
if dist[node] == -1:
s.append(node)
dist[node] = dist[d] + 1
ans = float("inf")
for i1 in range(3):
for i2 in range(3):
if i1 == i2:
continue
for i3 in range(3):
if i1 == i3 or i2 == i3:
continue
tmp = 0
for j in range(n):
tmp += c[(i1, i2, i3)[dist[j]]][j]
if tmp < ans:
idx = i1, i2, i3
ans = tmp
print(ans)
for i in range(n - 1):
print(idx[dist[i]] + 1, end=" ")
print(idx[dist[n - 1]] + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | from itertools import permutations
n = int(input())
c = [list(map(int, input().split())) for i in range(3)]
edges = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
edges[u - 1].append(v - 1)
edges[v - 1].append(u - 1)
for i in range(n):
if len(edges[i]) > 2:
print(-1)
exit()
path = []
for i in range(n):
if len(edges[i]) == 1:
path.append(i)
break
seen = [False] * n
seen[path[-1]] = True
while True:
for i in edges[path[-1]]:
if not seen[i]:
path.append(i)
seen[i] = True
break
else:
break
d = [([0] * n) for i in range(3)]
for i in range(3):
now = 0
for j in path:
d[i][now] = c[i][j]
now += 1
ans = 10**30
now = []
for i in permutations(range(3), 3):
ans_sub = 0
now_sub = [0] * n
for j in range(n):
now_sub[path[j]] = i[j % 3] + 1
ans_sub += d[i[j % 3]][j]
if ans_sub < ans:
ans = ans_sub
now = now_sub
print(ans)
print(" ".join(map(str, now))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
input = sys.stdin.readline
n = int(input())
c1 = [int(x) for x in input().split()]
c2 = [int(x) for x in input().split()]
c3 = [int(x) for x in input().split()]
c = {"1": c1, "2": c2, "3": c3}
a = {i: [] for i in range(1, n + 1)}
for i in range(n - 1):
x, y = [int(x) for x in input().split()]
a[x].append(y)
a[y].append(x)
start = 0
bad = False
for k, v in a.items():
l = len(v)
if l > 2:
bad = True
break
elif l == 1:
start = k
if bad:
print(-1)
else:
prev = start
cur = a[start][0]
b = [prev]
while True:
b.append(cur)
if len(a[cur]) == 1:
break
nxt = [x for x in a[cur] if x != prev][0]
prev = cur
cur = nxt
ms = sum(c1) + sum(c2) + sum(c3)
best_per = ""
for per in {"123", "132", "231", "213", "312", "321"}:
s = 0
ci = 0
for cur in b:
cvet = per[ci]
s += c[cvet][cur - 1]
ci = (ci + 1) % 3
if s < ms:
ms = s
best_per = per
print(ms)
ci = 0
res = ["" for i in range(n + 1)]
for cur in b:
cvet = best_per[ci]
res[cur] = cvet
ci = (ci + 1) % 3
for i in range(1, n + 1):
print(res[i], end=" ") | IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
color = []
for i in range(3):
color.append(list(map(int, input().split())))
graph = {}
degree = {}
color_assign = {}
for i in range(1, n + 1):
graph[i] = []
degree[i] = 0
color_assign[i] = -1
flag = 0
for i in range(n - 1):
a, b = map(int, input().split())
degree[a] += 1
degree[b] += 1
graph[a].append(b)
graph[b].append(a)
if degree[a] > 2 or degree[b] > 2:
flag = 1
break
if flag == 1:
print(-1)
else:
starting_node = -1
for keys in degree:
if degree[keys] == 1:
starting_node = keys
break
sumo = float("inf")
for i in range(3):
for j in range(3):
if i == j:
continue
for k in range(3):
if j == k or i == k:
continue
else:
s1 = 0
first = starting_node
second = graph[first][0]
third = graph[second][0]
if graph[second][0] == first:
third = graph[second][1]
count = 1
temp_color = {}
while count <= n // 3:
s1 += (
color[i][first - 1]
+ color[j][second - 1]
+ color[k][third - 1]
)
temp_color[first] = i + 1
temp_color[second] = j + 1
temp_color[third] = k + 1
if count == n // 3:
break
first = graph[third][0]
if second == first:
first = graph[third][1]
second = graph[first][0]
if second == third:
second = graph[first][1]
third = graph[second][0]
if third == first:
third = graph[second][1]
count += 1
if n % 3 == 1:
first = graph[third][0]
if second == first:
first = graph[third][1]
s1 += color[i][first - 1]
temp_color[first] = i + 1
if n % 3 == 2:
first = graph[third][0]
if second == first:
first = graph[third][1]
second = graph[first][0]
if second == third:
second = graph[first][1]
s1 += color[i][first - 1] + color[j][second - 1]
temp_color[first] = i + 1
temp_color[second] = j + 1
if s1 < sumo:
sumo = s1
for keys in color_assign:
color_assign[keys] = temp_color[keys]
print(sumo)
for keys in color_assign:
print(color_assign[keys], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | def is_possible(adj, n):
for i in range(n):
if len(adj[i]) >= 3:
return False
return True
def color_pattern(adj, n):
stack = []
for i in range(n):
if len(adj[i]) == 1:
stack.append(i)
break
dist = [(-1) for i in range(n)]
dist[i] = 0
while stack:
curr = stack.pop()
for n in adj[curr]:
if dist[n] == -1:
stack.append(n)
dist[n] = (dist[curr] + 1) % 3
return dist
def try_for_colors(n, adj, costs, pattern, seq):
colors = []
cost = 0
for i in range(n):
cost += costs[seq[pattern[i]] - 1][i]
colors.append(seq[pattern[i]])
return cost, colors
n = int(input())
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
c3 = list(map(int, input().split()))
costs = [c1, c2, c3]
adj = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
adj[u].append(v)
adj[v].append(u)
if is_possible(adj, n):
pattern = color_pattern(adj, n)
min_cost, min_colors = try_for_colors(n, adj, costs, pattern, [1, 2, 3])
poss = [[1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
for p in poss:
cost, colors = try_for_colors(n, adj, costs, pattern, p)
if cost < min_cost:
min_cost = cost
min_colors = colors
print(min_cost)
print(" ".join(map(str, min_colors)))
else:
print(-1) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | class Vertex:
def __init__(self, index):
self.index = index
self.neighbours = []
self.color = None
def main():
n = int(input())
cols = []
cols.append([int(z) for z in input().split()])
cols.append([int(z) for z in input().split()])
cols.append([int(z) for z in input().split()])
tree = [Vertex(i) for i in range(n)]
for _ in range(n - 1):
u1, u2 = [(int(p) - 1) for p in input().split()]
tree[u1].neighbours.append(u2)
tree[u2].neighbours.append(u1)
if any(len(vertex.neighbours) > 2 for vertex in tree):
print("-1")
return
root = None
for vertex in tree:
if len(vertex.neighbours) == 1:
root = vertex
break
current_group = 1
prev_vertex = root.index
vertex = tree[root.neighbours[0]]
groups = [[root.index], [], []]
while True:
is_leaf = len(vertex.neighbours) == 1
groups[current_group].append(vertex.index)
if is_leaf:
break
current_group = (current_group + 1) % 3
next_vertex_index = sum(vertex.neighbours) - prev_vertex
prev_vertex = vertex.index
vertex = tree[next_vertex_index]
best_col_per_group = None
best_price = None
for col_per_group in [
[1, 2, 3],
[1, 3, 2],
[2, 1, 3],
[2, 3, 1],
[3, 1, 2],
[3, 2, 1],
]:
price = 0
for group_id in range(len(groups)):
group_col = col_per_group[group_id] - 1
for node_id in groups[group_id]:
price += cols[group_col][node_id]
if best_price is None or price < best_price:
best_price = price
best_col_per_group = col_per_group
print(best_price)
for color, group in zip(best_col_per_group, groups):
for node_id in group:
tree[node_id].color = color
for node in tree:
print(node.color, end=" ")
main() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NONE FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST VAR LIST LIST WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | from itertools import chain
from sys import stdin, stdout
def main():
from sys import stdin, stdout
n = int(stdin.readline())
from itertools import chain
c1 = tuple(map(int, input().split()))
c2 = tuple(map(int, input().split()))
c3 = tuple(map(int, input().split()))
tree = tuple([] for _ in range(n))
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
tree[u].append(v)
tree[v].append(u)
if len(tree[u]) > 2 or len(tree[v]) > 2:
print("-1")
break
else:
for u in range(n):
if len(tree[u]) < 2:
v = u
break
ans = [0] * n
ansi = 1
cost = tuple([0, 0, 0] for _ in range(4))
w = 0
for _ in range(n):
ans[v] = ansi
cost[1][ansi] += c1[v]
cost[2][ansi] += c2[v]
cost[3][ansi] += c3[v]
ansi = (ansi + 1) % 3
if len(tree[v]) == 1:
w = v
(v,) = tree[v]
else:
v, w = next(x for x in tree[v] if x != w), v
pal = min(
(
(p1, p2, p3, cost[p1][0] + cost[p2][1] + cost[p3][2])
for p1 in (1, 2, 3)
for p2 in (1, 2, 3)
if p1 != p2
for p3 in (6 - p1 - p2,)
),
key=lambda x: x[3],
)
print(pal[3])
print(*(pal[ansi] for ansi in ans))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
c3 = list(map(int, input().split()))
c = [c1, c2, c3]
graph = [[] for i in range(n)]
for i in range(n - 1):
v1, v2 = map(int, input().split())
graph[v1 - 1].append(v2 - 1)
graph[v2 - 1].append(v1 - 1)
for i in range(n):
if len(graph[i]) > 2:
print(-1)
break
else:
for start in range(n):
if len(graph[start]) == 1:
ans = 10**18
anscol = []
for col in range(3):
v = start
used = [0] * n
used[v] = 1
colors = [0] * n
colors[v] = col + 1
cost = c[col][v]
col = (col + 1) % 3
for i in range(n - 1):
if not used[graph[v][0]]:
v = graph[v][0]
elif not used[graph[v][1]]:
v = graph[v][1]
used[v] = 1
cost += c[col][v]
colors[v] = col + 1
col = (col + 1) % 3
if cost < ans:
ans = cost
anscol = colors[:]
v = start
used = [0] * n
used[v] = 1
colors = [0] * n
colors[v] = col + 1
cost = c[col][v]
col = (col - 1) % 3
for i in range(n - 1):
if not used[graph[v][0]]:
v = graph[v][0]
elif not used[graph[v][1]]:
v = graph[v][1]
used[v] = 1
cost += c[col][v]
colors[v] = col + 1
col = (col - 1) % 3
if cost < ans:
ans = cost
anscol = colors[:]
print(ans)
print(*anscol)
break
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
input = sys.stdin.readline
n = int(input())
C = []
C.append(list(map(int, input().split())))
C.append(list(map(int, input().split())))
C.append(list(map(int, input().split())))
E = [[] for i in range(n)]
COUNT = [0] * n
for i in range(n - 1):
x, y = map(int, input().split())
E[x - 1].append(y - 1)
E[y - 1].append(x - 1)
COUNT[x - 1] += 1
COUNT[y - 1] += 1
if max(COUNT) >= 3:
print(-1)
sys.exit()
for i in range(n):
if len(E[i]) == 1:
start = i
break
LIST = [start, E[start][0]]
USE = [0] * n
USE[start] = 1
USE[E[start][0]] = 1
while True:
now = LIST[-1]
if len(E[now]) == 1:
break
for x in E[now]:
if USE[x] == 0:
USE[x] = 1
LIST.append(x)
ANS = 1 << 60
MINABC = [-1, -1, -1]
for a, b, c in [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]:
score = 0
for i in range(n):
if i % 3 == 0:
score += C[a][LIST[i]]
elif i % 3 == 1:
score += C[b][LIST[i]]
else:
score += C[c][LIST[i]]
if ANS > score:
ANS = score
MINABC = [a, b, c]
print(ANS)
ANSLIST = [0] * n
a, b, c = MINABC
for i in range(n):
if i % 3 == 0:
ANSLIST[LIST[i]] = a + 1
elif i % 3 == 1:
ANSLIST[LIST[i]] = b + 1
else:
ANSLIST[LIST[i]] = c + 1
print(*ANSLIST) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input().strip())
c1 = list(map(int, input().strip().split()))
c2 = list(map(int, input().strip().split()))
c3 = list(map(int, input().strip().split()))
c = [c1, c2, c3]
g = [[] for i in range(n)]
d = [(0) for i in range(n)]
for i in range(n - 1):
x, y = map(int, input().strip().split())
g[x - 1].append(y - 1)
g[y - 1].append(x - 1)
d[x - 1] += 1
d[y - 1] += 1
if max(d) >= 3:
print(-1)
sys.exit(0)
vis = [(False) for i in range(n)]
l = []
for idx, d in enumerate(d):
if d == 1:
start = idx
break
l.append(start)
vis[start] = True
start = g[start][0]
l.append(start)
vis[start] = True
while len(g[start]) == 2:
x, y = g[start]
if vis[x] == True:
x, y = y, x
l.append(x)
vis[x] = True
start = x
t = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
cost = []
for type_ in t:
tc = 0
for idx, i in enumerate(l):
tc += c[type_[idx % 3]][i]
cost.append((tc, type_))
temp = min(cost)
print(temp[0])
ans = [(-1) for i in range(n)]
for idx, i in enumerate(l):
ans[i] = temp[1][idx % 3] + 1
print(*ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | from sys import setrecursionlimit, stdin
setrecursionlimit(10**7)
def iin():
return int(stdin.readline())
def lin():
return list(map(int, stdin.readline().split()))
def BFS(s, adj):
parent = {s: None}
n = len(adj)
color = [-1] * n
u = [s]
ch = 0
while u:
nextu = []
for i in u:
for v in adj[i]:
if v not in parent:
parent[v] = i
color[v] = ch % 3
ch += 1
nextu.append(v)
u = nextu.copy()
return color
def main():
n = iin()
color = [lin(), lin(), lin()]
adj = [[] for i in range(n)]
for _ in range(n - 1):
i, j = lin()
i -= 1
j -= 1
adj[i].append(j)
adj[j].append(i)
S = 0
for i in range(n):
if len(adj[i]) > 2:
print(-1)
return
if len(adj[i]) == 1:
S = i
c1 = BFS(S, adj)
pat = [[1, 2, 3], [2, 3, 1], [3, 2, 1], [1, 3, 2], [2, 1, 3], [3, 1, 2]]
sol = [-1, 0]
for p in pat:
ch = 0
for i in range(n):
ch += color[p[c1[i]] - 1][i]
if sol[0] == -1 or sol[0] > ch:
sol = [ch, p]
c1 = [sol[1][c1[i]] for i in range(n)]
print(sol[0])
print(*c1)
main() | EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input())
c1 = [int(i) for i in input().split()]
c2 = [int(i) for i in input().split()]
c3 = [int(i) for i in input().split()]
ccc = [c1, c2, c3]
cnt = [0] * (n + 1)
neigh = [[] for i in range(n + 1)]
for j in range(n - 1):
a, b = [int(i) for i in sys.stdin.readline().split()]
cnt[a] += 1
cnt[b] += 1
neigh[a].append(b)
neigh[b].append(a)
leaf = -1
for i in range(1, n + 1):
if cnt[i] == 1:
leaf = i
elif cnt[i] > 2:
print(-1)
exit()
color = [-1] * (n + 1)
v = leaf
cc = 0
for i in range(n):
color[v] = cc
cc = (cc + 1) % 3
ne = neigh[v]
if color[ne[0]] == -1:
v = ne[0]
elif len(ne) > 1:
v = ne[1]
mn = sum(c1) + sum(c2) + sum(c3)
ch = [(0, 1, 2), (0, 2, 1), (1, 2, 0), (1, 0, 2), (2, 0, 1), (2, 1, 0)]
mni = -1
for i in range(6):
pt = ch[i]
cost = 0
for v in range(1, n + 1):
rc = pt[color[v]]
cost += ccc[rc][v - 1]
if cost < mn:
mni = i
mn = cost
print(mn)
pt = ch[mni]
ans = []
for v in range(1, n + 1):
rc = pt[color[v]]
ans.append(rc + 1)
print(*ans) | IMPORT 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | def main():
n = int(input())
color1 = [int(elm) for elm in input().split(" ")]
color2 = [int(elm) for elm in input().split(" ")]
color3 = [int(elm) for elm in input().split(" ")]
graph = {}
for i in range(n):
graph[i] = []
num_adjs = [(0) for i in range(n)]
for _ in range(n - 1):
p, q = map(int, input().split(" "))
p -= 1
q -= 1
graph[p].append(q)
graph[q].append(p)
num_adjs[p] += 1
num_adjs[q] += 1
if max(num_adjs) >= 3:
print(-1)
return
node_type = [(0) for i in range(n)]
first_nodes = [i for i, num_adj in enumerate(num_adjs) if num_adj == 1]
if len(first_nodes) != 2:
print(-1)
return
first_node, end_node = first_nodes
junban = [first_node]
parent = first_node
jibun = graph[first_node][0]
count = 0
for i in range(n - 1):
junban.append(jibun)
if i == n - 2:
pass
kids = graph[jibun]
for kid in kids:
if parent != kid:
parent = jibun
jibun = kid
break
count += 1
colors = [color1, color2, color3]
min_tensu = sum(color1) + sum(color2) + sum(color3)
color_orders = [[0, 1, 2], [0, 2, 1], [1, 2, 0], [1, 0, 2], [2, 0, 1], [2, 1, 0]]
for ci, color_order in enumerate(color_orders):
cur_tensu = 0
node_colors = [(-1) for i in range(n)]
for i, node_i in enumerate(junban):
i = i % 3
node_color = color_order[i]
cost = colors[node_color][node_i]
node_colors[node_i] = node_color
cur_tensu += cost
if cur_tensu < min_tensu:
min_ci = ci
min_tensu = cur_tensu
min_node_colors = node_colors
print(min_tensu)
print(" ".join([str(elm + 1) for elm in min_node_colors]))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
wie = []
wie.append([-1, -1, -1])
tab = [list(map(int, input().split())) for i in range(3)]
for i in range(n):
wie.append([tab[0][i], tab[1][i], tab[2][i]])
d = {}
for i in range(1, n + 1):
d[i] = []
for j in range(n - 1):
v1, v2 = map(int, input().split())
d[v1].append(v2)
d[v2].append(v1)
no = 1
for i in d:
if len(d[i]) > 2:
print(-1)
no = 0
break
if no == 1:
lista = []
kon = 0
for i in range(1, n + 1):
if len(d[i]) == 1:
kon = i
break
i = kon
pop = -1
s = 1
rev = [0] * (n + 1)
while s == 1:
cur = i
lista.append(i)
rev[i] = len(lista) - 1
for v in d[i]:
if v != pop:
i = v
break
pop = cur
if len(d[i]) == 1:
lista.append(i)
rev[i] = len(lista) - 1
s = 0
dupy = []
dupy.append([-1, -1, -1])
for i in lista:
dupy.append(wie[i])
wynik = 23658267823658763485
opti = []
for i in range(3):
for j in range(3):
if i == j:
continue
res = 0
res += dupy[1][i]
res += dupy[2][j]
c1 = i
c2 = j
kolory = [i, j]
pos = 3
while pos < n + 1:
kol = 3 - c1 - c2
kolory += [kol]
res += dupy[pos][kol]
c1 = c2
c2 = kol
pos += 1
wynik = min(wynik, res)
if wynik == res:
opti = kolory
print(wynik)
kolorki = [0] * (n + 1)
for i in range(1, n + 1):
kolorki[i] = opti[rev[i]] + 1
print(*kolorki[1:]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL 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 VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
from itertools import permutations
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 list(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 = float("inf")
MOD = 10**9 + 7
N = INT()
C1 = LIST()
C2 = LIST()
C3 = LIST()
nodes = [[] for i in range(N)]
for i in range(N - 1):
a, b = MAP()
a -= 1
b -= 1
nodes[a].append(b)
nodes[b].append(a)
start = 0
for i in range(N):
if len(nodes[i]) >= 3:
print(-1)
return
if len(nodes[i]) == 1:
start = i
Cs = [[] for i in range(3)]
stack = [(start, -1)]
while stack:
u, prev = stack.pop()
Cs[0].append(C1[u])
Cs[1].append(C2[u])
Cs[2].append(C3[u])
for v in nodes[u]:
if v != prev:
stack.append((v, u))
mn = INF
p = []
for perm in permutations(list(range(3))):
sm = 0
for i in range(N):
sm += Cs[perm[i % 3]][i]
if mn > sm:
mn = min(mn, sm)
p = perm
ans = [0] * N
stack = [(start, -1, 0)]
while stack:
u, prev, cnt = stack.pop()
ans[u] = p[cnt % 3] + 1
for v in nodes[u]:
if v != prev:
stack.append((v, u, cnt + 1))
print(mn)
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 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 FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input())
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
c3 = list(map(int, input().split()))
ans = -1
edges = {}
degree = [0] * n
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
degree[u] += 1
degree[v] += 1
if u not in edges:
edges[u] = [v]
else:
edges[u].append(v)
if v not in edges:
edges[v] = [u]
else:
edges[v].append(u)
if max(degree) > 2:
print(-1)
sys.exit()
bfs = []
for i in range(n):
if degree[i] == 1:
bfs.append(i)
break
visited = [0] * n
visited[bfs[0]] = 1
cost123 = c1[bfs[0]]
cost132 = c1[bfs[0]]
cost213 = c2[bfs[0]]
cost231 = c2[bfs[0]]
cost312 = c3[bfs[0]]
cost321 = c3[bfs[0]]
turn = 1
colors = [0] * n
colors[bfs[0]] = 1
while bfs:
temp = []
for u in bfs:
for v in edges[u]:
if visited[v] == 0:
colors[v] = turn + 1
visited[v] = 1
temp.append(v)
if turn == 0:
cost123 += c1[v]
cost132 += c1[v]
cost213 += c2[v]
cost231 += c2[v]
cost312 += c3[v]
cost321 += c3[v]
if turn == 1:
cost123 += c2[v]
cost132 += c3[v]
cost213 += c1[v]
cost231 += c3[v]
cost312 += c1[v]
cost321 += c2[v]
if turn == 2:
cost123 += c3[v]
cost132 += c2[v]
cost213 += c3[v]
cost231 += c1[v]
cost312 += c2[v]
cost321 += c1[v]
bfs = temp
turn += 1
turn = turn % 3
x = min(cost123, cost132, cost213, cost231, cost312, cost321)
print(x)
if x == cost132:
for i in range(n):
if colors[i] == 3:
colors[i] = 2
elif colors[i] == 2:
colors[i] = 3
elif x == cost213:
for i in range(n):
if colors[i] == 1:
colors[i] = 2
elif colors[i] == 2:
colors[i] = 1
elif x == cost231:
for i in range(n):
if colors[i] == 1:
colors[i] = 2
elif colors[i] == 2:
colors[i] = 3
else:
colors[i] = 1
elif x == cost312:
for i in range(n):
if colors[i] == 1:
colors[i] = 3
elif colors[i] == 2:
colors[i] = 1
else:
colors[i] = 2
elif x == cost321:
for i in range(n):
if colors[i] == 1:
colors[i] = 3
elif colors[i] == 3:
colors[i] = 1
print(*colors, sep=" ") | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
r1 = input()
r1 = r1.split()
r2 = input()
r2 = r2.split()
r3 = input()
r3 = r3.split()
costs = list()
vertices = list()
powers = list()
i = 0
while i < n:
costs.append(list())
costs[i].append(int(r1[i]))
costs[i].append(int(r2[i]))
costs[i].append(int(r3[i]))
powers.append(0)
vertices.append(list())
i = i + 1
i = 0
lim = n - 1
while i < lim:
edge = input()
edge = edge.split()
edge[0] = int(edge[0]) - 1
edge[1] = int(edge[1]) - 1
vertices[edge[0]].append(edge[1])
vertices[edge[1]].append(edge[0])
powers[edge[0]] = powers[edge[0]] + 1
powers[edge[1]] = powers[edge[1]] + 1
i = i + 1
possibility = True
i = 0
while i < n:
if powers[i] > 2 or powers[i] == 0:
possibility = False
break
i = i + 1
if possibility == False:
print(-1)
else:
tree = list()
i = 0
while i < n:
tree.append(None)
i = i + 1
i = 0
while i < n:
if powers[i] == 1:
tree[0] = i
tree[1] = vertices[i][0]
break
i = i + 1
i = 1
lim = n - 1
while i < lim:
if vertices[tree[i]][0] != tree[i - 1]:
tree[i + 1] = vertices[tree[i]][0]
else:
tree[i + 1] = vertices[tree[i]][1]
i = i + 1
min = -1
right_cache = list()
amount = 0
cache = list()
i = 0
while i < n:
cache.append(None)
i = i + 1
i = 0
while i < 3:
j = 0
while j < 3:
if j == i:
j = j + 1
continue
cache[tree[0]] = i
cache[tree[1]] = j
amount = costs[tree[0]][i] + costs[tree[1]][j]
prev_colors = list()
prev_colors.append(i)
prev_colors.append(j)
k = 2
while k < n:
cur_color = 3 - prev_colors[0] - prev_colors[1]
cache[tree[k]] = cur_color
amount = amount + costs[tree[k]][cur_color]
prev_colors[0] = prev_colors[1]
prev_colors[1] = cur_color
k = k + 1
if min == -1:
min = amount
right_cache = cache.copy()
elif amount < min:
min = amount
right_cache = cache.copy()
j = j + 1
i = i + 1
print(min)
i = 0
while i < n:
print(right_cache[i] + 1, end=" ")
i = i + 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | def dfs(colors, leaf, edges):
stack = [edges[leaf][0]]
visited = set()
n = len(edges)
visited.add(leaf)
used = {}
for i in range(1, n + 1):
used[i] = []
used[edges[leaf][0]].append(colors[leaf])
used[edges[leaf][0]].append(colors[edges[leaf][0]])
while stack:
curr = stack.pop()
if curr not in visited:
visited.add(curr)
for kid in edges[curr]:
if kid not in visited:
stack.append(kid)
poss = [1, 2, 3]
for i in used[curr]:
poss.remove(i)
colors[kid] = poss[0]
used[kid].append(colors[kid])
used[kid].append(colors[curr])
def cost(colors, color1, color2, color3):
c = 0
colors.pop(0)
n = len(colors)
for i in range(n):
if colors[i] == 1:
c += color1[i]
elif colors[i] == 2:
c += color2[i]
else:
c += color3[i]
return c
def solve(edges, leaf, color1, color2, color3):
min_cost = float("inf")
perms = [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
n = len(edges)
ans = []
for i in perms:
colors = [0] * (n + 1)
colors[leaf] = i[0]
colors[edges[leaf][0]] = i[1]
dfs(colors, leaf, edges)
c = cost(colors, color1, color2, color3)
if c < min_cost:
min_cost = c
ans = colors[:]
print(min_cost)
for i in ans:
print(i, end=" ")
def main():
n = int(input())
color1 = list(map(int, input().split()))
color2 = list(map(int, input().split()))
color3 = list(map(int, input().split()))
edges = {}
for i in range(1, n + 1):
edges[i] = []
found = True
leaf = -1
for i in range(n - 1):
a, b = map(int, input().split())
edges[a].append(b)
edges[b].append(a)
if len(edges[a]) > 2:
found = False
if len(edges[b]) > 2:
found = False
if not found:
print(-1)
return
for i in range(1, n + 1):
if len(edges[i]) == 1:
leaf = i
break
solve(edges, leaf, color1, color2, color3)
main() | FUNC_DEF ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = [([0] + list(map(int, input().split()))) for _ in range(3)]
e = [set() for u in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
e[u].add(v)
e[v].add(u)
for i in range(1, 1 + n):
if len(e[i]) > 2:
print(-1)
exit()
for i in range(1, n + 1):
if len(e[i]) == 1:
S = i
break
s = [S, next(iter(e[S]))]
for i in range(n - 2):
u = s[-1]
for v in e[u]:
if v == s[-2]:
continue
s.append(v)
break
ans, ans_d = 10**100, []
d = [(0) for _ in range(n + 1)]
for i in range(3):
for j in range(3):
if i != j:
d[s[0]] = i + 1
d[s[1]] = j + 1
for k in range(2, n):
d[s[k]] = 6 - (d[s[k - 1]] + d[s[k - 2]])
w = sum(map(lambda u: c[d[u] - 1][u], range(1, n + 1)))
if w < ans:
ans = w
ans_d = d[:]
print(ans)
print(*ans_d[1:]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = list()
edges = [[] for _ in range(n)]
for k in range(3):
c.append([int(x) for x in input().split()])
for _ in range(n - 1):
a, b = [(int(x) - 1) for x in input().split()]
edges[a].append(b)
edges[b].append(a)
flag = True
ends = list()
for i in range(n):
if len(edges[i]) > 2:
flag = False
if len(edges[i]) == 1:
ends.append(i)
if flag:
group = [-1] * n
tmp = ends[0]
group[tmp] = 0
count = 1
while tmp != ends[1]:
for x in edges[tmp]:
if group[x] == -1:
tmp = x
group[tmp] = count % 3
count += 1
coloredSum = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(n):
coloredSum[group[i]][0] += c[0][i]
coloredSum[group[i]][1] += c[1][i]
coloredSum[group[i]][2] += c[2][i]
ans = coloredSum[0][0] + coloredSum[1][1] + coloredSum[2][2]
grouptocolour = [1, 2, 3]
if coloredSum[0][0] + coloredSum[1][2] + coloredSum[2][1] < ans:
ans = coloredSum[0][0] + coloredSum[1][2] + coloredSum[2][1]
grouptocolour = [1, 3, 2]
if coloredSum[0][1] + coloredSum[1][0] + coloredSum[2][2] < ans:
ans = coloredSum[0][1] + coloredSum[1][0] + coloredSum[2][2]
grouptocolour = [2, 1, 3]
if coloredSum[0][1] + coloredSum[1][2] + coloredSum[2][0] < ans:
ans = coloredSum[0][1] + coloredSum[1][2] + coloredSum[2][0]
grouptocolour = [2, 3, 1]
if coloredSum[0][2] + coloredSum[1][0] + coloredSum[2][1] < ans:
ans = coloredSum[0][2] + coloredSum[1][0] + coloredSum[2][1]
grouptocolour = [3, 1, 2]
if coloredSum[0][2] + coloredSum[1][1] + coloredSum[2][0] < ans:
ans = coloredSum[0][2] + coloredSum[1][1] + coloredSum[2][0]
grouptocolour = [3, 2, 1]
print(ans)
for x in group:
print(grouptocolour[x], end=" ")
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
c = []
for i in range(3):
c.append(list(map(int, input().split())))
graph = {}
for i in range(1, n + 1):
graph[i] = []
for i in range(n - 1):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(1, n + 1):
if len(graph[i]) > 2:
print(-1)
break
if len(graph[i]) == 1:
root = i
else:
ls = [root, graph[root][0]]
root = graph[root][0]
while len(ls) != n:
for i in graph[root]:
if i != ls[-2]:
ls.append(i)
root = i
break
perm = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
for lis in perm:
N = n - len(lis)
for i in range(N):
lis.append(lis[i % 3])
cost = []
for lis in perm:
temp = 0
for i in range(n):
temp += c[lis[i] - 1][ls[i] - 1]
cost.append(temp)
m = min(cost)
print(m)
for i in range(6):
if cost[i] == m:
ls = {ls[j]: perm[i][j] for j in range(n)}
for i in range(1, n + 1):
print(ls[i], end=" ")
print()
break | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
input = sys.stdin.readline
n = int(input())
c = [list(map(int, input().split())) for i in range(3)]
g = [[] for i in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
g[u - 1].append(v - 1)
g[v - 1].append(u - 1)
for i in range(n):
if len(g[i]) >= 3:
print(-1)
exit()
v = g[0][0]
ans = 10**18
ans_b = []
for c0 in range(3):
for cv in range(3):
if c0 == cv:
continue
cost = 0
b = [-1] * n
b[0] = c0
b[v] = cv
cost += c[c0][0] + c[cv][v]
q = [[v, 0, cv, c0], [0, v, c0, cv]]
while q:
par, ver, cx, cy = q.pop()
for to in g[ver]:
if par == to:
continue
if b[to] == -1:
cz = 3 - cx - cy
b[to] = cz
cost += c[cz][to]
q.append([ver, to, cy, cz])
if cost < ans:
ans = cost
ans_b = b
print(ans)
for i in range(len(ans_b)):
ans_b[i] += 1
print(*ans_b) | 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST LIST VAR NUMBER VAR VAR LIST NUMBER VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
input = sys.stdin.readline
n = int(input())
cost = [tuple(map(int, input().split())) for _ in range(3)]
adj_list = [[] for _ in range(n)]
for _ in range(n - 1):
ui, vi = map(int, input().split())
adj_list[ui - 1].append(vi - 1)
adj_list[vi - 1].append(ui - 1)
for i in range(n):
if len(adj_list[i]) >= 3:
print(-1)
exit()
if len(adj_list[i]) == 1:
root = i
nodes = [root]
prev = None
for i in range(n - 1):
for nn in adj_list[nodes[-1]]:
if nn != prev:
nodes.append(nn)
prev = nodes[-2]
ans_cost = 10**18
ans_l = None
for c0, c1 in [(0, 1), (1, 0), (1, 2), (2, 1), (2, 0), (0, 2)]:
cost_cand = 0
color = [-1] * n
color[nodes[0]] = c0
color[nodes[1]] = c1
cost_cand += cost[c0][nodes[0]] + cost[c1][nodes[1]]
for i in range(2, n):
s = set([color[nodes[i - 2]], color[nodes[i - 1]]])
if s == {0, 1}:
color[nodes[i]] = 2
cost_cand += cost[2][nodes[i]]
elif s == {1, 2}:
color[nodes[i]] = 0
cost_cand += cost[0][nodes[i]]
if s == {2, 0}:
color[nodes[i]] = 1
cost_cand += cost[1][nodes[i]]
if ans_cost > cost_cand:
ans_cost = cost_cand
ans_l = color
print(ans_cost)
for ans_li in ans_l[:-1]:
print(ans_li + 1, end=" ")
print(ans_l[-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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NONE FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | def main():
n = int(input())
costs = [
list(map(int, input().split(" "))),
list(map(int, input().split(" "))),
list(map(int, input().split(" "))),
]
nodes = []
for i in range(0, n):
nodes.append([])
for i in range(0, n - 1):
node_1_index, node_2_index = map(int, input().split(" "))
nodes[node_1_index - 1].append(node_2_index - 1)
nodes[node_2_index - 1].append(node_1_index - 1)
for node in nodes:
if len(node) > 2:
print(-1)
return
init_node_index = -1
for i in range(0, n):
if len(nodes[i]) == 1:
init_node_index = i
break
minimum_node_colors = []
minimum_cost = -1
for i in range(0, 3):
for j in range(0, 3):
if i == j:
continue
nodes_colors = [0] * n
prev_node_index = init_node_index
nodes_colors[prev_node_index] = i
current_node_index = nodes[prev_node_index][0]
nodes_colors[current_node_index] = j
while True:
next_node_index = get_next_node(
prev_node_index, nodes[current_node_index]
)
nodes_colors[next_node_index] = (
3 - nodes_colors[prev_node_index] - nodes_colors[current_node_index]
)
prev_node_index = current_node_index
current_node_index = next_node_index
if len(nodes[next_node_index]) == 1:
break
cost = calculate_weight(nodes_colors, costs)
if minimum_cost == -1 or minimum_cost > cost:
minimum_node_colors = nodes_colors
minimum_cost = cost
print(minimum_cost)
print(" ".join([str(value + 1) for value in minimum_node_colors]))
def calculate_weight(nodes, costs):
total = 0
for i in range(0, len(nodes)):
total += costs[nodes[i]][i]
return total
def get_next_node(prev_node_index, node):
for index in node:
if prev_node_index != index:
return index
return -1
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c1 = input().split()
c2 = input().split()
c3 = input().split()
for i in range(n):
c1[i] = int(c1[i])
c2[i] = int(c2[i])
c3[i] = int(c3[i])
a = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
a[u - 1].append(v - 1)
a[v - 1].append(u - 1)
count1 = 0
count2 = 0
j = 0
for i in range(n):
if len(a[i]) == 1:
j = i
count1 += 1
if len(a[i]) == 2:
count2 += 1
if count1 == 2 and count2 == n - 2:
b = []
check = [(0) for i in range(n)]
while len(b) < n:
b.append(j)
check[j] = 1
if len(b) == n:
break
if check[a[j][0]] == 0:
j = a[j][0]
else:
j = a[j][1]
ans1 = 0
color1 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans1 += c1[b[i]]
color1[b[i]] = 1
elif i % 3 == 1:
ans1 += c2[b[i]]
color1[b[i]] = 2
else:
ans1 += c3[b[i]]
color1[b[i]] = 3
ans2 = 0
color2 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans2 += c3[b[i]]
color2[b[i]] = 3
elif i % 3 == 1:
ans2 += c1[b[i]]
color2[b[i]] = 1
else:
ans2 += c2[b[i]]
color2[b[i]] = 2
ans3 = 0
color3 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans3 += c2[b[i]]
color3[b[i]] = 2
elif i % 3 == 1:
ans3 += c3[b[i]]
color3[b[i]] = 3
else:
ans3 += c1[b[i]]
color3[b[i]] = 1
ans4 = 0
color4 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans4 += c1[b[i]]
color4[b[i]] = 1
elif i % 3 == 1:
ans4 += c3[b[i]]
color4[b[i]] = 3
else:
ans4 += c2[b[i]]
color4[b[i]] = 2
ans5 = 0
color5 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans5 += c3[b[i]]
color5[b[i]] = 3
elif i % 3 == 1:
ans5 += c2[b[i]]
color5[b[i]] = 2
else:
ans5 += c1[b[i]]
color5[b[i]] = 1
ans6 = 0
color6 = [(0) for i in range(n)]
for i in range(n):
if i % 3 == 0:
ans6 += c2[b[i]]
color6[b[i]] = 2
elif i % 3 == 1:
ans6 += c1[b[i]]
color6[b[i]] = 1
else:
ans6 += c3[b[i]]
color6[b[i]] = 3
if ans1 == min(ans1, ans2, ans3, ans4, ans5, ans6):
print(ans1)
print(*color1)
elif ans2 == min(ans1, ans2, ans3, ans4, ans5, ans6):
print(ans2)
print(*color2)
elif ans3 == min(ans1, ans2, ans3, ans4, ans5, ans6):
print(ans3)
print(*color3)
elif ans4 == min(ans1, ans2, ans3, ans4, ans5, ans6):
print(ans4)
print(*color4)
elif ans5 == min(ans1, ans2, ans3, ans4, ans5, ans6):
print(ans5)
print(*color5)
else:
print(ans6)
print(*color6)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
cost = []
for _ in range(3):
cost.append(list(map(int, input().split())))
deg = [0] * n
adj = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
adj[u].append(v)
adj[v].append(u)
deg[u] += 1
deg[v] += 1
if sorted(deg) != [1, 1] + [2] * (n - 2):
print(-1)
else:
best = None
best_colors = None
root = 0
while deg[root] != 1:
root += 1
for c1 in range(3):
for c2 in range(3):
if c1 == c2:
continue
c3 = 0
while c3 in [c1, c2]:
c3 += 1
idx = [c1, c2, c3]
parent = -1
cur = root
j = 0
cur_cost = 0
coloring = [-1] * n
while True:
i = 0
cur_cost += cost[idx[j % 3]][cur]
coloring[cur] = idx[j % 3] + 1
while i < len(adj[cur]) and adj[cur][i] == parent:
i += 1
if i == len(adj[cur]):
break
parent = cur
cur = adj[cur][i]
j += 1
if best is None or cur_cost < best:
best = cur_cost
best_colors = coloring
print(best)
print(*best_colors) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR LIST VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE NUMBER ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
edge = [[] for _ in range(n)]
costs = [[] for _ in range(3)]
for i in range(3):
costs[i] = [int(item) for item in input().split()]
for i in range(n - 1):
u, v = [(int(item) - 1) for item in input().split()]
edge[u].append(v)
edge[v].append(u)
beggining = None
for i, e in enumerate(edge):
l = len(e)
if l > 2:
print(-1)
exit()
elif beggining is None and l == 1:
beggining = i
place = []
visited = [0] * n
node = beggining
while node != -1:
place.append(node)
visited[node] = 1
ok = False
for v in edge[node]:
if visited[v]:
continue
node = v
ok = True
if not ok:
break
c_ans = [0] * n
color = [0] * n
ans = 10**18
for init_c in range(3):
ret = 0
c = init_c
for item in place:
ret += costs[c][item]
color[item] = c + 1
c = (c + 1) % 3
if ret < ans:
ans = ret
c_ans = color[:]
for init_c in range(3):
ret = 0
c = init_c
for item in place:
ret += costs[c][item]
color[item] = c + 1
c = (c + 2) % 3
if ret < ans:
ans = ret
c_ans = color[:]
print(ans)
print(" ".join([str(item) for item in c_ans])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NONE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = [a, b, c]
s = {}
s1 = set()
for _ in range(n - 1):
x1, x2 = map(int, input().split())
x3 = s.get(x1, 0)
if x3 == 0:
s[x1] = {x2}
s1.add(x1)
else:
x3.add(x2)
s1.remove(x1) if x1 in s1 else None
if len(x3) > 2:
print(-1)
exit(0)
else:
s[x1] = x3
x4 = s.get(x2, 0)
if x4 == 0:
s[x2] = {x1}
s1.add(x2)
else:
x4.add(x1)
s1.remove(x2) if x2 in s1 else None
if len(x4) > 2:
print(-1)
exit(0)
else:
s[x2] = x4
s2 = s1.pop()
s3 = [a[s2 - 1], a[s2 - 1], b[s2 - 1], b[s2 - 1], c[s2 - 1], c[s2 - 1]]
flag = [[1, 1, 2, 2, 3, 3], [2, 3, 1, 3, 1, 2], [3, 2, 3, 1, 2, 1]]
f = 1
used = {s2}
p = [s2]
while 1:
x5 = s.get(s2)
x6 = x5 - used
if len(x6) == 0:
r1 = min(s3)
r2 = s3.index(r1)
path = ["1 2 3 ", "1 3 2 ", "2 1 3 ", "2 3 1 ", "3 1 2 ", "3 2 1 "]
print(r1)
p1 = (path[r2] * (n // 3) + path[r2][: n % 3 * 2]).split(" ")[:-1]
p2 = zip(*sorted(zip(p, p1)))
print(" ".join(list(p2)[1]))
exit(0)
else:
x7 = x6.pop()
p.append(x7)
for i in range(6):
s3[i] += d[flag[f][i] - 1][x7 - 1]
f = (f + 1) % 3
s2 = x7
used.add(x7) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR VAR VAR FUNC_CALL VAR VAR NONE IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR VAR VAR FUNC_CALL VAR VAR NONE IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
C = []
C.append(list(map(int, input().split())))
C.append(list(map(int, input().split())))
C.append(list(map(int, input().split())))
D = dict()
for i in range(n - 1):
g, h = list(map(int, input().split()))
if g in D:
D[g] += [h]
else:
D[g] = [h]
if h in D:
D[h] += [g]
else:
D[h] = [g]
if max(list(map(len, D.values()))) >= 3:
print(-1)
else:
seq = []
for i in D:
if len(D[i]) == 1:
seq.append(i)
break
A = set()
A.add(seq[0])
for i in range(n - 1):
if D[seq[i]][0] in A:
seq.append(D[seq[i]][1])
A.add(D[seq[i]][1])
else:
A.add(D[seq[i]][0])
seq.append(D[seq[i]][0])
S = dict()
S[123] = 0
S[132] = 0
S[213] = 0
S[231] = 0
S[321] = 0
S[312] = 0
for i in range(n):
S[123] += C[i % 3][seq[i] - 1]
S[312] += C[(i + 2) % 3][seq[i] - 1]
S[231] += C[(i + 1) % 3][seq[i] - 1]
S[321] += C[(2 - i) % 3][seq[i] - 1]
S[213] += C[(1 - i) % 3][seq[i] - 1]
S[132] += C[(0 - i) % 3][seq[i] - 1]
MinIndex = 123
for j in S:
if S[j] < S[MinIndex]:
MinIndex = j
print(S[MinIndex])
Ans = []
Ans.append(MinIndex // 100)
Ans.append(MinIndex // 10 % 10)
Ans.append(MinIndex % 10)
b = list()
for i in range(n):
b.append(0)
for i in range(n):
b[seq[i] - 1] = Ans[i % 3]
for i in range(n):
print(b[i], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR LIST VAR IF VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR LIST VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
def main():
n = int(input())
c0 = readIntArr()
c1 = readIntArr()
c2 = readIntArr()
adj = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = readIntArr()
u -= 1
v -= 1
adj[u].append(v)
adj[v].append(u)
root = -1
for i in range(n):
if len(adj[i]) >= 3:
print(-1)
return
if len(adj[i]) == 1:
root = i
arr = [-1] * n
curr = root
for i in range(n):
arr[i] = curr
if i + 1 < n:
if i > 0 and adj[curr][0] == arr[i - 1]:
curr = adj[curr][1]
else:
curr = adj[curr][0]
cr = [[-1, -1, -1] for _ in range(n)]
for i in range(n):
cr[i][0] = c0[arr[i]]
cr[i][1] = c1[arr[i]]
cr[i][2] = c2[arr[i]]
schemes = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
minTotal = inf
minScheme = [-1, -1, -1]
for scheme in schemes:
total = 0
for i in range(n):
total += cr[i][scheme[i % 3]]
if total < minTotal:
minTotal = total
minScheme = scheme
print(minTotal)
ans = [-1] * n
for i in range(n):
ans[arr[i]] = minScheme[i % 3] + 1
oneLineArrayPrint(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(defaultValFactory, dimensionArr):
dv = defaultValFactory
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP 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 FUNC_CALL 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 |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input())
costs = [list(map(int, input().split())) for _ in range(3)]
deg = [0] * n
adj = [[] for _ in range(n)]
for u, v in (map(int, l.split()) for l in sys.stdin):
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
deg[u - 1] += 1
deg[v - 1] += 1
if max(deg) > 2:
print(-1)
exit()
s = deg.index(1)
t = deg.index(1, s + 1)
inf = 10**18
dp = [[([inf] * 3) for _ in range(3)] for _ in range(n + 1)]
for i in range(3):
for j in range(3):
dp[n][i][j] = 0
v, child = t, n
childs = [-1] * n
while True:
for i in range(3):
for j in range(3):
if i == j:
continue
for k in range(3):
if i == k or j == k:
continue
dp[v][i][j] = min(dp[v][i][j], dp[child][j][k] + costs[i][v])
for dest in adj[v]:
if dest == child:
continue
child = v
v = dest
childs[v] = child
break
else:
break
color = [0] * n
ans = inf
x, y = 0, 0
for i in range(3):
for j in range(3):
if ans > dp[s][i][j]:
ans = dp[s][i][j]
x, y = i, j
v = s
while v != -1:
color[v] = x + 1
for z in range(3):
if x != z and y != z:
x, y = y, z
v = childs[v]
break
print(ans)
print(*color) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
color = []
for j in range(3):
color.append(list(map(int, input().split())))
adj = [[] for i in range(n + 1)]
for j in range(n - 1):
u, v = map(int, input().split())
adj[u].append(v)
adj[v].append(u)
ones = 0
two = 0
for j in range(1, n + 1):
l = len(adj[j])
if l == 1:
ones += 1
strt = j
elif l == 2:
two += 1
if ones == 2 and two == n - 2:
j = strt
vis = [0] * (n + 1)
dp = [
[[[[-1, -1], float("inf")] for u in range(3)] for k in range(3)]
for i in range(n + 1)
]
for k in range(3):
for q in range(3):
if q != k:
dp[j][k][q][1] = color[k][j - 1]
vis = [0] * (n + 1)
vis[j] = 1
j = adj[strt][0]
prev = strt
i = 1
while i < n:
vis[j] = 1
for k in range(3):
for q in range(3):
if k != q:
for u in range(3):
if u != q and u != k:
if dp[j][k][q][1] > dp[prev][q][u][1] + color[k][j - 1]:
dp[j][k][q][1] = dp[prev][q][u][1] + color[k][j - 1]
dp[j][k][q][0] = [q, u]
prev = j
for ne in adj[prev]:
if vis[ne] == 0:
j = ne
i += 1
res = float("inf")
col = [0] * n
for k in range(3):
for q in range(3):
if res > dp[prev][k][q][1]:
col[prev - 1] = k + 1
ner = q
res = dp[prev][k][q][1]
vis = [0] * (n + 1)
j = prev
vis[j] = 1
i = 1
while i < n:
vis[j] = 1
prev = j
for ne in adj[prev]:
if vis[ne] == 0:
j = ne
col[j - 1] = dp[prev][col[prev - 1] - 1][ner][0][0] + 1
ner = dp[prev][col[prev - 1] - 1][ner][0][1]
i += 1
print(res)
print(*col)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER LIST VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
sys.setrecursionlimit(10**9)
maxi = 0
def rint():
return map(int, sys.stdin.readline().split())
def input():
return sys.stdin.readline().rstrip("\n")
def oint():
return int(input())
def bfs(a):
global maxi
bool[a] = True
queue = [a]
level = [0] * (n + 1)
level[a] = 0
leveli[0].append(a)
while queue:
z = queue.pop(0)
for i in hash[z]:
if not bool[i]:
bool[i] = True
queue.append(i)
level[i] = level[z] + 1
leveli[level[i]].append(i)
maxi = max(level[i], maxi)
n = oint()
c = []
for i in range(3):
c.append(list(rint()))
l1, l2, l3 = c
hash = []
for i in range(n + 1):
hash.append([])
bool = [False] * (n + 1)
leveli = []
for i in range(n):
leveli.append([])
for i in range(n - 1):
a, b = map(int, input().split())
hash[a].append(b)
hash[b].append(a)
perm = [[0, 1, 2], [1, 0, 2], [1, 2, 0], [2, 1, 0], [2, 0, 1], [0, 2, 1]]
count = 0
s = -1
for i in range(1, n + 1):
if len(hash[i]) == 1:
s = i
break
if s == -1:
print(-1)
exit()
bfs(s)
level = 0
mini = 10**18
for i in range(len(leveli)):
if len(leveli[i]) > 1:
print(-1)
exit()
yo = 0
for i in range(len(perm)):
level = 0
count = 0
cost = 0
while level <= maxi:
count = count % 3
for j in leveli[level]:
z = perm[i][count]
if z == 0:
cost += l1[j - 1]
elif z == 1:
cost += l2[j - 1]
else:
cost += l3[j - 1]
level += 1
count += 1
if cost < mini:
yo = i
mini = cost
level = 0
count = 0
cost = 0
ans = [0] * n
while level <= maxi:
count = count % 3
for j in leveli[level]:
z = perm[yo][count]
ans[j - 1] = z + 1
level += 1
count += 1
print(mini)
print(*ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | import sys
n = int(input())
vs = []
for i in range(0, 3):
ln = [int(j) for j in sys.stdin.readline().rstrip().split(" ")]
vs.append(ln)
vt = {}
f = True
for i in range(0, n - 1):
edg = [int(j) for j in sys.stdin.readline().rstrip().split(" ")]
if edg[0] in vt:
vt[edg[0]].append(edg[1])
else:
vt[edg[0]] = [edg[1]]
if edg[1] in vt:
vt[edg[1]].append(edg[0])
else:
vt[edg[1]] = [edg[0]]
st = -1
for i in vt:
if len(vt[i]) > 2:
f = False
break
if len(vt[i]) == 1:
st = i
if f:
combos = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
m = -1
ms = -1
for i in range(0, len(combos)):
cost = 0
seq = [0] * n
vert = st
vis = {}
for j in range(0, len(vs[0])):
vis[vert] = True
cost += vs[combos[i][j % 3] - 1][vert - 1]
seq[vert - 1] = combos[i][j % 3]
vert = vt[vert]
if j == len(vs[0]) - 1:
break
if vert[0] in vis:
vert = vert[1]
else:
vert = vert[0]
if m == -1 or cost < m:
m = cost
ms = seq
print(m)
print(" ".join([str(i) for i in ms]))
else:
print(-1) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | n = int(input())
c = []
c.append([int(x) for x in input().split()])
c.append([int(x) for x in input().split()])
c.append([int(x) for x in input().split()])
ngbr = {}
flag = 1
for i in range(n - 1):
i, j = [int(x) for x in input().split()]
if i not in ngbr:
ngbr[i] = [j]
else:
ngbr[i].append(j)
if j not in ngbr:
ngbr[j] = [i]
else:
ngbr[j].append(i)
if len(ngbr[i]) > 2 or len(ngbr[j]) > 2:
flag = 0
if not flag:
print(-1)
else:
right_order = []
for el in ngbr:
if len(ngbr[el]) == 1:
cur = el
break
nex = ngbr[cur][0]
while len(ngbr[nex]) != 1:
right_order.append(cur)
for nex2 in ngbr[nex]:
if nex2 != cur:
break
cur = nex
nex = nex2
right_order.extend([ngbr[nex][0], nex])
sum = [[(0) for i in range(3)] for j in range(3)]
for i in range(3):
for j in range(3):
for el in right_order[i::3]:
sum[i][j] += c[j][el - 1]
min_sum = sum[0][0] + sum[1][1] + sum[2][2]
right_colors = [0, 1, 2]
for i in set(range(3)):
for j in set(range(3)) - {i}:
for k in set(range(3)) - {i} - {j}:
if sum[0][i] + sum[1][j] + sum[2][k] < min_sum:
min_sum = sum[0][i] + sum[1][j] + sum[2][k]
right_colors = [i, j, k]
print(min_sum)
result_color = [(0) for i in range(n)]
for i in range(len(right_order)):
result_color[right_order[i] - 1] = right_colors[i % 3]
for i in result_color:
print(i + 1, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR |
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree.
You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.
You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.
You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
-----Input-----
The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.
The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.
The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.
The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.
Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.
-----Output-----
If there is no good painting, print $-1$.
Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.
-----Examples-----
Input
3
3 2 3
4 3 2
3 1 3
1 2
2 3
Output
6
1 3 2
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 3
Output
-1
Input
5
3 4 2 1 2
4 2 1 5 4
5 3 2 1 1
1 2
3 2
4 3
5 4
Output
9
1 3 2 1 3
-----Note-----
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex — into color $3$, and the third vertex — into color $2$. The cost of this painting is $3 + 2 + 1 = 6$. | from itertools import permutations
def cost(start, C):
prev = start
color = 0
mycost = C[color][start]
color = (color + 1) % 3
cur = tree[start][0]
for _ in range(n - 1):
glnext[prev] = cur
mycost += C[color][cur]
if prev != tree[cur][0]:
prev = cur
cur = tree[cur][0]
elif len(tree[cur]) > 1:
prev = cur
cur = tree[cur][1]
color = (color + 1) % 3
return mycost
n = int(input())
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
c3 = list(map(int, input().split()))
C = [c1, c2, c3]
tree = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
tree[u].append(v)
tree[v].append(u)
for node in tree:
if len(node) > 2:
print(-1)
exit(0)
for i in range(n):
if len(tree[i]) == 1:
start = i
break
best = 999999999999999999
glnext = [-1] * n
for p in permutations(range(3)):
Cperm = [C[p[0]], C[p[1]], C[p[2]]]
cst = cost(start, Cperm)
if cst < best:
best = cst
ans = [-1] * n
cur = start
col = 0
for _ in range(n):
ans[cur] = p[col] + 1
cur = glnext[cur]
col = (col + 1) % 3
print(best)
print(" ".join(list(map(str, ans)))) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero. | for _ in range(int(input())):
s = str(input())
dp = [0, 0, len(s), len(s), len(s), len(s)]
for i in s:
if i == "1":
dp[3] = min(dp[3], dp[0])
dp[0] += 1
dp[5] = min(dp[5], dp[2])
dp[2] += 1
dp[4] += 1
else:
dp[2] = min(dp[2], dp[1])
dp[1] += 1
dp[4] = min(dp[4], dp[3])
dp[3] += 1
dp[5] += 1
print(min(dp)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero. | t = int(input())
while t > 0:
st = input()
dp = [(0 if i < 2 else len(st)) for i in range(6)]
for c in st:
if c == "1":
dp[3] = min(dp[3], dp[1])
dp[1] += 1
dp[4] = min(dp[4], dp[2])
dp[2] += 1
dp[5] += 1
else:
dp[2] = min(dp[2], dp[0])
dp[0] += 1
dp[5] = min(dp[5], dp[3])
dp[3] += 1
dp[4] += 1
print(min(dp))
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero. | for _ in range(int(input())):
s = input()
prev = None
l = []
S = ""
cnt = 0
for i in s:
if i != prev:
if cnt != 0:
l.append(cnt)
prev = i
S += i
cnt = 1
else:
cnt += 1
l.append(cnt)
if len(l) <= 3:
print(0)
else:
ans = 0
n = len(l)
suf = [None] * (n + 2)
suf[n] = suf[n + 1] = 0
for i in range(n - 1, -1, -1):
suf[i] = suf[i + 2] + l[i]
curr1 = 0
for i in range(0, n, 2):
curr1 += l[i]
curr2 = 0
for j in range(i + 1, n, 2):
curr2 += l[j]
ans = max(ans, curr1 + curr2 + suf[j + 1])
curr1 = 0
for i in range(1, n, 2):
curr1 += l[i]
curr2 = 0
for j in range(i + 1, n, 2):
curr2 += l[j]
ans = max(ans, curr1 + curr2 + suf[j + 1])
print(len(s) - ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero. | t = int(input())
for _ in range(t):
s = input()
n = len(s)
o, z, oz, zo, ozo, zoz = 0, 0, 0, n, n, n
for i in s:
if i == "1":
zo = min(zo, z)
z = z + 1
ozo = min(ozo, oz)
oz = oz + 1
zoz = zoz + 1
else:
oz = min(oz, o)
o = o + 1
zoz = min(zoz, zo)
zo = zo + 1
ozo = ozo + 1
print(min(o, z, oz, zo, ozo, zoz)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR |
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero. | T = int(input())
for _ in range(T):
s = input()
l = []
a = 0
b = 0
for i in s:
if i == "1":
if a != 0:
l.append(a)
a = 0
b += 1
else:
if b != 0:
l.append(b)
b = 0
a -= 1
if a != 0:
l.append(a)
if b != 0:
l.append(b)
A = [0]
B = [0]
for i in l:
if i < 0:
A.append(-i + A[-1])
B.append(B[-1])
else:
A.append(A[-1])
B.append(i + B[-1])
m = 0
x = len(A) - 1
for i in range(1, x + 1):
for j in range(i, x + 1):
m = max(
m,
A[j] - A[i - 1] + B[i - 1] + B[x] - B[j],
B[j] - B[i - 1] + A[i - 1] + A[x] - A[j],
)
print(len(s) - m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | import sys
TESTING = False
def reverse(a, l, r):
res = list(a)
while l < r:
res[l], res[r] = res[r], res[l]
l += 1
r -= 1
return res
def inversions(a):
res = 0
for l in range(len(a)):
for r in range(l + 1, len(a)):
if a[l] > a[r]:
res += 1
return res
def solve():
n, k = read()
a = read()
al = list()
al.append(list(a))
for kk in range(k):
newal = list()
for val in al:
for l in range(n):
for r in range(l, n):
newal.append(reverse(val, l, r))
al = newal
res = 0
for val in al:
res += inversions(val)
return res / len(al)
def read(mode=2):
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2:
return list(map(int, inputs.split()))
def write(s="\n"):
if s is None:
s = ""
if isinstance(s, list):
s = " ".join(map(str, s))
s = str(s)
print(s, end="")
def run():
if TESTING:
sys.stdin = open("test.txt")
res = solve()
write(res)
run() | IMPORT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NONE ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | def genrevs(n):
ans = []
for l in range(n):
for r in range(l, n):
ans.append((l, r))
return ans
def cnt_rev(p):
n = len(p)
ans = 0
for i in range(n):
for j in range(i + 1, n):
if p[i] > p[j]:
ans += 1
return ans
def repl(l, r):
global p
for c in range((r - l + 1) // 2):
p[l + c], p[r - c] = p[r - c], p[l + c]
n, k = map(int, input().split())
p = list(map(int, input().split()))
rev = genrevs(n)
lr = len(rev)
tr = []
ans = 0
if k == 1:
for i in range(lr):
repl(rev[i][0], rev[i][1])
ans += cnt_rev(p)
repl(rev[i][0], rev[i][1])
print(ans / lr)
if k == 2:
for k1 in range(lr):
repl(rev[k1][0], rev[k1][1])
for k2 in range(lr):
repl(rev[k2][0], rev[k2][1])
ans += cnt_rev(p)
repl(rev[k2][0], rev[k2][1])
repl(rev[k1][0], rev[k1][1])
print(ans / lr**2)
if k == 3:
for k1 in range(lr):
repl(rev[k1][0], rev[k1][1])
for k2 in range(lr):
repl(rev[k2][0], rev[k2][1])
for k3 in range(lr):
repl(rev[k3][0], rev[k3][1])
ans += cnt_rev(p)
repl(rev[k3][0], rev[k3][1])
repl(rev[k2][0], rev[k2][1])
repl(rev[k1][0], rev[k1][1])
print(ans / lr**3)
if k == 4:
for k1 in range(lr):
repl(rev[k1][0], rev[k1][1])
for k2 in range(lr):
repl(rev[k2][0], rev[k2][1])
for k3 in range(lr):
repl(rev[k3][0], rev[k3][1])
for k4 in range(lr):
repl(rev[k4][0], rev[k4][1])
ans += cnt_rev(p)
repl(rev[k4][0], rev[k4][1])
repl(rev[k3][0], rev[k3][1])
repl(rev[k2][0], rev[k2][1])
repl(rev[k1][0], rev[k1][1])
print(ans / lr**4) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | f = lambda: map(int, input().split())
n, k = f()
p = list(f())
r = range
u = [(l * l + l >> 1) for l in r(n + 1)]
v = [(i, j) for i in r(n) for j in r(i + 1, n)]
t = [[(p[i] > p[j]) for j in r(n)] for i in r(n)]
a = [([0] * n) for i in r(n)]
b = [([0] * n) for i in r(n)]
c = [([0] * n) for i in r(n)]
for l in r(min(k, 1000)):
for j in r(1, n):
s, x = 0, a[j]
for i in r(j):
s += t[i][j]
x[i + 1] = x[i] + s
for i in r(n):
s, y = 0, b[i]
for j in r(n - 1, i, -1):
s += t[i][j]
y[j - 1] = y[j] + s
for d in r(1, n):
s, z = 0, c[d]
for i in r(n - d):
s += t[i][i + d]
z[i + 1] = z[i] + s
for i, j in v:
d = j - i
x, y, z = a[j], b[i], c[d]
s = t[i][j] * (u[i] + u[d - 1] + u[n - j - 1])
s += x[j] - x[i] - x[d - 1]
s += y[i] - y[j] - y[n - d]
s += (i + 1) * (n - j) - z[n - d] + z[n - j - 1] + z[i]
t[i][j] = s / u[n]
print(sum(t[i][j] for i, j in v)) | 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 ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | n, k = map(int, input().split())
p = list(map(int, input().split()))
def count_invs(a):
ans = 0
for i in range(n - 1):
for j in range(i + 1, n):
if a[i] > a[j]:
ans += 1
return ans
def inv_in_perms(a, count):
if count > 0:
ans = 0
for l in range(n):
for r in range(l, n):
a[l : r + 1] = a[l : r + 1][::-1]
ans += inv_in_perms(a, count - 1)
a[l : r + 1] = a[l : r + 1][::-1]
return ans
else:
return count_invs(a)
total = (n * (n + 1) // 2) ** k
perms = 0
print(inv_in_perms(p, k) / total) | 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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | def main():
def rec(k):
nonlocal S, N
if k == 0:
for l in range(n):
for r in range(l + 1, n):
if per[l] > per[r]:
S += 1
N += 1
return
for l in range(n):
for r in range(l + 1, n + 1):
per[l:r] = reversed(per[l:r])
rec(k - 1)
per[l:r] = reversed(per[l:r])
n, k = [int(i) for i in input().split()]
per = [int(i) for i in input().split()]
S = N = 0
rec(k)
print(S / N)
main() | FUNC_DEF FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR 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 VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | def ofsum(num):
num2 = 0
for i in range(num + 1):
num2 += i
return num2
def strspace(k):
spaces = 11 - len(str(k)) + len(str(int(k)))
return str(k) + "0" * spaces
def inversion(array):
num = 0
z = len(array)
for i in range(z):
for j in range(i + 1, z):
if array[i] > array[j]:
num += 1
return num
def reversede(array):
z = len(array)
for i in range(len(array) // 2):
temp = array[i]
array[i] = array[z - i - 1]
array[z - i - 1] = temp
return array
n, k = list(map(int, input().split()))
pn = list(map(int, input().split()))
const = 10000 / float(ofsum(n) ** k)
answer = 0
if k == 1:
for i in range(n):
for j in range(i, n):
pn2 = pn[:i] + reversede(pn[i : j + 1]) + pn[j + 1 :]
answer += inversion(pn2) * const
elif k == 2:
for i in range(n):
for j in range(i, n):
pn2 = pn[:i] + reversede(pn[i : j + 1]) + pn[j + 1 :]
for i1 in range(n):
for j1 in range(i1, n):
pn3 = pn2[:i1] + reversede(pn2[i1 : j1 + 1]) + pn2[j1 + 1 :]
answer += inversion(pn3) * const
elif k == 3:
for i in range(n):
for j in range(i, n):
pn2 = pn[:i] + reversede(pn[i : j + 1]) + pn[j + 1 :]
for i1 in range(n):
for j1 in range(i1, n):
pn3 = pn2[:i1] + reversede(pn2[i1 : j1 + 1]) + pn2[j1 + 1 :]
for i2 in range(n):
for j2 in range(i2, n):
pn4 = pn3[:i2] + reversede(pn3[i2 : j2 + 1]) + pn3[j2 + 1 :]
answer += inversion(pn4) * const
elif k == 4:
for i in range(n):
for j in range(i, n):
pn2 = pn[:i] + reversede(pn[i : j + 1]) + pn[j + 1 :]
for i1 in range(n):
for j1 in range(i1, n):
pn3 = pn2[:i1] + reversede(pn2[i1 : j1 + 1]) + pn2[j1 + 1 :]
for i2 in range(n):
for j2 in range(i2, n):
pn4 = pn3[:i2] + reversede(pn3[i2 : j2 + 1]) + pn3[j2 + 1 :]
for i3 in range(n):
for j3 in range(i3, n):
pn5 = (
pn4[:i3]
+ reversede(pn4[i3 : j3 + 1])
+ pn4[j3 + 1 :]
)
answer += inversion(pn5) * const
print(answer / 10000) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR 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 BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | def f(p):
ans = 0
for i in range(len(p)):
for j in range(i, len(p)):
if p[i] > p[j]:
ans += 1
return ans
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
num = 0
cnt = 0
if k == 1:
tmp = []
for i in range(n):
for j in range(i + 1):
tmp = p[0:j] + list(reversed(p[j : i + 1])) + p[i + 1 :]
num += 1
cnt += f(tmp)
elif k == 2:
tmp1 = []
tmp2 = []
for i in range(n):
for j in range(i + 1):
tmp1 = p[0:j] + list(reversed(p[j : i + 1])) + p[i + 1 :]
for i1 in range(n):
for j1 in range(i1 + 1):
tmp2 = (
tmp1[0:j1] + list(reversed(tmp1[j1 : i1 + 1])) + tmp1[i1 + 1 :]
)
num += 1
cnt += f(tmp2)
elif k == 3:
tmp1 = []
tmp2 = []
tmp3 = []
for i in range(n):
for j in range(i + 1):
tmp1 = p[0:j] + list(reversed(p[j : i + 1])) + p[i + 1 :]
for i1 in range(n):
for j1 in range(i1 + 1):
tmp2 = (
tmp1[0:j1] + list(reversed(tmp1[j1 : i1 + 1])) + tmp1[i1 + 1 :]
)
for i2 in range(n):
for j2 in range(i2 + 1):
num += 1
tmp3 = (
tmp2[0:j2]
+ list(reversed(tmp2[j2 : i2 + 1]))
+ tmp2[i2 + 1 :]
)
cnt += f(tmp3)
elif k == 4:
tmp1 = []
tmp2 = []
tmp3 = []
tmp4 = []
for i in range(n):
for j in range(i + 1):
tmp1 = p[0:j] + list(reversed(p[j : i + 1])) + p[i + 1 :]
for i1 in range(n):
for j1 in range(i1 + 1):
tmp2 = (
tmp1[0:j1] + list(reversed(tmp1[j1 : i1 + 1])) + tmp1[i1 + 1 :]
)
for i2 in range(n):
for j2 in range(i2 + 1):
tmp3 = (
tmp2[0:j2]
+ list(reversed(tmp2[j2 : i2 + 1]))
+ tmp2[i2 + 1 :]
)
for i3 in range(n):
for j3 in range(i3 + 1):
tmp4 = (
tmp3[0:j3]
+ list(reversed(tmp3[j3 : i3 + 1]))
+ tmp3[i3 + 1 :]
)
num += 1
cnt += f(tmp4)
print(cnt / num) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR 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 NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | n, k = [int(x) for x in input().split()]
base = [int(x) for x in input().split()]
def invert_count(l):
swaps = 0
for i in range(len(l)):
while i > 0 and l[i - 1] > l[i]:
temp = l[i - 1]
l[i - 1] = l[i]
l[i] = temp
swaps += 1
i -= 1
return swaps
def make_reflects(base, rem=k):
if rem == 0:
return invert_count(base)
else:
ans = 0
for i in range(len(base)):
for j in range(i + 1, len(base)):
new = base[:i] + list(reversed(base[i : j + 1])) + base[j + 1 :]
ans += make_reflects(new, rem - 1)
ans += n * make_reflects(base, rem - 1)
return ans
total = make_reflects(base)
reflect_number = n * (n + 1) / 2
total_number = reflect_number**k
ans = total / total_number
print("{0:.10f}".format(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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | f = lambda: map(int, input().split())
g = lambda k: k * k - k >> 1
n, k = f()
p = list(f())
a = [([0] * n) for i in range(n)]
for i in range(n):
for j in range(i + 1, n):
if p[i] > p[j]:
a[i][j] = 1
else:
a[j][i] = 1
for t in range(k):
b = [([0] * n) for i in range(n)]
for i in range(n):
for j in range(i + 1, n):
p = q = 0
for x in range(j):
d = min(i + 1, j - x, x + 1, j - i)
p += d * a[x][j]
q += d
for y in range(i + 1, n):
d = min(n - j, y - i, n - y, j - i)
p += d * a[i][y]
q += d
for s in range(j, i + n):
x, y = s - i, s - j
d = min(i + 1, n - j, y + 1, n - x)
p += d * a[x][y]
q += d
d = g(j - i) + g(i + 1) + g(n - j)
b[i][j] = (p + d * a[i][j]) / (d + q)
a = b
for i in range(n):
for j in range(i + 1, n):
a[j][i] = 1 - a[i][j]
s = 0
for i in range(n):
for j in range(i + 1, n):
s += a[i][j]
print(s) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | def memo(f):
def _f(*args):
key = str(args)
try:
return cache[key]
except KeyError:
data = cache[key] = f(*args)
return data
cache = {}
return _f
def count_inversions(lo, hi):
if hi - lo <= 1:
return 0, nums[lo:hi]
mid = (lo + hi) // 2
invs, (nums1, nums2) = list(
zip(count_inversions(lo, mid), count_inversions(mid, hi))
)
invs = sum(invs)
new_nums = []
i1 = i2 = 0
l1, l2 = list(map(len, (nums1, nums2)))
for _ in range(l1 + l2):
if i1 == l1:
new_nums.append(nums2[i2])
i2 += 1
elif i2 == l2:
new_nums.append(nums1[i1])
i1 += 1
elif nums1[i1] <= nums2[i2]:
new_nums.append(nums1[i1])
i1 += 1
else:
new_nums.append(nums2[i2])
i2 += 1
invs += l1 - i1
return invs, new_nums
@memo
def search(depth, nums):
if depth >= max_depth:
invs, _ = count_inversions(0, len(nums))
return invs, 1
else:
invs = total = 0
depth += 1
for i in range(length):
for j in range(i + 1, length + 1):
nums[i:j] = nums[i:j][::-1]
invs, total = list(
map(sum, list(zip(search(depth, nums), (invs, total))))
)
nums[i:j] = nums[i:j][::-1]
return invs, total
MAX = 1000000000.0
length, max_depth = list(map(int, input().split()))
nums = list(map(int, input().split()))
invs, total = search(0, nums)
print(invs / total) | FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT RETURN VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER 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 VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.
-----Input-----
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold.
-----Output-----
Output the answer with absolute or relative error no more than 1e - 9.
-----Examples-----
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
-----Note-----
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. | R = lambda: map(int, input().split())
n, k = R()
arr = list(R())
q, tq = [arr], []
while k:
cur = q.pop(0)
for i in range(n):
for j in range(i + 1):
tq.append(cur[:j] + cur[j : i + 1][::-1] + cur[i + 1 :])
if not q:
q, tq = tq, []
k -= 1
res = 0
for ar in q:
cnt = 0
for i in range(n):
for j in range(i):
cnt += ar[j] > ar[i]
res += cnt / len(q)
print(res) | 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 LIST VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def max_floor(eggs, drops):
return sum(max_floor(eggs - 1, d) + 1 for d in range(drops)) if eggs else 0
def solve(emulator):
floor = 1
while emulator.eggs and emulator.drops:
while emulator.drops:
test = floor + max_floor(emulator.eggs - 1, emulator.drops - 1) + 1
if emulator.drop(test):
break
floor = test
return floor + 1 | FUNC_DEF RETURN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | flockhash = {}
def solve(emulator):
num_drops = emulator.drops
num_eggs = emulator.eggs
def climbing(eggs, drops):
if eggs == 1:
return 1
everest = flockhash.get((eggs, drops))
if everest != None:
return everest
everest = 1
for kamikaze in range(1, drops):
everest += climbing(eggs - 1, drops - kamikaze)
flockhash[eggs, drops] = everest
return everest
last_broken_egg = 1
floor = 1 + climbing(num_eggs, num_drops)
exfloor = 1
while num_eggs and num_drops:
if emulator.drop(floor):
last_broken_egg = floor
num_eggs -= 1
floor = exfloor
else:
exfloor = floor
num_drops -= 1
floor += climbing(num_eggs, num_drops)
else:
return last_broken_egg | ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def solve(emulator):
num_drops = emulator.drops
num_eggs = emulator.eggs
floor = 0
def height(eggs, drops):
max_floor = 0
if eggs <= 0:
return 0
elif eggs == 1:
return drops
else:
for x in range(1, drops + 1):
max_floor += height(eggs - 1, drops - x) + 1
return max_floor
while num_drops and num_eggs:
temp = height(num_eggs - 1, num_drops - 1) + 1
if not emulator.drop(floor + temp):
floor += temp
num_drops -= 1
else:
num_eggs -= 1
num_drops -= 1
return floor + 1 | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def solve(emulator):
print("Drops")
print(emulator.drops)
num_drops = emulator.drops
print("Eggs")
print(emulator.eggs)
num_eggs = emulator.eggs
currFloor = 0
broke = False
maxFloor = -1
minFloor = 0
print(next_jump(num_eggs + 1, num_drops + 1))
while emulator.drops > 0 and emulator.eggs > 0:
if emulator.eggs == 1:
currFloor += 1
print(currFloor)
broke = emulator.drop(currFloor)
if broke:
return currFloor
else:
minFloor = currFloor
elif emulator.eggs == 2:
currFloor += emulator.drops
print(currFloor)
broke = emulator.drop(currFloor)
if broke:
maxFloor = currFloor
currFloor = minFloor
else:
minFloor = currFloor
else:
num_drops = emulator.drops
num_eggs = emulator.eggs
currFloor += next_jump(num_eggs, num_drops)
print(
"up by: "
+ str(next_jump(num_eggs, num_drops))
+ " eggs: "
+ str(num_eggs)
+ " drops: "
+ str(num_drops)
)
print(currFloor)
broke = emulator.drop(currFloor)
if broke:
maxFloor = currFloor
currFloor = minFloor
else:
minFloor = currFloor
return currFloor + 1
def find_sum_to_current(dropCount):
number = 0
for i in range(1, dropCount + 1):
number += i
return number
def next_jump(num_egg, num_drop):
if num_egg == 1 or num_drop == 1:
return 1
else:
sum = 0
for i in range(0, num_drop):
sum += next_jump(num_egg - 1, i)
if num_egg > 2:
sum += 1
return sum | FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER RETURN VAR |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def solve(emulator):
num_drops = emulator.drops
num_eggs = emulator.eggs
HEIGHTS = heights(num_eggs, num_drops)
return solve_part(emulator, 0, HEIGHTS[num_eggs][num_drops], HEIGHTS) + 1
def solve_part(emulator, min_floor, max_floor, HEIGHTS):
if min_floor == max_floor:
return min_floor
else:
num_drops = emulator.drops
num_eggs = emulator.eggs
partition = HEIGHTS[num_eggs - 1][num_drops - 1]
partition_point = min_floor + partition + 1
if emulator.drop(partition_point):
return solve_part(emulator, min_floor, partition_point - 1, HEIGHTS)
else:
return solve_part(emulator, partition_point, max_floor, HEIGHTS)
def heights(n, m):
heights = [[(0) for i in range(m + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
heights[i][j] = heights[i - 1][j - 1] + heights[i][j - 1] + 1
return heights | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def height(n, m):
if n >= m:
return 2 ** min(n, m) - 1
f = 1
res = 0
for i in range(n):
f = f * (m - i) // (i + 1)
res += f
return res
def solve(emulator):
m = emulator.drops
n = emulator.eggs
h = 0
tryh = 0
while n and m:
tryh = height(n - 1, m - 1) + 1
if emulator.drop(h + tryh):
n -= 1
else:
h += tryh
m -= 1
return h + 1 | FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
In this kata you must determine the lowest floor in a building from which you cannot drop an egg without it breaking. You may assume that all eggs are the same; if one egg breaks when dropped from floor `n`, all eggs will. If an egg survives a drop from some floor, it will survive a drop from any floor below too.
You are given `num_eggs` eggs and are allowed to make a maximum of `num_drops` drops. If an egg does not break when dropped, it may be reused. You may assume that it is feasible to determine the floor with the given number of eggs and drops.
Example 1:
You are given 1 egg and 10 drops. You start by dropping the egg from the first floor (floor numbering starts at 1). If it breaks, the answer is 1. If it does not break, you drop it from the second floor. If it breaks, the answer is 2. Otherwise you continue in the same manner until you reach a floor where the egg breaks. You may assume that you will reach that floor before you have exhausted your drops (i.e., in this example, the egg will break when dropped from floor 10 or lower).
Example 2: You are given 2 eggs and 10 drops. The highest floor for which this problem has a solution is floor 55, so you may assume that the answer is floor 55 or a lower floor. You will have to determine how the number 55 is derived and generalize it for any number of eggs and drops.
To solve this problem, you need to write a function `solve` that takes an argument `emulator` and returns the lowest floor from which an egg will break if dropped. The `emulator` object has the following properties and method which you may invoke:
1. `emulator.eggs` returns the number of eggs you have left. You may use this property to detemine the number of eggs you are given initially.
2. `emulator.drops` returns the the number of drops you have left. You may use this property to detemine the number of drops you are given initially.
3. `emulator.drop(n)` returns `True` if an egg dropped from floor `n` breaks and `False` if it does not break. The number of times that you may invoke this method is limited to the number of drops you were given. If invoked after no drops are left, it will throw an exception. Same happens if you invoke this method after all your eggs are broken.
You are not allowed to access any other method or state attribute of the `emulator` object, even if you can. This is considered cheating and is a very dishonorable thing to do! Note that `emulator.__dict__` has been disabled.
Note: [This kata](http://www.codewars.com/kata/faberge-easter-eggs-crush-test) is similar although not the same. | def solve(emulator):
def max_height(eggs, drops):
if eggs == 0 or drops == 0:
return 0
return max_height(eggs - 1, drops - 1) + 1 + max_height(eggs, drops - 1)
low = 1
high = max_height(emulator.eggs, emulator.drops)
while low <= high:
n = min(low + max_height(emulator.eggs - 1, emulator.drops - 1), high)
if emulator.drop(n):
high = n - 1
else:
low = n + 1
return low | FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | st = list(input())
counts = []
for i in range(0, 10):
counts.append(0)
for i in range(1, len(st)):
counts[(int(st[i]) - int(st[i - 1]) + 10) % 10] += 1
for x in range(0, 10):
st = ""
for y in range(0, 10):
s = 0
f = False
for i in range(0, 10):
if counts[i] == 0:
continue
j = 0
c = 100000000000
ff = False
f = False
while j < 10:
k = 0
while k < 10:
if j == 0 and k == 0:
k += 1
continue
if (x * j + y * k) % 10 == i:
c = min(c, j + k)
ff = True
k += 1
j += 1
if ff:
s += counts[i] * (max(1, c) - 1)
f = True
if not f:
s = -1
break
st += str(s)
if y != 9:
st += " "
print(st) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | inf = 1000000000
string = input()
s = []
for i in range(string.__len__()):
s.append(int(string[i]))
cf = [([0] * 10) for i in range(10)]
for i in range(1, s.__len__()):
cf[s[i - 1]][s[i]] += 1
for x in range(10):
for y in range(10):
ds = [([inf + 7] * 10) for i in range(10)]
for v in range(10):
for cx in range(10):
for cy in range(10):
if cx + cy == 0:
continue
to = (v + cx * x + cy * y) % 10
ds[v][to] = min(ds[v][to], cx + cy)
res = 0
for v in range(10):
for to in range(10):
if ds[v][to] > inf and cf[v][to] > 0:
res = -1
break
res += (ds[v][to] - 1) * cf[v][to]
if res == -1:
break
print(res, end=" ")
print() | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | import sys
def main():
import sys
input = sys.stdin.readline
S = input().rstrip("\n")
A = [int(s) for s in S]
N = len(A)
cnt = [([0] * 10) for _ in range(10)]
for i in range(N - 1):
cnt[A[i]][A[i + 1]] += 1
ans = [([0] * 10) for _ in range(10)]
for i in range(10):
for j in range(10):
if i <= j:
for k in range(10):
for l in range(10):
if cnt[k][l]:
d = (l - k) % 10
c = 100
for m in range(11):
for n in range(11):
if m == n == 0:
continue
if (i * m + j * n) % 10 == d:
c = min(c, m + n)
if c == 100:
ans[i][j] = -1
elif ans[i][j] >= 0:
ans[i][j] += (c - 1) * cnt[k][l]
else:
ans[i][j] = ans[j][i]
for i in range(10):
print(*ans[i])
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | bigger = 1000000000000000000000000000
s = input()
cf = [[(0) for _ in range(10)] for __ in range(10)]
ans = []
for i in range(1, len(s)):
cf[int(s[i - 1])][int(s[i])] += 1
for x in range(10):
line = []
for y in range(10):
ds = [[(bigger + 7) for _ in range(10)] for __ in range(10)]
for v in range(10):
for cx in range(10):
for cy in range(10):
if cx + cy == 0:
continue
to = (v + cx * x + cy * y) % 10
ds[v][to] = min(ds[v][to], cx + cy)
res = 0
for v in range(10):
for to in range(10):
if ds[v][to] > bigger and cf[v][to] > 0:
res = -1
break
res += (ds[v][to] - 1) * cf[v][to]
if res == -1:
break
line.append(res)
ans.append(line)
for line in ans:
print(*line) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | a = []
for i in range(100):
b = [-1] * 10
l = [0]
k = 0
while l:
m = []
k += 1
for x in l:
for j in (i // 10, i % 10):
y = (x + j) % 10
if b[y] < 0:
b[y] = k
m += (y,)
l = m
a += (b,)
r = [0] * 100
s = (*map(int, input()),)
for x, y in zip(s, s[1:]):
for i in range(100):
if r[i] >= 0:
d = a[i][(y - x) % 10]
if d < 0:
r[i] = -1
else:
r[i] += d - 1
print(*r) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FOR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | import sys
d = [int(i) for i in input()]
data = [((d[i + 1] - d[i]) % 10) for i in range(len(d) - 1)]
cnt = [0] * 10
for d in data:
cnt[d] += 1
for i in range(10):
for j in range(10):
row = [32] * 10
for a in range(10):
for b in range(10):
val = (a * i + b * j) % 10
s = a + b
if s > 0 and s < row[val]:
row[val] = s
for k in range(10):
if row[k] == 32:
row[k] = -1
else:
row[k] -= 1
ans = 0
for c in range(10):
inc = row[c]
if inc == -1 and cnt[c] > 0:
ans = -1
break
ans += inc * cnt[c]
sys.stdout.write(str(ans) + " ")
sys.stdout.write("\n") | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | from itertools import product
def helper(a):
l = [0] * 10
for t in range(1, 11):
x = a * t % 10
if l[x]:
break
l[x] = t
return [*((x, t) for x, t in enumerate(l) if t), (0, 0)]
def main():
a, cnt = 48, [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for b in map(ord, input()):
cnt[(b - a) % 10] += 1
a = b
inf, res, hbb = 10**9, {}, []
for a in range(10):
ha = helper(a)
hbb.append(ha)
for b, hb in enumerate(hbb):
dist = [inf] * 10
for (x, t), (y, u) in product(ha, hb):
x = (x + y) % 10
t += u
if 0 < t <= dist[x]:
dist[x] = t - 1
r = sum(c * d for c, d in zip(cnt, dist))
res[a, b] = res[b, a] = r if r < inf else -1
for a in range(10):
print(*[res[a, b] for b in range(10)])
main() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER DICT LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | import sys
s = sys.stdin.readline().strip()
if s[0] != "0":
s = "0" + s
ans = [[[(10**9) for i in range(0, 10)] for j in range(0, 10)] for k in range(0, 10)]
for n in range(0, 20):
for i in range(0, 10):
for j in range(0, 10):
for k in range(0, 10):
if k == i or k == j:
ans[i][j][k] = 1
else:
ans[i][j][k] = min(
[
ans[i][j][k],
1 + ans[i][j][(k - i + 10) % 10],
1 + ans[i][j][(k - j + 10) % 10],
]
)
x = [0] * 10
for i in range(0, len(s) - 1):
x[(int(s[i + 1]) - int(s[i]) + 10) % 10] = (
x[(int(s[i + 1]) - int(s[i]) + 10) % 10] + 1
)
for i in range(0, 10):
ans2 = [0] * 10
for j in range(0, 10):
for k in range(0, 10):
ans2[j] = ans2[j] + (ans[i][j][k] - 1) * x[k]
if ans2[j] >= 10**8:
ans2[j] = -1
print(" ".join(list(map(str, ans2)))) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | def floyd_warshall(cost):
n = len(cost)
dist = cost[:]
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
return dist
def insert_matrix(x, y):
cost = [[(0) for i in range(10)] for j in range(10)]
for i in range(10):
for j in range(10):
if (i + x) % 10 == j or (i + y) % 10 == j:
cost[i][j] = 1
else:
cost[i][j] = 100
dist = floyd_warshall(cost)
for i in range(10):
for j in range(10):
if dist[i][j] >= 100:
dist[i][j] = -1
else:
dist[i][j] -= 1
return dist
def solve_one(x, y, transitions):
mat = insert_matrix(x, y)
result = 0
for i in range(10):
for j in range(10):
if mat[i][j] * transitions[i][j] < 0:
return -1
result += mat[i][j] * transitions[i][j]
return result
def solve(S):
transitions = [[(0) for i in range(10)] for j in range(10)]
for i in range(1, len(S)):
transitions[int(S[i - 1])][int(S[i])] += 1
result = [[(0) for i in range(10)] for j in range(10)]
for x in range(10):
for y in range(10):
result[x][y] = solve_one(x, y, transitions)
return result
result = solve(input())
for x in range(10):
for y in range(10):
print(result[x][y], end=" ")
print() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | def findMoves(x, y, p, q):
res = 21
for i in range(11):
for j in range(11):
if i > 0 or j > 0:
if (x + i * p + j * q) % 10 == y:
res = min(i + j - 1, res)
if res == 21:
return -1
return res
def calc(p, q):
ans = 0
for i in range(10):
for j in range(10):
if count[i][j] > 0:
moves = findMoves(i, j, p, q)
if moves == -1:
return -1
else:
ans += moves * count[i][j]
return ans
s = str(input())
n = len(s)
count = [([0] * 10) for i in range(10)]
for i in range(n - 1):
j = int(s[i])
k = int(s[i + 1])
count[j][k] += 1
answer = [([0] * 10) for i in range(10)]
for i in range(10):
for j in range(10):
answer[i][j] = calc(i, j)
print(" ".join(str(x) for x in answer[i])) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = input()
inf = 1061109567
ans = []
for i in range(10):
ans.append([-1] * 10)
for i in range(10):
for j in range(i, 10):
dist = [0] * 10
for k in range(10):
dist[k] = [inf] * 10
for k in range(10):
dist[k][(k + i) % 10] = 1
dist[k][(k + j) % 10] = 1
for k in range(10):
for ii in range(10):
for jj in range(10):
dist[ii][jj] = min(dist[ii][jj], dist[ii][k] + dist[k][jj])
p = 0
cnt = 0
for k in range(1, len(s)):
if dist[p][ord(s[k]) - 48] == inf:
ans[i][j] = ans[j][i] = -1
break
cnt += dist[p][ord(s[k]) - 48] - 1
p = ord(s[k]) - 48
else:
ans[i][j] = ans[j][i] = cnt
for arr in ans:
for a in arr:
print(a, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | import sys
input = lambda: sys.stdin.readline().strip()
s = input()
num = {}
for i in range(10):
num[i] = 0
for i in range(len(s) - 1):
num[(int(s[i + 1]) - int(s[i])) % 10] += 1
for x in range(10):
for y in range(10):
d = {}
for i in range(10):
d[i] = sys.maxsize
for i in range(10):
for j in range(10):
if i + j == 0:
continue
cur = (i * x + j * y) % 10
d[cur] = min(d[cur], i + j)
ans = 0
for i in range(10):
if d[i] == sys.maxsize and num[i] > 0:
break
ans += num[i] * (d[i] - 1)
else:
print(ans, end=" ")
continue
print(-1, end=" ")
print() | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | from sys import setrecursionlimit as SRL
from sys import stdin
SRL(10**7)
rd = stdin.readline
rrd = lambda: list(map(int, rd().strip().split()))
s = str(rd().strip())
ans = [([0] * 11) for _j in range(11)]
ddp = [[([100000] * 11) for _k in range(11)] for kk in range(11)]
tt = [0] * 11
for i in range(10):
for j in range(10):
for u in range(11):
for v in range(11):
if u == 0 and v == 0:
continue
k = (i * u + j * v) % 10
ddp[i][j][k] = min(ddp[i][j][k], max(0, u + v - 1))
pre = 0
for i in range(1, len(s)):
t = (int(s[i]) + 10 - pre) % 10
tt[t] += 1
pre = int(s[i])
for i in range(10):
asi = ""
for j in range(10):
for k in range(11):
if tt[k] and ddp[i][j][k] >= 80000:
ans[i][j] = -1
break
ans[i][j] += tt[k] * ddp[i][j][k]
asi = asi + str(ans[i][j]) + " "
print(asi) | EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | S = input()
T = [([0] * 10) for i in range(10)]
for i in range(1, len(S)):
T[int(S[i - 1])][int(S[i])] += 1
C = [
[[[(0) for i in range(10)] for j in range(10)] for k in range(10)]
for l in range(10)
]
for i in range(10):
for j in range(10):
for k in range(10):
for l in range(10):
min_val = 1000
for a1 in range(11):
for a2 in range(11):
if a1 != 0 or a2 != 0:
if j == (a1 * k + a2 * l + i) % 10:
min_val = min(min_val, a1 + a2)
if min_val == 1000:
min_val = -(10**10)
C[i][j][k][l] = min_val - 1
ans = [([0] * 10) for i in range(10)]
for k in range(10):
for l in range(10):
a = 0
for i in range(10):
for j in range(10):
a += C[i][j][k][l] * T[i][j]
if a < 0:
a = -1
ans[k][l] = a
for a in ans:
print(*a) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = input()
df = [0] * 10
ss = zip(s, s[1:])
for f, s in ss:
df[(ord(s) - ord(f)) % 10] += 1
result = [([0] * 10) for _ in range(10)]
for i in range(10):
for j in range(10):
for k in range(10):
if df[k] == 0:
continue
v = -1
for u in range(1, 11):
for x in range(0, u + 1):
if (i * x + j * (u - x)) % 10 == k:
v = u
break
if v != -1:
break
if v == -1:
result[i][j] = -1
break
result[i][j] += (v - 1) * df[k]
for i in range(10):
print(" ".join(map(str, result[i]))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | from itertools import product
def main():
a, cnt, res, hbb = 48, [-1] + [0] * 18, {}, []
for b in map(ord, input()):
cnt[b - a] += 1
a = b
for i in range(9, 0, -1):
cnt[i] += cnt.pop()
cnt = [z for z in enumerate(cnt) if z[1]]
for a in range(10):
dist = [0] * 10
for t in range(1, 11):
x = a * t % 10
if dist[x]:
break
dist[x] = t
ha = [*(z for z in enumerate(dist) if z[1]), (0, 0)]
hbb.append(ha)
for b, hb in enumerate(hbb):
dist = [99999999] * 10
for (x, t), (y, u) in product(ha, hb):
x = (x + y) % 10
t += u
if 0 < t <= dist[x]:
dist[x] = t - 1
t = sum(c * dist[x] for x, c in cnt)
res[a, b] = res[b, a] = t if t < 99999999 else -1
for a in range(10):
print(*[res[a, b] for b in range(10)])
main() | FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP LIST NUMBER NUMBER DICT LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = input()
pairs = {(a + b): (0) for a in "0123456789" for b in "0123456789"}
for a, b in zip(s, s[1:]):
pairs[a + b] += 1
def solve(x, y, i, j):
ans = 20
for a in range(10):
for b in range(10):
if (i + a * x + b * y + x) % 10 == j:
ans = min(ans, a + b)
if (i + a * x + b * y + y) % 10 == j:
ans = min(ans, a + b)
if ans == 20:
return -1
return ans
for x in range(10):
row = []
for y in range(10):
ans = 0
for i in range(10):
for j in range(10):
s = f"{i}{j}"
if pairs[s] > 0:
tmp = solve(x, y, i, j)
ans += tmp * pairs[s]
if tmp == -1:
ans = -1
break
else:
continue
break
row.append(ans)
print(*row) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | from itertools import accumulate
from sys import stdin, stdout
s = input()
def computeGCD(x, y):
while y:
x, y = y, x % y
return x
record = {}
for x in range(10):
for y in range(10):
for d in range(10):
m = 9999
if (x == 0 or y == 0) and d == 0:
record[x, y, d] = 0
elif x == 0 and y == 0 and d != 0:
record[x, y, d] = -1
else:
for k in range(10):
d_tmp = d + 10 * k
if x == 0:
if d_tmp % y == 0:
record[x, y, d] = d_tmp // y - 1
break
elif y == 0:
if d_tmp % x == 0:
record[x, y, d] = d_tmp // x - 1
break
else:
for a in range(d_tmp // x + 1):
for b in range(d_tmp // y + 1):
if x * a + y * b == d_tmp and d_tmp != 0:
m = min(m, a + b - 1)
if (x, y, d) not in record:
if m == 9999:
m = -1
record[x, y, d] = m
data = [([0] * 10) for _ in range(10)]
len_s = len(s)
s = list(s)
freq = [0] * 10
for i in range(len_s - 1):
A = s[i]
B = s[i + 1]
d = ord(B) - ord(A)
if d < 0:
d += 10
freq[d] += 1
for x in range(10):
for y in range(10):
for d in range(10):
if freq[d] == 0:
data[x][y] += 0
elif record[x, y, d] == -1:
data[x][y] = -10000000
else:
data[x][y] += freq[d] * record[x, y, d]
for x in range(10):
for y in range(10):
if data[x][y] < 0:
print(-1, end=" ")
else:
print(data[x][y], end=" ")
print("", end="\n") | ASSIGN VAR FUNC_CALL VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | def f(x, y, d):
while d <= 0:
d += 10
ans = 10000
for i in range(10):
for j in range(10):
if (i * x + j * y) % 10 == d % 10 and i + j > 0:
ans = min(ans, i + j)
if ans == 10000:
return -1
return max(ans - 1, 0)
arr = [0] * 10
for i in range(10):
arr[i] = [0] * 10
for i in range(10):
for j in range(10):
arr[i][j] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
arr[x][y][d] = f(x, y, d)
s = input()
b = []
for i in range(len(s) - 1):
b.append(int(s[i + 1]) - int(s[i]))
bb = [0] * 10
for i in b:
bb[i] += 1
ans = [0] * 10
for i in range(10):
ans[i] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
t = arr[x][y][d]
if t == -1 and bb[d] != 0:
ans[x][y] = -1
break
ans[x][y] += t * bb[d]
for x in range(10):
for y in range(10):
print(ans[x][y], end=" ")
print()
print() | FUNC_DEF WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = input()
def floyd_warshall(n, A):
dist = [[float("inf") for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(n):
if A[i][j]:
dist[i][j] = A[i][j]
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
return dist
cp = {}
for ii in range(len(s) - 1):
p = int(s[ii])
q = int(s[ii + 1])
try:
cp[p, q] += 1
except:
cp[p, q] = 1
for i in range(10):
res = []
for j in range(10):
adj = [[] for i in range(10)]
adj = [([0] * 10) for i in range(10)]
for ii in range(10):
adj[ii][ii] = 0
adj[ii][(ii + i) % 10] = 1
adj[ii][(ii + j) % 10] = 1
dist = floyd_warshall(10, adj)
tot = 0
tot = 0
st = True
for ii in cp:
ii = tuple(ii)
p = ii[0]
q = ii[1]
x = dist[p][q]
if x == float("inf"):
st = False
break
tot += (x - 1) * cp[ii]
if not st:
res.append(-1)
else:
res.append(tot)
print(*res) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = list(map(int, input()))
ans = [([0] * 10) for i in range(10)]
for i in range(10):
for j in range(i + 1):
arr = [1000] * 10
for i1 in range(10):
for j1 in range(i1 == 0, 10):
st = (i * i1 + j * j1) % 10
arr[st] = min(arr[st], i1 + j1 - 1)
for e in range(1, len(s)):
cur = (s[e] - s[e - 1]) % 10
if arr[cur] != 1000:
ans[i][j] += arr[cur]
else:
ans[i][j] = -1
break
ans[j][i] = ans[i][j]
for i in ans:
print(*i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR 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 VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | s = input()
mp = [0] * 10
for i in range(1, len(s)):
x = (int(s[i]) - int(s[i - 1]) + 10) % 10
mp[x] += 1
def tab(a, b):
tb = [1000] * 10
for i in range(10):
for j in range(10):
if i == 0 and j == 0:
continue
x = (a * i + b * j) % 10
tb[x] = min(tb[x], i + j)
return tb
for x in range(10):
for y in range(10):
tb = tab(x, y)
ans = 0
for t in range(10):
if mp[t] > 0 and tb[t] == 1000:
print(-1, end=" ")
break
ans += mp[t] * (tb[t] - 1)
else:
print(ans, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$.
This is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.
You wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ — the remaining data of the sequence.
For all $0 \le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.
-----Input-----
The first line contains a single string $s$ ($1 \le |s| \le 2 \cdot 10^6$, $s_i \in \{\text{0} - \text{9}\}$) — the remaining data you have. It's guaranteed that $s_1 = 0$.
-----Output-----
Print a $10 \times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.
-----Example-----
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
-----Note-----
Let's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).
One of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.
The $6$-$8$-counter could print exactly the string $0840$. | n = input()
cache = {}
def solve(a, b, c):
if a == b == 0:
if c == 0:
return 1
else:
return None
if a > b:
a, b = b, a
if (a, b, c) in cache:
return cache[a, b, c]
for i in range(b + 1):
j = -1
while True:
j += 1
x = c + i * 10 - a * j
if x < 0:
break
if x % b == 0:
y = x // b
if y == 0 and a == 0:
y = 1
v = cache.get((a, b, c))
if j + y > 0 and (v is None or j + y < v):
cache[a, b, c] = j + y
if a == 0:
break
cache.setdefault((a, b, c), None)
return cache.get((a, b, c))
x = 0
table = {}
for y in n[1:]:
y = int(y)
z = (10 + y - x) % 10
x = y
ti = table.get(z, 0)
ti += 1
table[z] = ti
for i in range(10):
for j in range(10):
s = 0
for z, ti in table.items():
k = solve(i, j, z)
if k is None:
print(f"{-1:>10}", end="")
break
else:
s += k * ti
else:
print(f"{s - len(n) + 1:>10}", end="")
print() | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NONE IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NONE BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER STRING STRING VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR |
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.
Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).
The seller can obtain array b from array a if the following conditions hold: b_{i} > 0; 0 ≤ a_{i} - b_{i} ≤ k for all 1 ≤ i ≤ n.
Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
-----Input-----
The first line contains two integers n and k (1 ≤ n ≤ 3·10^5; 1 ≤ k ≤ 10^6). The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — array a.
-----Output-----
In the single line print a single number — the maximum possible beauty of the resulting array.
-----Examples-----
Input
6 1
3 6 10 12 13 16
Output
3
Input
5 3
8 21 52 15 77
Output
7
-----Note-----
In the first sample we can obtain the array:
3 6 9 12 12 15
In the second sample we can obtain the next array:
7 21 49 14 77 | n, k = list(map(int, input().split()))
t = set(map(int, input().split()))
y = x = min(t)
t = list(t)
while True:
for i in t:
if i % x > k:
x = i // (i // x + 1)
if y == x:
break
y = x
print(y) | 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.
Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).
The seller can obtain array b from array a if the following conditions hold: b_{i} > 0; 0 ≤ a_{i} - b_{i} ≤ k for all 1 ≤ i ≤ n.
Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
-----Input-----
The first line contains two integers n and k (1 ≤ n ≤ 3·10^5; 1 ≤ k ≤ 10^6). The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — array a.
-----Output-----
In the single line print a single number — the maximum possible beauty of the resulting array.
-----Examples-----
Input
6 1
3 6 10 12 13 16
Output
3
Input
5 3
8 21 52 15 77
Output
7
-----Note-----
In the first sample we can obtain the array:
3 6 9 12 12 15
In the second sample we can obtain the next array:
7 21 49 14 77 | import sys
line = sys.stdin.readline()
n, k = [int(i) for i in line.split()]
line = sys.stdin.readline()
_min = int(10000000.0)
_max = 0
accum = [0] * int(1000000.0 + 2)
for i in line.split():
j = int(i)
accum[j] += 1
if j > _max:
_max = j
if j < _min:
_min = j
for i in range(1, _max + 1):
accum[i] += accum[i - 1]
if k >= _min - 1:
print(_min)
return
gcd = 2
for i in range(_min, 1, -1):
div = True
j = 2 * i
comp = i - k
while div:
if j - 1 <= _max:
div = accum[j - 1] == accum[j - comp]
elif j - comp <= _max:
div = accum[_max] == accum[j - comp]
else:
break
j += i
if div:
gcd = i
break
print(gcd) | IMPORT 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def isSafe(c, i, k):
if s[c : c + k] == i:
return True
return False
l = []
def wb(c, ans):
if c == len(s):
l.append(" ".join(ans))
return
for i in dict:
k = len(i)
if isSafe(c, i, k):
ans.append(i)
t = c + k
if wb(t, ans):
return True
t = c - k
ans.pop()
return False
wb(0, [])
return l | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, wordDict, s):
return self.helper(s, wordDict, {})
def helper(self, s, wordDict, memo):
if s in memo:
return memo[s]
if not s:
return []
res = []
for word in wordDict:
if not s.startswith(word):
continue
if len(word) == len(s):
res.append(word)
else:
resultOfTheRest = self.helper(s[len(word) :], wordDict, memo)
for item in resultOfTheRest:
item = word + " " + item
res.append(item)
memo[s] = res
return res | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR RETURN LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def solve(self, s, st, dict, ans):
if len(s) == 0:
ans.append(st[: len(st) - 1])
return
n = len(s)
for i in range(n):
left = s[: i + 1]
if left in dict:
right = s[i + 1 :]
self.solve(right, st + left + " ", dict, ans)
def wordBreak(self, n, dict, s):
ans = []
self.solve(s, "", dict, ans)
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
ans_list = []
def wordBreak(self, n, words, s):
self.ans_list = []
self.solve(words, s, "")
return self.ans_list
def solve(self, words, s, result_string):
for word in words:
result_string_copy = result_string
if s == word:
result_string_copy = result_string_copy + " " + word
self.ans_list.append(result_string_copy.strip())
if s.startswith(word):
result_string_copy = result_string_copy + " " + word
self.solve(words, s[len(word) :], result_string_copy) | CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR STRING RETURN VAR FUNC_DEF FOR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def _wordBreak(s, wordDict, start, cur, res):
if start == len(s) and cur:
res.append(" ".join(cur))
return res
for i in range(start, len(s)):
word = s[start : i + 1]
if word in wordDict:
cur.append(word)
_wordBreak(s, wordDict, i + 1, cur, res)
cur.pop()
res = []
_wordBreak(s, dict, 0, [], res)
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.