description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
f = [] f.append(1) f.append(1) mod = 1000000000.0 + 7 for i in range(100010): f.append((f[-1] + f[-2]) % mod) n, m = map(int, input().split()) print(int(2 * (f[n] + f[m] - 1) % mod))
ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
a, b = map(int, input().split()) if a == 1 and b == 1: print(2) else: d = max(a, b) * [0] d[0] = 1 d[1] = 2 for i in range(2, len(d)): d[i] = d[i - 1] + d[i - 2] print(2 * (d[a - 1] + d[b - 1] - 1) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
ai = [0] * 100100 bi = [0] * 100100 n, m = input().split() n = int(n) m = int(m) ai[1] = 2 ai[2] = 4 len = max(n, m) for i in range(3, 100100): ai[i] = (ai[i - 1] + ai[i - 2]) % 1000000007 print((ai[n] - 2 + ai[m]) % 1000000007)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = list(map(int, input().split())) max_element = max(n, m) min_element = min(n, m) dp = [(2) for i in range(max_element + 1)] for i in range(2, max_element + 1): dp[i] = dp[i - 1] + dp[i - 2] print((dp[max_element] + dp[min_element] - 2) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
dp = [] dp.append(2) dp.append(4) dp.append(6) for i in range(3, 100001): dp.append((dp[i - 1] + dp[i - 2]) % 1000000007) n, m = map(int, input().split()) ans = dp[m - 1] ans += dp[n - 1] - 2 print(ans % 1000000007)
ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) q = 10**9 + 7 a1 = [0] * n a2 = [0] * n b1 = [0] * n b2 = [0] * n a1[0] = 1 b1[0] = 1 for i in range(1, n): a1[i] = b1[i - 1] + b2[i - 1] a2[i] = a1[i - 1] b1[i] = a1[i - 1] + a2[i - 1] b2[i] = b1[i - 1] a1[i] %= q a2[i] %= q b1[i] %= q b2[i] %= q ans = (a1[n - 1] + a2[n - 1] + b1[n - 1] + b2[n - 1]) % q a1 = [0] * m a2 = [0] * m b1 = [0] * m b2 = [0] * m a1[0] = 1 b1[0] = 1 for i in range(1, m): a1[i] = b1[i - 1] + b2[i - 1] a2[i] = a1[i - 1] b1[i] = a1[i - 1] + a2[i - 1] b2[i] = b1[i - 1] a1[i] %= q a2[i] %= q b1[i] %= q b2[i] %= q ans += a1[m - 1] + a2[m - 1] + b1[m - 1] + b2[m - 1] ans %= q print((q + ans - 2) % q)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) mod = 10**9 + 7 fib = [1, 1] for i in range(200001): fib.append((fib[-1] + fib[-2]) % mod) print((fib[n] * 2 - 2 + 2 * fib[m]) % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
import sys import time def modulo_div(a, b, modulo): if b == 1: return a % modulo return (a + modulo_div(-a, modulo % b, b) * modulo) // b % modulo for line in sys.stdin.readlines(): inputs = line.split() n = int(inputs[0]) m = int(inputs[1]) numer = 1 denom = 1 sums = 0 modulo = 10**9 + 7 fibonacci = [1, 1] for i in range(max(n, m) + 1): fibonacci.append((fibonacci[-1] + fibonacci[-2]) % modulo) sums += (fibonacci[n] + fibonacci[m] - 1) * 2 % modulo print(sums)
IMPORT IMPORT FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") sys.setrecursionlimit(10**9) INF = float("inf") MOD = 10**9 + 7 H, W = MAP() N = max(H, W) dp = [0] * (N + 2) dp[1] = 1 dp[2] = 2 for i in range(3, N + 1): dp[i] = dp[i - 1] + dp[i - 2] dp[i] %= MOD print(2 * (dp[H] + dp[W] - 1) % MOD)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) ans = [0, 1, 2, 3, 5] for i in range(5, max(m, n) + 1): ans.append((ans[i - 2] + 2 * ans[i - 3] + ans[i - 4]) % (10**9 + 7)) print(2 * (ans[n] + ans[m] - 1) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
MOD = 10**9 + 7 n, m = map(int, input().split()) fib = [1] * (max(n, m) + 2) for i in range(2, max(n, m) + 2): fib[i] = fib[i - 1] + fib[i - 2] fib[i] %= MOD print(((fib[m] + fib[n]) * 2 - 2) % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = [int(i) for i in input().split()] f = [1, 1] mod = 1000000007 for i in range(100005): f.append((f[-1] + f[-2]) % mod) ans2 = 2 * f[m] + 2 * f[n] - 2 print(ans2 % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) a = [0, 2] mod = pow(10, 9) + 7 s = 0 t = 2 for i in range(2, 100002): u = (a[i - 1] * 2 % mod - s % mod) % mod if u < 0: u += mod a.append(u) s = t t = (u % mod - s % mod) % mod if t < 0: t += mod print((a[n] % mod + a[m] % mod - 2) % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
r = [int(x) for x in input().split()] fibonacci = [(0) for i in range(100001)] fibonacci[0] = 1 fibonacci[1] = 1 for i in range(99999): fibonacci[i + 2] = (fibonacci[i + 1] + fibonacci[i]) % 1000000007 print( (2 * ((fibonacci[r[0]] + fibonacci[r[1]]) % 1000000007) - 2 + 1000000007) % 1000000007 )
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
def read_int(): return int(input()) def read_str(): return input().strip() def solve(n, m): mod = 10**9 + 7 k = max(n, m) + 1 count_1 = [0] * k count_2 = [0] * k count_1[1] = 1 count_2[1] = 0 for i in range(2, k): count_1[i] = (count_1[i - 1] + count_2[i - 1]) % mod count_2[i] = count_1[i - 1] return 2 * (count_1[n] + count_2[n] + count_1[m] + count_2[m] - 1 + mod) % mod n, m = map(int, read_str().split()) res = solve(n, m) print("%s" % res)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
width, height = list(map(int, input().strip().split(" ")[:2])) modulo = 10**9 + 7 fibs = [0, 0] sum_fibs = [0, 0] a, b = 1, 0 for i in range(max(width, height)): fibs.append(a) sum_fibs.append((sum_fibs[-1] + a) % modulo) a, b = (a + b) % modulo, a def ans(w, h): return 2 * (1 + sum_fibs[w] + sum_fibs[h]) % modulo print(ans(width, height))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) def fib(x): a = 0 b = 1 temp = 0 for i in range(0, x): temp = a + b a = b b = temp return temp print(2 * (fib(n) + fib(m) - 1) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) MOD = 1000000007 if n < m: n, m = m, n dp = list() dp.append(1) dp.append(1) for i in range(2, n + 1): dp.append((dp[i - 1] + dp[i - 2]) % MOD) print((dp[n] + dp[m] + MOD - 1) * 2 % MOD)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) mod = 10**9 + 7 if n > m: n, m = m, n if m == 1: print(2) else: DP = [([0] * 2) for _ in range(m)] DP[1][0] = 2 DP[1][1] = 2 for i in range(2, m): DP[i][0] = DP[i - 1][1] % mod DP[i][1] = (DP[i - 1][0] + DP[i - 1][1]) % mod a = DP[m - 1][0] + DP[m - 1][1] if n == 1: print(a % mod) else: ans = a - 2 ans += DP[n - 1][0] + DP[n - 1][1] print(ans % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
import sys input = sys.stdin.readline n, m = map(int, input().split()) if n == 1 and m == 1: print(2) exit() MOD = 10**9 + 7 N = max(n, m) dp = [([0] * 4) for _ in range(N + 1)] for j in range(4): dp[2][j] = 1 for i in range(2, N): dp[i + 1][0] = dp[i][2] dp[i + 1][1] = dp[i][0] + dp[i][2] dp[i + 1][2] = dp[i][1] + dp[i][3] dp[i + 1][3] = dp[i][1] for j in range(4): dp[i + 1][j] %= MOD if n == 1: print(sum(dp[m]) % MOD) elif m == 1: print(sum(dp[n]) % MOD) else: print((sum(dp[n]) + sum(dp[m]) - 2) % MOD)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) d = [2, 4] k = 10**9 + 7 for i in range(2, max(n, m)): d += [(d[i - 1] + d[i - 2]) % k] print((d[m - 1] + d[n - 1] - 2) % k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR LIST BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) a = [0] * 10**6 a[1], a[2] = 2, 4 for i in range(3, max(n, m) + 1): a[i] = a[i - 1] + a[i - 2] print((a[n] + a[m] - 2) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
foo = [] n, m = input().split() n = int(n) m = int(m) foo.append(1) foo.append(1) for i in range(max(n, m)): foo.append(foo[i + 1] + foo[i]) o = 2 * (foo[n] + foo[m] - 1) print(o % 1000000007)
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = list(map(int, input().split())) def fib(n): if n < 2: return 1 a = 1 b = 1 for i in range(2, n + 1): c = a + b a = b b = c return c print((2 * fib(n) + 2 * fib(m) - 2) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
N, M = map(int, input().split()) mod = 10**9 + 7 L = max(N, M) + 1 dp = [[1, 1] for _ in range(L + 1)] dp[1][0] = 1 dp[1][1] = 1 for l in range(2, L + 1): dp[l][0] = (dp[l - 1][1] + dp[l - 2][1]) % mod dp[l][1] = (dp[l - 1][0] + dp[l - 2][0]) % mod ans = dp[N][0] + dp[N][1] + dp[M][0] + dp[M][1] - 2 if ans < 0: ans += mod ans %= mod print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side. Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$. -----Input----- The only line contains two integers $n$ and $m$ ($1 \le n, m \le 100\,000$), the number of rows and the number of columns of the field. -----Output----- Print one integer, the number of random pictures modulo $10^9 + 7$. -----Example----- Input 2 3 Output 8 -----Note----- The picture below shows all possible random pictures of size $2$ by $3$. [Image]
n, m = map(int, input().split()) def create_dp(x): dp = [(0) for _ in range(x)] dp[0] = 1 if x == 1: return dp dp[1] = 2 if x == 2: return dp for i in range(2, x): dp[i] = dp[i - 1] + dp[i - 2] dp[i] %= MOD return dp MOD = 10**9 + 7 cut_pattern = 0 dp = create_dp(max(n, m)) cut_pattern += dp[n - 1] * 2 cut_pattern += dp[m - 1] * 2 cut_pattern -= 2 cut_pattern %= MOD print(cut_pattern)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Ikshu's love for binary numbers Ikshu recently learnt to generate random numbers. He is generating stream binary numbers. Uptil now he has generated N bits of the binary number. Now, he wants to know if there is a streak of contiguous 1's of length K. Help him to find the probability of existence of such a streak in his binary number. Assume that probability of generating a one and zero is equal i.e 0.5. Input: First and only line of input contains two space separated integers N and K as described above. Output: output contains one line containing probablity. output should be in n/m form where it is n/m is in its lowest fraction. Constraints: 1 ≤ N ≤ 60 1 ≤ K ≤ N SAMPLE INPUT 5 1 SAMPLE OUTPUT 31/32 Explanation Ikshu generated the 5 BIT binary number. Now, Out of 32 distinct binary numbers possible there are 31 cases having atleast one set bit. So, ans is 31/32
import sys for line in sys.stdin: if len(line) > 1: n, k = list(map(int, line.split())) s = [0] * (n + 1) s[k] = 1 for i in range(k + 1, n + 1): s[i] = 2 * s[i - 1] + pow(2, i - k - 1) - s[i - 1 - k] a = s[n] b = pow(2, n) while a % 2 == 0: a = a / 2 b = b / 2 print("%d%c%d" % (a, "/", b))
IMPORT FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING VAR
Ikshu's love for binary numbers Ikshu recently learnt to generate random numbers. He is generating stream binary numbers. Uptil now he has generated N bits of the binary number. Now, he wants to know if there is a streak of contiguous 1's of length K. Help him to find the probability of existence of such a streak in his binary number. Assume that probability of generating a one and zero is equal i.e 0.5. Input: First and only line of input contains two space separated integers N and K as described above. Output: output contains one line containing probablity. output should be in n/m form where it is n/m is in its lowest fraction. Constraints: 1 ≤ N ≤ 60 1 ≤ K ≤ N SAMPLE INPUT 5 1 SAMPLE OUTPUT 31/32 Explanation Ikshu generated the 5 BIT binary number. Now, Out of 32 distinct binary numbers possible there are 31 cases having atleast one set bit. So, ans is 31/32
n, k = list(map(int, input().strip().split())) array = [(0) for _ in range(k)] array.append(1) powers = [(2**i) for i in range(n + 1)] for i in range(k + 1, n + 1): array.append(powers[i - k - 1] - array[i - k - 1] + 2 * array[i - 1]) num = array[-1] denom = powers[-1] while num % 2 == 0: num = int(num / 2) denom = int(denom / 2) print("%d/%d" % (num, denom))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
Ikshu's love for binary numbers Ikshu recently learnt to generate random numbers. He is generating stream binary numbers. Uptil now he has generated N bits of the binary number. Now, he wants to know if there is a streak of contiguous 1's of length K. Help him to find the probability of existence of such a streak in his binary number. Assume that probability of generating a one and zero is equal i.e 0.5. Input: First and only line of input contains two space separated integers N and K as described above. Output: output contains one line containing probablity. output should be in n/m form where it is n/m is in its lowest fraction. Constraints: 1 ≤ N ≤ 60 1 ≤ K ≤ N SAMPLE INPUT 5 1 SAMPLE OUTPUT 31/32 Explanation Ikshu generated the 5 BIT binary number. Now, Out of 32 distinct binary numbers possible there are 31 cases having atleast one set bit. So, ans is 31/32
import sys def compute(mat, n, k): for i in range(1, n + 1): for j in range(k + 1): if j == 1: mat[i][j] = pow(2, i) - 1 continue if i - j - 1 >= 0: mat[i][j] = 2 * mat[i - 1][j] + (pow(2, i - j - 1) - mat[i - j - 1][j]) elif i - j == 0: mat[i][j] = 1 def main(): n, k = [int(tok) for tok in sys.stdin.readline().split()] mat = [[(0) for i in range(k + 1)] for i in range(n + 1)] compute(mat, n, k) num = mat[n][k] denom = pow(2, n) while num & 1 == 0: num >>= 1 denom >>= 1 print("%d/%d" % (num, denom)) if __name__ == "__main__": main()
IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR IF VAR STRING EXPR FUNC_CALL VAR
Ikshu's love for binary numbers Ikshu recently learnt to generate random numbers. He is generating stream binary numbers. Uptil now he has generated N bits of the binary number. Now, he wants to know if there is a streak of contiguous 1's of length K. Help him to find the probability of existence of such a streak in his binary number. Assume that probability of generating a one and zero is equal i.e 0.5. Input: First and only line of input contains two space separated integers N and K as described above. Output: output contains one line containing probablity. output should be in n/m form where it is n/m is in its lowest fraction. Constraints: 1 ≤ N ≤ 60 1 ≤ K ≤ N SAMPLE INPUT 5 1 SAMPLE OUTPUT 31/32 Explanation Ikshu generated the 5 BIT binary number. Now, Out of 32 distinct binary numbers possible there are 31 cases having atleast one set bit. So, ans is 31/32
def gcd(a, b): while b: a, b = b, a % b return a n, k = list(map(int, input().split())) q = 2**n a = [0] * (n + 1) a[0] = 1 for i in range(1, n + 1): a[i] = a[i - 1] * 2 if i == k: a[i] -= 1 elif i > k: a[i] -= a[i - k - 1] p = a[n] p = q - p d = gcd(p, q) p = p // d q = q // d print("{}/{}".format(p, q))
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) A = list(map(int, input().strip().split())) B = list(map(int, input().strip().split())) def tree_cutting(n, A, B): C = [(0) for _ in range(n)] hullx = [(0) for _ in range(n)] hully = [(0) for _ in range(n)] sz = 0 p = 0 C[0] = 0 hullx[0] = B[0] hully[0] = C[0] for i in range(1, n): p = min(sz, p) while ( sz > 0 and p < sz and (hully[p + 1] - hully[p]) / (hullx[p] - hullx[p + 1]) <= A[i] ): p += 1 C[i] = hully[p] + hullx[p] * A[i] sz += 1 hullx[sz] = B[i] hully[sz] = C[i] while sz > 1 and (hully[sz - 2] - hully[sz - 1]) / ( hullx[sz - 1] - hullx[sz - 2] ) >= (hully[sz - 2] - hully[sz]) / (hullx[sz] - hullx[sz - 2]): hullx[sz - 1] = hullx[sz] hully[sz - 1] = hully[sz] sz -= 1 return C[n - 1] print(tree_cutting(n, A, B))
ASSIGN VAR FUNC_CALL VAR 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 FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [0] * n stk = [0] for i in range(1, n): while len(stk) > 1 and c[stk[1]] - c[stk[0]] <= a[i] * (b[stk[0]] - b[stk[1]]): del stk[0] c[i] = c[stk[0]] + a[i] * b[stk[0]] while len(stk) > 1 and (c[stk[-1]] - c[stk[-2]]) * (b[stk[-1]] - b[i]) > ( b[stk[-2]] - b[stk[-1]] ) * (c[i] - c[stk[-1]]): del stk[-1] stk.append(i) print(c[n - 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [0] * n stackDp = [0] * n stackB = [0] * n stackB[0] = b[0] l = 0 r = 1 for i in range(1, n): while ( r - l > 1 and a[i] * stackB[l] + stackDp[l] >= a[i] * stackB[l + 1] + stackDp[l + 1] ): l += 1 dp[i] = a[i] * stackB[l] + stackDp[l] while r - l > 1 and (dp[i] - stackDp[r - 1]) * (stackB[r - 2] - stackB[r - 1]) <= ( stackDp[r - 1] - stackDp[r - 2] ) * (stackB[r - 1] - b[i]): r -= 1 stackB[r] = b[i] stackDp[r] = dp[i] r += 1 print(dp[n - 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
f = lambda: list(map(int, input().split())) g = lambda j: a[i] * b[j] + t[j] h = lambda j, k: (t[i] - t[j]) * (b[j] - b[k]) < (t[j] - t[k]) * (b[i] - b[j]) n = int(input()) a, b = f(), f() t = [0] * n p = [0] for i in range(1, n): while len(p) > 1 and g(p[1]) < g(p[0]): p.pop(0) t[i] = g(p[0]) while len(p) > 1 and h(p[-2], p[-1]): p.pop() p.append(i) print(t[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
read = lambda: map(int, input().split()) n = int(input()) a = list(read()) b = list(read()) dp = [0] * n st = [0] def f1(): i0, i1 = st[0], st[1] b1 = dp[i1] - dp[i0] k1 = b[i0] - b[i1] return b1 <= a[i] * k1 def f2(): i1, i2 = st[-1], st[-2] k1, k2 = b[i1] - b[i], b[i2] - b[i1] b1, b2 = dp[i] - dp[i1], dp[i1] - dp[i2] return b2 * k1 > b1 * k2 for i in range(1, n): while len(st) > 1 and f1(): st.pop(0) dp[i] = dp[st[0]] + a[i] * b[st[0]] while len(st) > 1 and f2(): st.pop() st.append(i) print(dp[n - 1])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
def intersection(p, q): return (q[1] - p[1]) / (p[0] - q[0]) def tree_cutting(n, A, B): I = [[0, 0] for _ in range(n)] C = [(0) for _ in range(n)] C[0] = 0 I[0][0] = -float("inf") I[0][1] = float("inf") C[1] = C[0] + A[1] * B[0] hull = [[B[0], C[0]], [B[1], C[1]]] I[0][1] = intersection(hull[-1], hull[-2]) I[1][0] = I[0][1] I[1][1] = float("inf") curr = 1 k = 0 for i in range(2, n): k = min(k, curr) - 1 while True: k += 1 if I[k][0] <= A[i] and A[i] <= I[k][1]: j = k break C[i] = hull[k][1] + A[i] * hull[k][0] p = [B[i], C[i]] while intersection(p, hull[-2]) <= intersection(hull[-1], hull[-2]): hull.pop() curr -= 1 if len(hull) < 2: break if B[i] != hull[-1][0]: hull.append(p) I[curr][1] = intersection(hull[-1], hull[-2]) curr += 1 I[curr][0] = intersection(hull[-1], hull[-2]) I[curr][1] = +float("inf") else: I[curr][1] = +float("inf") return C[n - 1] n = int(input()) A = list(map(int, input().strip().split())) B = list(map(int, input().strip().split())) print(tree_cutting(n, A, B))
FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
def cross(i, j, k, b, cost): return (cost[i] - cost[j]) * (b[i] - b[k]) - (b[i] - b[j]) * (cost[i] - cost[k]) def dot(i, j, a, b, cost): return cost[j] + a[i] * b[j] def CF319C(): N = int(input()) a = tuple(map(int, input().split())) b = tuple(map(int, input().split())) cost = [0] * N hull = [0] * N left = 0 right = 1 for i in range(1, N): while left + 1 < right and dot(i, hull[left], a, b, cost) >= dot( i, hull[left + 1], a, b, cost ): left += 1 cost[i] = dot(i, hull[left], a, b, cost) while right >= 2 and cross(hull[right], hull[right - 1], i, b, cost) >= 0: right -= 1 if left >= right: left = right - 1 right += 1 hull[right] = i return cost[-1] def __starting_point(): res = CF319C() print(res) __starting_point()
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR BIN_OP VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) c = [0] * n st = [0] * n first = 0 second = 0 for i in range(1, n): while ( second - first > 0 and a[i] * b[st[first]] + c[st[first]] >= a[i] * b[st[first + 1]] + c[st[first + 1]] ): first = first + 1 c[i] = a[i] * b[st[first]] + c[st[first]] while second - first > 0 and (b[st[second]] - b[i]) * ( c[st[second]] - c[st[second - 1]] ) > (c[i] - c[st[second]]) * (b[st[second - 1]] - b[st[second]]): second = second - 1 second = second + 1 st[second] = i print(c[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
aa, bb, dp = [], [], [] def dot(i, a): return dp[i] + a * bb[i] def main(): n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) for k in range(n): dp.append(0) aa.append(a[k]) bb.append(b[k]) st, i, j = [(0) for _ in range(n)], 0, 0 for k in range(1, n): while j - i > 0 and dot(st[i], a[k]) >= dot(st[i + 1], a[k]): i += 1 dp[k] = a[k] * b[st[i]] + dp[st[i]] while j - i > 0 and (b[st[j]] - b[k]) * (dp[st[j]] - dp[st[j - 1]]) > ( dp[k] - dp[st[j]] ) * (b[st[j - 1]] - b[st[j]]): j -= 1 j += 1 st[j] = k print(dp[-1]) main()
ASSIGN VAR VAR VAR LIST LIST LIST FUNC_DEF RETURN BIN_OP VAR VAR BIN_OP VAR VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
def dot(a, b, c): return a + c * b def main(): n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) dp = [(0) for _ in range(n)] st = [(0) for _ in range(n)] i, j = 0, 0 for k in range(1, n): while j - i > 0 and dot(dp[st[i]], b[st[i]], a[k]) >= dot( dp[st[i + 1]], b[st[i + 1]], a[k] ): i += 1 dp[k] = a[k] * b[st[i]] + dp[st[i]] while j - i > 0 and (b[st[j]] - b[k]) * (dp[st[j]] - dp[st[j - 1]]) > ( dp[k] - dp[st[j]] ) * (b[st[j - 1]] - b[st[j]]): j -= 1 j += 1 st[j] = k print(dp[-1]) def __starting_point(): main() __starting_point()
FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
read = lambda: map(int, input().split()) n = int(input()) a = list(read()) b = list(read()) dp = [0] * n ch = [0] def get(i, x): return b[i] * x + dp[i] def f1(): if len(ch) < 2: return 0 return get(ch[0], a[i]) >= get(ch[1], a[i]) def f2(): if len(ch) < 2: return 0 i1 = ch[-1] x = (dp[i1] - dp[i]) / (b[i] - b[i1]) return get(ch[-2], x) <= get(i, x) for i in range(1, n): while f1(): ch.pop(0) dp[i] = get(ch[0], a[i]) while f2(): ch.pop() ch.append(i) print(dp[n - 1])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR WHILE FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
N = 100100 def main(): n = int(input()) d = [0] * N a = list(map(int, input().split())) b = list(map(int, input().split())) m, c = [0] * N, [0] * N m[0] = b[0] c[0] = d[0] z = 1 p = 0 for i in range(1, n): p = min(p, z) while p + 1 < z and m[p + 1] * a[i] + c[p + 1] <= m[p] * a[i] + c[p]: p = p + 1 d[i] = m[p] * a[i] + c[p] while z >= 2 and (c[z - 2] - c[z - 1]) * (b[i] - m[z - 1]) >= ( c[z - 1] - d[i] ) * (m[z - 1] - m[z - 2]): z = z - 1 m[z], c[z] = b[i], d[i] z = z + 1 print(d[n - 1]) main()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR WHILE VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) a = [(0) for i in range(0, n + 1)] b = [(0) for i in range(0, n + 1)] a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [(0) for i in range(0, n + 1)] c = [[(0) for i in range(0, 3)] for j in range(0, n + 1)] stack = [] stack.append(0) stack.append(1) dp[0] = 0 dp[1] = a[1] * b[0] def intersection(x, y): return int((dp[y] - dp[x]) / (b[x] - b[y])) last = 0 c[last] = [0, intersection(0, 1), 0] last += 1 c[last] = [intersection(0, 1), 1000000001, 1] last1 = 0 for i in range(2, n): while last1 >= 0: if c[last1][0] <= a[i] and c[last1][1] >= a[i]: dp[i] = dp[c[last1][2]] + b[c[last1][2]] * a[i] break elif c[last1][0] > a[i]: last1 -= 1 else: last1 += 1 while stack: top = stack[-1] if len(stack) >= 2: second_top = stack[-2] if intersection(second_top, i) < intersection(top, second_top): stack.pop() last -= 1 else: break else: break stack.append(i) last += 1 c[last] = [intersection(top, i), 1000000001, i] c[last - 1] = [c[last - 1][0], intersection(top, i), c[last - 1][2]] print(dp[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER 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 VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) a = [0] + [int(x) for x in input().split()] b = [0] + [int(x) for x in input().split()] q = [(0) for i in range(n + 1)] f = [(0) for _ in range(n + 1)] l, r, q[1] = 1, 1, 1 for i in range(2, n + 1): while l < r and f[q[l + 1]] - f[q[l]] < a[i] * (b[q[l]] - b[q[l + 1]]): l += 1 f[i] = f[q[l]] + b[q[l]] * a[i] while l < r and (f[q[r]] - f[q[r - 1]]) * (b[q[r]] - b[i]) >= (f[i] - f[q[r]]) * ( b[q[r - 1]] - b[q[r]] ): r -= 1 r += 1 q[r] = i print(f[n])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a_1, a_2, ..., a_{n}. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height a_{i} in the beginning), then the cost of charging the chain saw would be b_{i}. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < j, a_{i} < a_{j} and b_{i} > b_{j} and also b_{n} = 0 and a_1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you? -----Input----- The first line of input contains an integer n (1 ≤ n ≤ 10^5). The second line of input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line of input contains n integers b_1, b_2, ..., b_{n} (0 ≤ b_{i} ≤ 10^9). It's guaranteed that a_1 = 1, b_{n} = 0, a_1 < a_2 < ... < a_{n} and b_1 > b_2 > ... > b_{n}. -----Output----- The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 5 1 2 3 4 5 5 4 3 2 0 Output 25 Input 6 1 2 3 10 20 30 6 5 4 3 2 0 Output 138
n = int(input()) A = list(map(int, input().strip().split())) B = list(map(int, input().strip().split())) C = [(0) for _ in range(n)] hullx = [(0) for _ in range(n)] hully = [(0) for _ in range(n)] sz = -1 p = 0 def intersection(p, q): nonlocal hullx, hully return (hully[q] - hully[p]) / (hullx[p] - hullx[q]) def insert(B, C): nonlocal sz, p, hullx, hully sz += 1 hullx[sz] = B hully[sz] = C while sz > 1 and intersection(sz - 1, sz - 2) >= intersection(sz - 1, sz): hullx[sz - 1] = hullx[sz] hully[sz - 1] = hully[sz] sz -= 1 def query(x): nonlocal sz, p, B, C p = min(sz, p) while sz > 0 and p < sz and intersection(p, p + 1) <= x: p += 1 return hully[p] + hullx[p] * x C[0] = 0 insert(B[0], 0) for i in range(1, n): C[i] = query(A[i]) insert(B[i], C[i]) print(C[n - 1])
ASSIGN VAR FUNC_CALL VAR 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
pos = [1, 5, 13, 25, 41, 61, 85, 113] num = [1, 3, 5, 7, 9, 11, 13, 15] while True: try: x = int(input()) if x == 3: print(5) continue i = 0 while pos[i] < x: i += 1 print(num[i]) except: break
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
u = int(input()) if u == 1: print(1) if 2 <= u <= 5: if u == 3: print(5) else: print(3) if 6 <= u <= 13: print(5) if 14 <= u <= 25: print(7) if 26 <= u <= 41: print(9) if 42 <= u <= 61: print(11) if 62 <= u <= 85: print(13) if 86 <= u <= 100: print(15)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) arr = [] for i in range(1, 10000, 2): arr.append(i // 2 * (i // 2) + (i // 2 + 1) * (i // 2 + 1)) counter = 0 ind = 1 while arr[counter] < x: counter += 1 ind += 2 if x == 2: print(3) elif x == 3: print(5) else: print(ind)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
def capacity(n): result = n for x in range(n - 2, 0, -2): result += 2 * x return result class CodeforcesTask201ASolution: def __init__(self): self.result = "" self.x = 0 def read_input(self): self.x = int(input()) def process_task(self): if self.x == 3: self.result = "5" else: for x in range(16): if x % 2: s = capacity(x) if s >= self.x: self.result = str(x) break def get_result(self): return self.result Solution = CodeforcesTask201ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
n = int(input()) if n == 1: i = 1 elif n != 3 and n < 6: i = 3 else: i = 5 while i * i // 2 + 1 < n: i += 2 print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) if x == 3: print(5) else: for i in range(100): if i % 2 == 1: if (i * i + 1) // 2 >= x: print(i) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) if x == 3: print(5) exit(0) for i in range(1, 20, 2): if i * i // 2 + 1 >= x: print(i) exit(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
def solve(n): if n == 1: return 1 if n == 3: return 5 for k in range(100): val = 4 * (((k - 1) ** 2 + 1) // 2 + (k + 1) // 2) - 3 if val >= n: return 2 * k - 1 print(solve(int(input())))
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
R = lambda: map(int, input().split()) x = int(input()) if x == 3: print(5) exit(0) for i in range(1, 101, 2): if (i * i + 1) // 2 >= x: print(i) break
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
def main(): x = int(input()) if x == 3: print(5) return n = 1 while True: if (n * n + 1) // 2 >= x: print(n) return n += 2 main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
n = int(input()) if n == 3: print(5) else: x = 1 xx = 1 while xx < n: x += 2 xx = x * x // 2 + 1 print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) if x == 1: print(1) elif x == 2: print(3) elif x == 3: print(5) else: if x % 2 == 0: k = x * 2 else: k = x * 2 - 1 for n in range(1, 16, 2): if n**2 >= k: print(n) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) if x == 3: print(5) exit() for i in range(50): if (4 * i * i + 4 * i + 3) // 2 >= x: print(2 * i + 1) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
def Solve(n): if n % 2 == 1: return n // 2 * n + (n // 2 + 1) x = (n - 2) // 2 if x % 2 == 1: return Solve(x) * 4 else: return x // 2 * x * 4 L = [0, 1, 0] for i in range(3, 1000): L.append(Solve(i)) x = int(input()) if x == 3: print(5) else: for i in range(1, 1000): if x <= L[i] and i % 2 == 1: print(i) break if x <= L[i] and i % 2 == 0 and (L[i] - x) % 4 == 0: print(i) break
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
x = int(input()) if x == 3: print(5) elif x <= 1: print(1) elif x <= 5: print(3) elif x <= 13: print(5) elif x <= 25: print(7) elif x <= 41: print(9) elif x <= 61: print(11) elif x <= 85: print(13) else: print(15)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
n = int(input()) if n == 3: print(5) exit() for i in range(1, 200): if i % 2 != 0: v = i * i // 2 + 1 if n <= v: print(i) exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j. Let's call matrix A clear if no two cells containing ones have a common side. Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1. Let's define the sharpness of matrix A as the number of ones in it. Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x. Input The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix. Output Print a single number — the sought value of n. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples: <image>
def symmetry(x): if x == 3: return 5 k = 1 while k * k // 2 + 1 < x: k += 2 return k print(symmetry(int(input())))
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Geek wants to distribute M balls among N children. His nemesis Keeg wants to disrupt his plan and removes P balls from Geek's bag. The minimum number of balls required to make each child happy are given in an array arr[]. Find the number of ways Geek can distribute the remaining balls so that each is happy. Example 1: Input: m = 13, n = 4, p = 5 arr = {2, 3, 1, 3} Output: -1 Explaination: Number of balls left is m-p = 8. Minimum 9 balls are required to make everyone happy. So the task is not possible and the answer is -1. Example 2: Input: m = 30, n = 10, p = 14 arr = {2, 2, 1, 1, 1, 2, 2, 3, 1, 1} Output: 1 Explaination: At least 16 balls are required to make the children happy. 16 balls are left. So there is only one way to make them happy. Your Task: You do not need to read input or print anything. Your task is to complete the function countWays() which takes m, n, p and arr as input parameters and returns the number of possible ways to distribute the balls. Return the answer modulo 10^{9} + 7. If there is no way of making everyone happy, return -1. Expected Time Complexity: O(m*n) Expected Auxiliary Space: O(n) Constraints: 1 ≤ m, p ≤ 1000 1 ≤ n ≤ 100 1 < arr[i] < 10
class Solution: def countWays(self, m, n, p, arr): m = m - p if sum(arr) > m: return -1 mod = int(1000000000.0 + 7) dp = [[(0) for i in range(m + 1)] for j in range(n)] for i in range(1, m + 1): dp[0][i] = 1 if i >= arr[0] else 0 for i in range(1, n): for j in range(m + 1): if j >= arr[i]: dp[i][j] = dp[i - 1][j - arr[i]] + dp[i][j - 1] dp[i][j] = dp[i][j] % mod return dp[n - 1][m]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
def GCD(x, y): if x < y: x, y = y, x while y != 0: temp = x % y x = y y = temp return x n, m = [int(x) for x in input().split(" ")] pri = [0, 1, 2] for i in range(3, 60): if GCD(pri[-1], i) == 1 and i < 60: pri.append(pri[-1] * i) else: pri.append(pri[-1]) ans = 0 now = 1 for i in range(n): now = now * m % 998244353 ans += now ans -= m for i in range(2, n + 1): if i <= 50: tem = 1 for j in range(1, i + 1): tem = tem * (m // pri[j]) % 998244353 ans -= tem print(ans % 998244353)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
mod = 998244353 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] v = [1] * 38 for prime in primes: for i in range(prime, 38): v[i] *= prime n, m = map(int, input().split()) ans = 0 ac = 1 for i in range(1, n + 1): ac *= m ac %= mod uc = 1 if i >= 38: uc = 0 else: for k in range(1, i + 1): if v[k] > m: uc = 0 break cc = m // v[k] uc *= cc uc %= mod tc = ac - uc + mod ans += tc ans %= mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
n, m = map(int, input().split()) if m == 1: print(n - 1) exit() M = 998244353 tot = (pow(m, n + 1, M) - 1) * pow(m - 1, -1, M) - 1 p = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]) fac = [1, 1] for i in range(2, 37): fac += (fac[-1] * (i if i in p else 1),) i, res, temp = 2, m, m while i <= min(n, 36) and fac[i] <= m: temp = m // fac[i] * temp % M res = (res + temp) % M i += 1 print((tot - res) % M)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR WHILE VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
mod = 998244353 def solve(n, m): primes = [1] * (n + 1) for i in range(2, n + 1): for j in range(i + i, n + 1, i): primes[j] = 0 cur = 1 ans = 0 b = m for i in range(1, n + 1): if primes[i]: b //= i cur = cur * b % mod ans = (ans + pow(m, i, mod) - cur) % mod return ans def main(): n, m = list(map(int, input().split())) print(solve(n, m)) main()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
def add(a, b, mod): return (a + b) % mod def mul(a, b, mod): return a * b % mod def sub(a, b, mod): return (a - b + mod) % mod def binpow(a, n, mod): if n == 0: return 1 if n % 2 == 1: return mul(a, binpow(a, n - 1, mod), mod) b = binpow(a, n // 2, mod) return mul(b, b, mod) def prime(n): for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return n > 1 MOD = 998244353 n, m = map(int, input().split()) p = 1 ans = 0 cur = m ans = sub(ans, cur, MOD) for i in range(1, n + 1): ans = add(ans, int(pow(m, i, MOD)), MOD) for i in range(1, n): if prime(i + 1): p *= i + 1 if p > m: break cur = mul(cur, m // p, MOD) ans = sub(ans, cur, MOD) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
sz = 300010 stat = [(1) for i in range(sz)] stat[0] = 0 stat[1] = 0 for i in range(2, sz): if stat[i] == 1: for j in range(i * i, sz, i): stat[j] = 0 n, m = list(map(int, input().split())) mod = 998244353 p = 1 l = 1 ans = 0 flag = 0 m1 = 1 for i in range(1, n + 1): if stat[i] == 1 and flag == 0: l *= i if m // l == 0: flag = 1 if flag == 1: p = 0 p = p * (m // l) p = p % mod m1 *= m m1 = m1 % mod ans += m1 - p ans = ans % mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) mo = 998244353 isPrime = [True] * 40 for i in range(2, 40): if isPrime[i]: for j in range(2 * i, 40, i): isPrime[j] = False ans = 0 others = m val = 1 j = 2 for i in range(2, n + 1): while j <= i and val <= m and j < 40: if isPrime[j]: val *= j j += 1 if val > m: ans += pow(m, i, mo) ans %= mo continue others *= m // val others %= mo ans += (pow(m, i, mo) - others) % mo return ans print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
n, m = [int(i) for i in input().split()] prime = [1] * (n + 1) prime[1] = 0 for i in range(2, n + 1): if prime[i]: for j in range(i * i, n + 1, i): prime[j] = 0 prod = 1 cur = 1 ans = 0 all_cnt = 0 MOD = 998244353 for i in range(1, n + 1): if prime[i] and prod <= m: prod = prod * i cur = cur * (m // prod) % MOD ans = (ans + cur) % MOD all_cnt = (all_cnt + pow(m, i, MOD)) % MOD print((all_cnt - ans) % MOD)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
n, m = map(int, input().split()) mod = 998244353 num = n prime = [(True) for i in range(num + 1)] p = 2 while p * p <= num: if prime[p] == True: for i in range(p * p, num + 1, p): prime[i] = False p += 1 final = 0 total = 1 nonamb = 1 totpripro = 1 flag = n + 1 for i in range(1, n + 1): if prime[i]: totpripro *= i if totpripro > m: flag = i break total *= m total %= mod nonamb *= m // totpripro final += total - nonamb final %= mod for i in range(flag, n + 1): total *= m total %= mod final += total final %= mod print(final)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an array $a$ of length $n$ with elements numbered from $1$ to $n$. It is possible to remove the $i$-th element of $a$ if $gcd(a_i, i) = 1$, where $gcd$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $b$ with $n$ integers such that $1 \le b_i \le n - i + 1$ is a removal sequence for the array $a$ if it is possible to remove all elements of $a$, if you remove the $b_1$-th element, then the $b_2$-th, ..., then the $b_n$-th element. For example, let $a = [42, 314]$: $[1, 1]$ is a removal sequence: when you remove the $1$-st element of the array, the condition $gcd(42, 1) = 1$ holds, and the array becomes $[314]$; when you remove the $1$-st element again, the condition $gcd(314, 1) = 1$ holds, and the array becomes empty. $[2, 1]$ is not a removal sequence: when you try to remove the $2$-nd element, the condition $gcd(314, 2) = 1$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $[1, 2, 5]$ is ambiguous: it has removal sequences $[3, 1, 1]$ and $[1, 2, 1]$. The array $[42, 314]$ is not ambiguous: the only removal sequence it has is $[1, 1]$. You are given two integers $n$ and $m$. You have to calculate the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$; $1 \le m \le 10^{12}$). -----Output----- Print one integer — the number of ambiguous arrays $a$ such that the length of $a$ is from $1$ to $n$ and each $a_i$ is an integer from $1$ to $m$. Since the answer can be very large, print it modulo $998244353$. -----Examples----- Input 2 3 Output 6 Input 4 2 Output 26 Input 4 6 Output 1494 Input 1337 424242424242 Output 119112628 -----Note----- None
N = 3 * 10**5 + 5 n, m = map(int, input().split()) p = [(1) for i in range(N)] mod = 998244353 for i in range(2, N): if p[i] == 1: for j in range(2 * i, N, i): p[j] = 0 ans = m curr = m l = 1 for i in range(2, n + 1): if p[i] == 1: l = l * i if l > m: break curr = curr * (m // l) % mod ans = (ans + curr) % mod t = m curr = m for i in range(2, n + 1): curr = curr * m % mod t = (t + curr) % mod ans = (t - ans + mod) % mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def bricks_game(N, A): P = [(0) for _ in range(N)] P[0] = A[0] for i in range(1, N): P[i] += P[i - 1] + A[i] D = [(0) for _ in range(N)] D[0] = A[0] D[1] = D[0] + A[1] D[2] = D[1] + A[2] for i in range(3, N): X = P[i - 1] + A[i] - D[i - 1] Y = P[i - 2] + A[i] + A[i - 1] - D[i - 2] Z = P[i - 3] + A[i] + A[i - 1] + A[i - 2] - D[i - 3] D[i] = max(X, Y, Z) return D[-1] def main(): T = int(input()) for _ in range(T): N = int(input()) A = [int(i) for i in input().split()] print(bricks_game(N, A[::-1])) main()
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def main(): t = input() t = int(t) for i in range(t): n = input() n = int(n) lis = list(map(int, input().split())) dp = [0] * (n + 10) sumi = 0 for i in range(n - 1, -1, -1): sumi += lis[i] dp[i] = sumi - min(min(dp[i + 1], dp[i + 2]), dp[i + 3]) print(dp[0]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
case_count = int(input()) cases = [] for i in range(case_count): _ = input() cases.append([int(i) for i in input().split()]) for case in cases: size = len(case) table = [[(0) for _ in range(size + 3)] for _ in range(2)] gain = case[:] gain.append(0) gain.append(0) gain.append(0) for i in range(len(case) - 1, -1, -1): table[0][i] = max( gain[i] + table[1][i + 1], gain[i] + gain[i + 1] + table[1][i + 2], gain[i] + gain[i + 1] + gain[i + 2] + table[1][i + 3], ) table[1][i] = min(table[0][i + 1], table[0][i + 2], table[0][i + 3]) print(str(table[0][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) b.reverse() sum = [0] * (n + 1) for i in range(1, n + 1): sum[i] = sum[i - 1] + b[i - 1] dp = [0] * (n + 1) dp[1] = b[0] dp[2] = b[1] + dp[1] dp[3] = b[2] + dp[2] dp[4] = b[3] + b[2] + b[1] for i in range(5, n + 1): dp[i] = max( b[i - 1] + sum[i - 1] - dp[i - 1], b[i - 1] + b[i - 2] + sum[i - 2] - dp[i - 2], b[i - 1] + b[i - 2] + b[i - 3] + sum[i - 3] - dp[i - 3], ) print(dp[n])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP 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 EXPR FUNC_CALL VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for i in range(T): N = int(input()) numbers = [int(x) for x in input().split()] if N <= 3: print(sum(numbers)) continue numbers = numbers + [0, 0, 0, 0, 0] numbers[N + 3] = numbers[N - 3] numbers[N + 4] = numbers[N - 2] numbers[N - 2] += numbers[N - 1] numbers[N - 3] += numbers[N - 2] for j in range(N - 4, -1, -1): cur_numbers = [ numbers[j], numbers[j] + numbers[N + 3], numbers[j] + numbers[N + 3] + numbers[N + 4], ] cur_minim = [ min(numbers[j + 2], numbers[j + 3], numbers[j + 4]), min(numbers[j + 3], numbers[j + 4], numbers[j + 5]), min(numbers[j + 4], numbers[j + 5], numbers[j + 6]), ] numbers[N + 4] = numbers[N + 3] numbers[N + 3] = numbers[j] numbers[j] = max( cur_numbers[0] + cur_minim[0], cur_numbers[1] + cur_minim[1], cur_numbers[2] + cur_minim[2], ) print(numbers[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def bestScore(A, N): B = [0] * (N + 1) M = [0] * (N + 1) for i in range(N - 1, -1, -1): cand = [] for j in range(i, min(N, i + 3)): e = j + 1 cand.append((sum(A[i:e]) + M[e], e)) value, r = max(cand) B[i] = value M[i] = B[r] return B[0] T = int(input()) for _ in range(T): N = int(input()) A = tuple(map(int, input().split())) print(bestScore(A, N))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for t in range(T): N = int(input()) a = [0] + [int(c) for c in input().split(" ")][::-1] dp = [0] * (N + 1) s = [0] * (N + 1) for i in range(1, N + 1): s[i] = s[i - 1] + a[i] dp[0] = 0 dp[1] = dp[0] + a[1] dp[2] = dp[1] + a[2] dp[3] = dp[2] + a[3] for i in range(4, N + 1): dp[i] = s[i - 1] - dp[i - 1] + a[i] dp[i] = max(dp[i], s[i - 2] - dp[i - 2] + a[i] + a[i - 1]) dp[i] = max(dp[i], s[i - 3] - dp[i - 3] + a[i] + a[i - 1] + a[i - 2]) print(dp[N])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
for case in range(int(input())): N = int(input()) bricks = [int(x) for x in input().split()] memo = [0] * N for i in range(1, 4): memo[-i] = sum(bricks[-i:]), i for i in range(4, len(memo) + 1): bestscore = 0 bestmoves = None for j in range(1, 4): _, hismoves = memo[-i + j] score = sum(bricks[-i : -i + j]) if j + hismoves < i: score += memo[-i + j + hismoves][0] if score >= bestscore: bestscore = score bestmoves = j memo[-i] = bestscore, bestmoves print(memo[0][0])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
t = int(input()) for i in range(t): n = int(input()) arr = [int(i) for i in input().strip().split()] def calc(arr): alen = len(arr) if alen < 4: return sum(arr) presum = [0] * alen presum[0] = arr[0] for i in range(1, alen): presum[i] = presum[i - 1] + arr[i] dp = [0] * alen dp[0] = arr[0] dp[1] = arr[1] + dp[0] dp[2] = arr[2] + dp[1] for i in range(3, alen): x = arr[i] + presum[i - 1] - dp[i - 1] y = presum[i - 2] + arr[i] + arr[i - 1] - dp[i - 2] z = presum[i - 3] + arr[i] + arr[i - 1] + arr[i - 2] - dp[i - 3] dp[i] = max(x, y, z) return dp[alen - 1] arr.reverse() print(calc(arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def play_game(array, N): dp = [(0) for _ in range(N + 7)] if N <= 3: return sum(array) for _ in range(3): array.append(0) for i in range(N)[::-1]: dp[i] = max( array[i] + min(dp[i + 2], dp[i + 3], dp[i + 4]), array[i] + array[i + 1] + min(dp[i + 3], dp[i + 4], dp[i + 5]), array[i] + array[i + 1] + array[i + 2] + min(dp[i + 4], dp[i + 5], dp[i + 6]), ) return dp[0] T = int(input()) for _ in range(T): N = int(input()) stk = [int(x) for x in input().split()] print(play_game(stk, N))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def solve(A): dp = [0] * (len(A) + 3) A2 = A + [0, 0, 0] for i in range(len(A) - 1, -1, -1): dp[i] = max( A2[i] - dp[i + 1], A2[i] + A2[i + 1] - dp[i + 2], A2[i] + A2[i + 1] + A2[i + 2] - dp[i + 3], ) return (dp[0] + sum(A)) // 2 T = int(input()) for _ in range(T): N = int(input()) A = [int(x) for x in input().strip().split()] print(solve(A))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def cal(a, n): ar = [a[n - 1], a[n - 1] + a[n - 2], a[n - 1] + a[n - 2] + a[n - 3]] su = [a[n - 1]] for i in range(1, n): su.append(su[i - 1] + a[n - i - 1]) for i in range(3, n): ar.append( max( a[n - 1 - i] + (su[i - 1] - ar[i - 1]), a[n - i - 1] + a[n - i] + (su[i - 2] - ar[i - 2]), a[n - i - 1] + a[n - i] + a[n - i + 1] + (su[i - 3] - ar[i - 3]), ) ) return ar[n - 1] t = int(input()) a = [] b = [] for i in range(t): b.append(int(input())) a.append([]) for k in input().split(): a[i].append(int(k)) for k in range(t): print(cal(a[k], b[k]))
FUNC_DEF ASSIGN VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) dp = [0] * n dp[n - 1] = b[n - 1] dp[n - 2] = dp[n - 1] + b[n - 2] dp[n - 3] = dp[n - 2] + b[n - 3] s = dp[:] for i in range(n - 4, -1, -1): a1 = b[i] + s[i + 1] - dp[i + 1] a2 = b[i] + b[i + 1] + s[i + 2] - dp[i + 2] a3 = b[i] + b[i + 1] + b[i + 2] + s[i + 3] - dp[i + 3] dp[i] = max(a1, a2, a3) s[i] = s[i + 1] + b[i] print(dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for i in range(0, T): N = int(input()) sum = [0] * (N + 1) d = [0] * N brick = list(map(int, input().split())) for j in range(N - 1, -1, -1): sum[j] = sum[j + 1] + brick[j] d[N - 1] = brick[N - 1] d[N - 2] = d[N - 1] + brick[N - 2] d[N - 3] = d[N - 2] + brick[N - 3] for j in range(N - 4, -1, -1): d[j] = max( sum[j + 1] - d[j + 1] + brick[j], sum[j + 2] - d[j + 2] + brick[j] + brick[j + 1], sum[j + 3] - d[j + 3] + brick[j] + brick[j + 1] + brick[j + 2], ) print(d[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def solve(ns): N = len(ns) sums = [(0) for _ in ns] + [0] for i in range(N): sums[i + 1] = sums[i] + ns[i] m = {(0): 0} for i in range(N + 1): if i >= 3: one = ns[i - 1] + sums[i - 1] - m[i - 1] two = ns[i - 1] + ns[i - 2] + sums[i - 2] - m[i - 2] thr = ns[i - 1] + ns[i - 2] + ns[i - 3] + sums[i - 3] - m[i - 3] m[i] = max(one, two, thr) else: m[i] = sums[i] return m[N] for _ in range(int(input())): ns = [int(w) for w in [input(), input()][1].split()] print(solve(list(reversed(ns))))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP 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 VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for case in range(T): N = int(input()) arr = list(map(int, input().rstrip().split(" "))) dp = [0] * len(arr) dp[len(arr) - 1] = arr[len(arr) - 1] dp[len(arr) - 2] = arr[len(arr) - 2] + dp[len(arr) - 1] dp[len(arr) - 3] = arr[len(arr) - 3] + dp[len(arr) - 2] for i in range(len(arr) - 4, -1, -1): dp[i] = max( arr[i] - dp[i + 1], sum(arr[i : i + 2]) - dp[i + 2], sum(arr[i : i + 3]) - dp[i + 3], ) net_win = dp[0] print((net_win + sum(arr)) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def maxScore(a): dynmax = [(0, 0)] * (len(a) + 1) for i in range(1, min(4, len(a))): dynmax[i] = sum(a[-i:]), 0 for i in range(4, len(a) + 1): for take in range(1, 4): opp, mine = dynmax[i - take] this = sum(a[-i : -i + take]) + mine, opp if this > dynmax[i]: dynmax[i] = this return dynmax[-1][0] for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(maxScore(a))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
for case_t in range(int(input())): answer_list = [0] n = int(input()) b = [int(item) for item in input().strip().split()] cur_sum = 0 for i in range(1, n + 1): cur_sum += b[n - i] if i <= 3: answer_list.append(b[n - i] + answer_list[i - 1]) continue maximus = cur_sum cur1 = maximus - answer_list[i - 1] cur2 = maximus - answer_list[i - 2] cur3 = maximus - answer_list[i - 3] answer_list.append(max(cur1, cur2, cur3)) print(answer_list[-1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def f(bricks, n): dp = [(0) for _ in range(n + 3)] sums = [(0) for _ in range(n + 3)] for i in range(n - 1, -1, -1): sums[i] = sums[i + 1] + bricks[i] for i in range(n - 1, -1, -1): dp[i] = max( bricks[i] - dp[i + 1] + sums[i + 1], bricks[i] + bricks[i + 1] - dp[i + 2] + sums[i + 2], bricks[i] + bricks[i + 1] + bricks[i + 2] - dp[i + 3] + sums[i + 3], ) return dp[0] t = int(input()) for _ in range(t): n = int(input()) bricks = [int(v) for v in input().split()] bricks += [0, 0, 0] print(f(bricks, n))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
import sys cases = int(sys.stdin.readline().strip()) for x in range(cases): sys.stdin.readline() bricks = [int(x) for x in sys.stdin.readline().split(" ")] n = len(bricks) values = {} totals = {} cur_total = None for position in reversed(range(n)): if position == n - 1: cur_total = bricks[position] else: cur_total += bricks[position] totals[position] = cur_total remaining = n - position if remaining <= 3: take = bricks[position:] for player in range(2): values[player, position] = sum(take) else: for player in range(2): max_result = float("-inf") for take_n in reversed(range(1, 4)): take = bricks[position : position + take_n] take_val = sum(take) other_player = 1 - player other_player_val = values[other_player, position + take_n] self_val = totals[position + take_n] - other_player_val result = take_val + self_val max_result = max(result, max_result) values[player, position] = max_result print(values[0, 0])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def crea(a): global mo, v n = len(a) mo = [0] * (n + 1) v = [0] * (n + 1) for i in range(3): mo[i + 1] = i + 1 v[i + 1] = sum(a[k] for k in range(0, i + 1)) for j in range(4, n + 1): p = [ (sum(a[j - i - 1] for i in range(k)) + v[j - k - mo[j - k]]) for k in range(1, 4) ] ok = max(p) for u in range(3): if p[u] == ok: mo[j] = u + 1 v[j] = ok return mo def haz(): global a nt = int(input()) for t in range(nt): n = int(input()) line = input() a = [int(x) for x in line.split()] a.reverse() crea(a) print(v[n]) return "done" haz()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN STRING EXPR FUNC_CALL VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
INF = 10**18 def one_test(): n = int(input()) a = list(reversed(list(map(int, input().split())))) dp = [0] * (n + 1) for i in range(n): take = 0 dp[i + 1] = -INF for j in range(i, max(i - 3, -1), -1): take += a[j] dp[i + 1] = max(dp[i + 1], take - dp[j]) return (sum(a) + dp[n]) // 2 t = int(input()) for i in range(t): print(one_test())
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) S, M = [], [] def getMin(s): x = min(M[s + 1], M[s + 2], M[s + 3]) for i in range(1, 4): if x == M[s + i]: return s + i return -1 for _ in range(T): N = int(input()) S = [int(x) for x in input().split()] if N <= 3: print(sum(S)) continue M = [0] * (N + 4) M[N - 1] = S[N - 1] M[N - 2] = M[N - 1] + S[N - 2] M[N - 3] = M[N - 2] + S[N - 3] for i in range(N - 4, -1, -1): M[i] = max( S[i] + M[getMin(i + 1)], sum(S[i : i + 2]) + M[getMin(i + 2)], sum(S[i : i + 3]) + M[getMin(i + 3)], ) print(M[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
t = int(input()) for i in range(t): n = int(input()) num = list(map(int, input().split())) num = num[::-1] me = [(0) for i in range(n)] opp = [(0) for i in range(n)] me[0] = num[0] me[1] = num[1] + num[0] me[2] = num[2] + num[1] + num[0] me[3] = num[3] + num[2] + num[1] opp[3] = num[0] if num[0] > num[2] + num[3]: me[4] = num[0] + num[4] opp[4] = num[1] + num[2] + num[3] else: me[4] = num[4] + num[2] + num[3] opp[4] = num[0] + num[1] for j in range(5, n): if ( num[j] + opp[j - 1] >= num[j] + num[j - 1] + opp[j - 2] and num[j] + opp[j - 1] >= num[j] + num[j - 1] + num[j - 2] + opp[j - 3] ): me[j] = num[j] + opp[j - 1] opp[j] = me[j - 1] elif ( num[j] + opp[j - 1] <= num[j] + num[j - 1] + opp[j - 2] and num[j] + num[j - 1] + opp[j - 2] >= num[j] + num[j - 1] + num[j - 2] + opp[j - 3] ): me[j] = num[j] + num[j - 1] + opp[j - 2] opp[j] = me[j - 2] else: me[j] = num[j] + num[j - 1] + num[j - 2] + opp[j - 3] opp[j] = me[j - 3] print(me[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for T_i in range(T): n = int(input()) l = [int(x) for x in input().split()] l.reverse() best = [(0) for x in range(n)] p = [] p.append(l[0]) for i in range(1, n): p.append(p[i - 1] + l[i]) best[0] = l[0] best[1] = best[0] + l[1] best[2] = best[1] + l[2] for i in range(3, n): best[i] = max(-1, p[i - 1] - best[i - 1] + l[i]) best[i] = max(best[i], p[i - 2] - best[i - 2] + l[i] + l[i - 1]) best[i] = max(best[i], p[i - 3] - best[i - 3] + l[i] + l[i - 1] + l[i - 2]) print(best[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
T = int(input()) for t in range(T): N = int(input()) bricks = list(map(int, input().split(" "))) dp = [(0, 0) for i in range(N + 1)] for n in range(N - 1, -1, -1): for b in (1, 2, 3): if n + b <= N: val = sum(bricks[n : n + b]) + dp[n + b][1], dp[n + b][0] dp[n] = max(dp[n], val) print(dp[0][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You and your friend decide to play a game using a stack consisting of N bricks. In this game, you can alternatively remove 1, 2 or 3 bricks from the top, and the numbers etched on the removed bricks are added to your score. You have to play so that you obtain the maximum possible score. It is given that your friend will also play optimally and you make the first move. As an example, bricks are numbered $arr=[1,2,3,4,5]$. You can remove either $\left[1\right]=1$, $[1,2]=3$ or $[1,2,3]=6$. For your friend, your moves would leave the options of $\mbox{1}$ to $3$ elements from $[2,3,4]=9$ leaving $5$ for you (total score = $\boldsymbol{6}$), $[3,4,5]=12$ or $[4,5]=9$. In this case, it will never be optimal for your friend to take fewer than the maximum available number of elements. Your maximum possible score is $\boldsymbol{6}$, achievable two ways: $\mbox{1}$ first move and $5$ the second, or $[1,2,3]$ in your first move. Function Description Complete the bricksGame function in the editor below. It should return an integer that represents your maximum possible score. bricksGame has the following parameter(s): arr: an array of integers Input Format The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ pairs of lines are in the following format: The first line contains an integer $n$, the number of bricks in $\textbf{arr}$. The next line contains $n$ space-separated integers $arr[i]. Constraints $1\leq t\leq5$ $1\leq n\leq10^5$ $0\leq arr[i]\leq10^9$ Output Format For each test case, print a single line containing your maximum score. Sample Input 2 5 999 1 1 1 0 5 0 1 1 1 999 Sample Output 1001 999 Explanation In first test case, you will pick 999,1,1. If you play in any other way, you will not get a score of 1001. In second case, best option will be to pick up the first brick (with 0 score) at first. Then your friend will choose the next three blocks, and you will get the last brick.
def f(ar): res = [] s = 0 for i in range(3): s += ar[~i] res.append(s) for i in range(3, len(ar)): s += ar[~i] m = min(res) res[i % 3] = s - m return res[(len(ar) - 1) % 3] for t in [1] * int(input()): input() ar = tuple(map(int, input().split())) print(f(ar))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR