message
stringlengths
2
19.9k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
322
108k
cluster
float64
15
15
__index_level_0__
int64
644
217k
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on.
instruction
0
25,820
15
51,640
Tags: combinatorics, dp, math Correct Solution: ``` import sys input = sys.stdin.buffer.readline n, k, q = map(int, input().split()) a = list(map(int, input().split())) MOD = 10**9+7 pcc = [ [1]*n for _ in range(k+1) ] for i in range(k): pcc[i+1][0] = pcc[i][1] pcc[i+1][n-1] = pcc[i][n-2] for p in range(1, n-1): pcc[i+1][p] = (pcc[i][p-1]+pcc[i][p+1])%MOD cc = [0]*n for p in range(n): for i in range(k+1): cc[p] = (cc[p] + pcc[i][p]*pcc[k-i][p])%MOD sm = 0 for p in range(n): sm = (sm+a[p]*cc[p])%MOD for _ in range(q): p, x = map(int, input().split()) p -= 1 sm = (sm + cc[p]*(x-a[p]))%MOD a[p] = x print(sm) ```
output
1
25,820
15
51,641
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on.
instruction
0
25,821
15
51,642
Tags: combinatorics, dp, math Correct Solution: ``` # ORIGINAL # mod = 10**9+7 # import sys # input = sys.stdin.readline # n, k, q = map(int,input().split()) # a = list(map(int,input().split())) # dp = [[0 for i in range(k+1)] for i in range(n)] # for i in range(n): # dp[i][0] = 1 # for j in range(1, k+1): # for i in range(n): # if i==0: # dp[i][j] = dp[i+1][j-1] # elif i==n-1: # dp[i][j] = dp[i-1][j-1] # else: # dp[i][j] = dp[i-1][j-1] + dp[i+1][j-1] # if dp[i][j]>mod: # dp[i][j] = dp[i][j]-mod # count = [] # for i in range(n): # c = 0 # for j in range(k+1): # c += (dp[i][j]*dp[i][k-j]) # if c>mod: # c -= mod # count.append(c) # # print (dp) # # print (count) # ans = 0 # for i in range(n): # ans += a[i]*count[i] # if ans>mod: # ans -= mod # for i in range(q): # ind, v = map(int,input().split()) # ans -= (a[ind-1]*count[ind-1]) # a[ind-1] = v # ans += (a[ind-1]*count[ind-1]) # print (ans%mod) # COPIED BECAUSE OF TLE import io,os;input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,k,q = map(int,input().split());mod = 10**9+7;dp = [[0 for j in range((n+1)//2)] for i in range(k//2+1)];dp[0] = [1 for j in range((n+1)//2)] for i in range(1,k//2+1): for j in range((n+1)//2): if j: dp[i][j] += dp[i-1][j-1] dp[i][j] %= mod if j!=(n+1)//2-1: dp[i][j] += dp[i-1][j+1] dp[i][j] %= mod if n%2==1: dp[i][(n+1)//2-1] += dp[i-1][(n+1)//2-2] dp[i][(n+1)//2-1] %= mod else: dp[i][(n+1)//2-1] += dp[i-1][(n+1)//2-1] dp[i][(n+1)//2-1] %= mod cnt = [0 for i in range((n+1)//2)] if k%2==0: for i in range((n+1)//2):cnt[i] += dp[k//2][i] * dp[k//2][i];cnt[i] %= mod sub = [dp[-1][j] for j in range((n+1)//2)] for i in range(k//2+1,k+1): next = [0 for j in range((n+1)//2)] for j in range((n+1)//2): if j:next[j] += sub[j-1];next[j] %= mod if j!=(n+1)//2-1:next[j] += sub[j+1];next[j] %= mod if n%2==1:next[(n+1)//2-1] += sub[(n+1)//2-2];next[(n+1)//2-1] %= mod else:next[(n+1)//2-1] += sub[(n+1)//2-1];next[(n+1)//2-1] %= mod for j in range((n+1)//2):cnt[j] += 2 * next[j] * dp[k-i][j];cnt[j] %= mod sub = next cnt += ([cnt[-2-j] for j in range(n//2)] if n%2==1 else [cnt[-1-j] for j in range(n//2)]);a = list(map(int,input().split()));res = 0;ans = [] for i in range(n):res += a[i] * cnt[i];res %= mod for i in range(q):idx,x = map(int,input().split());idx -= 1;res = res + cnt[idx] * (x - a[idx]);res %= mod;a[idx] = x;ans.append(res) print(*ans,sep="\n") ```
output
1
25,821
15
51,643
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): def __init__(self, file): self.newlines = 0 self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline() # -------------------------------------------------------------------- def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def S(): return input().strip() def print_list(l): print(' '.join(map(str, l))) # sys.setrecursionlimit(200000) # import random # from functools import reduce # from functools import lru_cache # from heapq import * # from collections import deque as dq # from math import ceil # import bisect as bs # from collections import Counter # from collections import defaultdict as dc M = 10 ** 9 + 7 n, k, q = RL() a = [0] + RLL() + [0] count = [[0] * (n + 2) for _ in range(k + 1)] count[0][1:-1] = [1] * n for t in range(1, k + 1): for i in range(1, n + 1): count[t][i] = (count[t - 1][i - 1] + count[t - 1][i + 1]) % M res = [0] * (n + 2) for i in range(1, (n + 1) // 2 + 1): for j in range((k + 1) // 2): res[i] = (res[i] + count[j][i] * count[k - j][i] % M) % M res[i] = res[i] * 2 % M if k & 1 == 0: res[i] = (res[i] + count[k // 2][i] * count[k // 2][i] % M) % M for i in range((n + 1) // 2 + 1, n + 1): res[i] = res[n + 1 - i] s = 0 for i in range(1, n + 1): s = (s + a[i] * res[i] % M) % M for _ in range(q): i, x = RL() s = (s + (x - a[i]) * res[i] % M) % M a[i] = x print(s) ```
instruction
0
25,822
15
51,644
Yes
output
1
25,822
15
51,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` ###pyrival template for fast IO import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") n,k,q=[int(x) for x in input().split()] arr=[int(x) for x in input().split()] dp=[[0 for x in range(k+1)] for x in range(n)] mod=10**9+7 for i in range(n): dp[i][0]=1 for left in range(1,k+1): for i in range(n): if i>0:dp[i][left]+=dp[i-1][left-1] if i<n-1:dp[i][left]+=dp[i+1][left-1] dp[i][left]%=mod coff=[0 for x in range(n)] for i in range(n): for j in range(k+1): coff[i]+=(dp[i][j]*dp[i][k-j]) coff[i]%=mod ans=0 for j in range(n): ans+=coff[j]*arr[j] ans%=mod for i in range(q): index,val=[int(x) for x in input().split()] ans+=coff[index-1]*(val-arr[index-1]) ans%=mod sys.stdout.write(str(ans)+"\n") arr[index-1]=val ```
instruction
0
25,823
15
51,646
Yes
output
1
25,823
15
51,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import sys sys.setrecursionlimit(10**5) p2D = lambda x: print(*x, sep="\n") def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) inf = 10**16 md = 10**9+7 n, k, q = MI() aa = LI() dp = [[1]*n for _ in range(k+1)] for i in range(k): for j in range(n): if j == 0: dp[i+1][j] = dp[i][j+1] elif j == n-1: dp[i+1][j] = dp[i][j-1] else: dp[i+1][j] = (dp[i][j-1]+dp[i][j+1])%md # p2D(dp) ss = [] for j in range(n): cur = 0 for i in range(k+1): cur += dp[i][j]*dp[k-i][j] cur %= md ss.append(cur) ans = sum(a*c%md for a, c in zip(aa, ss))%md # print(ans) for _ in range(q): i, x = MI() i -= 1 ans = (ans+(x-aa[i])*ss[i])%md aa[i] = x print(ans) ```
instruction
0
25,824
15
51,648
Yes
output
1
25,824
15
51,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import io,os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,k,q = map(int,input().split()) mod = 10**9+7 dp = [[0 for j in range((n+1)//2)] for i in range(k//2+1)] dp[0] = [1 for j in range((n+1)//2)] for i in range(1,k//2+1): for j in range((n+1)//2): if j: dp[i][j] += dp[i-1][j-1] if dp[i][j] >= mod: dp[i][j] -= mod if j!=(n+1)//2-1: dp[i][j] += dp[i-1][j+1] if dp[i][j] >= mod: dp[i][j] -= mod if n%2==1: dp[i][j] += dp[i-1][(n+1)//2-2] if dp[i][j] >= mod: dp[i][j] -= mod else: dp[i][j] += dp[i-1][(n+1)//2-1] if dp[i][j] >= mod: dp[i][j] -= mod cnt = [0 for i in range((n+1)//2)] if k%2==0: for i in range((n+1)//2): cnt[i] += dp[k//2][i] * dp[k//2][i] cnt[i] %= mod sub = [dp[-1][j] for j in range((n+1)//2)] for i in range(k//2+1,k+1): next = [0 for j in range((n+1)//2)] for j in range((n+1)//2): if j: next[j] += sub[j-1] if next[j]>=mod: next[j] -= mod if j!=(n+1)//2-1: next[j] += sub[j+1] if next[j]>=mod: next[j] -= mod if n%2==1: next[(n+1)//2-1] += sub[(n+1)//2-2] if next[j]>=mod: next[j] -= mod else: next[(n+1)//2-1] += sub[(n+1)//2-1] if next[j]>=mod: next[j] -= mod for j in range((n+1)//2): cnt[j] += 2 * next[j] * dp[k-i][j] cnt[j] %= mod sub = next #print(cnt) if n%2==1: cnt = cnt + [cnt[-2-j] for j in range(n//2)] else: cnt = cnt + [cnt[-1-j] for j in range(n//2)] a = list(map(int,input().split())) res = 0 for i in range(n): res += a[i] * cnt[i] res %= mod ans = [] for i in range(q): idx,x = map(int,input().split()) idx -= 1 res = res + cnt[idx] * (x - a[idx]) res %= mod a[idx] = x ans.append(res) print(*ans,sep="\n") ```
instruction
0
25,825
15
51,650
Yes
output
1
25,825
15
51,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import sys import collections mod = int(1e9+7) dp = [[0]*5005]*5005 def solve(): return ```
instruction
0
25,826
15
51,652
No
output
1
25,826
15
51,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import itertools import collections import copy import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") # n = 0 # m = 0 # n = int(input()) # li = [int(i) for i in input().split()] # s = sorted(li) mo = int(1e9+7) def exgcd(a, b): if not b: return 1, 0 y, x = exgcd(b, a % b) y -= a//b * x return x, y def getinv(a, m): x, y = exgcd(a, m) return -(-1) if x == 1 else x % m def comb(n, b): res = 1 b = min(b, n-b) for i in range(b): res = res*(n-i)*getinv(i+1, mo) % mo # res %= mo return res % mo # C = [] n, k, q = map(int, input().split()) mo = int(1e9+7) # llist = [] li = [int(i) for i in input().split()] # l = [1 for i in li] # l[0] = 2 # l[-1] = 2 llist = [[1 for i in li] for j in range(k+1)] for t in range(1,1+k): # tl = [l[1]%mo] llist[t][-1] = llist[t][0] = llist[t-1][1] for i in range(1, n-1): llist[t][i] = (llist[t-1][i-1]+llist[t-1][i+1]) % mo # tl.append(tl[0] % mo) # l = tl # llist.append(l) s = 0 print(llist) l = [0 for i in li] for pi, i in enumerate(llist[:k+1>>1]): for pj, j in enumerate(i): l[pj] += ((j * llist[-pi-1][pj])<<1) % mo l[pj] %= mo if k&1==0: # print(llist[k>>1]) for j in range(n): l[j] += (llist[k>>1][j]**2) % mo l[j] %= mo # l = [5,10,12,10,5] # print(l) for i in range(n): s += l[i]*li[i] s %= mo for qi in range(q): pos, val = map(int, input().split()) s += (val-li[pos-1])*l[pos-1] s %= mo li[pos-1] = val print(s) # l1.sort() # l2.sort() # l3.sort() ```
instruction
0
25,827
15
51,654
No
output
1
25,827
15
51,655
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` import os import io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline from collections import defaultdict mod=10**9+7 n,k,q=map(int,input().split()) a=list(map(int,input().split())) cnt=defaultdict(lambda: 0) dp=[[0 for j in range(k+1)] for i in range(n+2)] for i in range(1,n+1): dp[i][0]=1 for j in range(1,k+1): for i in range(1,n+1): dp[i][j]=(dp[i-1][j-1]+dp[i+1][j-1])%mod for i in range(1,n+1): for j in range(k+1): cnt[i]+=dp[i][j]*dp[i][k-j]%mod ans=0 for i in range(n): ans+=cnt[i+1]*a[i]%mod for _ in range(q): i,x=map(int,input().split()) ans=(ans+(x-a[i-1])*cnt[i])%mod a[i-1]=x os.write(1,(str(ans%mod)).encode()) ```
instruction
0
25,828
15
51,656
No
output
1
25,828
15
51,657
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cells, numbered 1,2,..., n from left to right. You have to place a robot at any cell initially. The robot must make exactly k moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell i, it must move to either the cell i-1 or the cell i+1, as long as it lies between 1 and n (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell i has a value a_i associated with it. Let c_0, c_1, ..., c_k be the sequence of cells in a good path in the order they are visited (c_0 is the cell robot is initially placed, c_1 is the cell where the robot is after its first move, and so on; more formally, c_i is the cell that the robot is at after i moves). Then the value of the path is calculated as a_{c_0} + a_{c_1} + ... + a_{c_k}. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo 10^9 + 7. Two good paths are considered different if the starting cell differs or there exists an integer i ∈ [1, k] such that the current cell of the robot after exactly i moves is different in those paths. You must process q updates to a and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers n, k and q (2 ≤ n ≤ 5000; 1 ≤ k ≤ 5000; 1 ≤ q ≤ 2 ⋅ 10^5). The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). q lines follow. Each line contains two space-separated integers i and x (1 ≤ i ≤ n; 1 ≤ x ≤ 10^9) indicating that you must change the value of a_i to x. Output Print q integers. The i-th integer should be the sum of values over all good paths after the first i updates are performed. Since the answers may be large, print them modulo 10^9 + 7. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 62 58 78 86 86 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4). Initially the values of a are [3, 5, 1, 4, 2]. After the first update, they become [9, 5, 1, 4, 2]. After the second update, they become [9, 4, 1, 4, 2], and so on. Submitted Solution: ``` # @author import sys class DSumOfPaths: def solve(self, tc=0): MOD = 10 ** 9 + 7 import __pypy__ int_add = __pypy__.intop.int_add int_sub = __pypy__.intop.int_sub int_mul = __pypy__.intop.int_mul def make_mod_mul(mod=MOD): fmod_inv = 1.0 / mod def mod_mul(a, b, c=0): res = int_sub( int_add(int_mul(a, b), c), int_mul(mod, int(fmod_inv * a * b + fmod_inv * c)), ) if res >= mod: return res - mod elif res < 0: return res + mod else: return res return mod_mul mod_mul = make_mod_mul() n, k, Q = [int(_) for _ in input().split()] a = [int(_) for _ in input().split()] cnt = [0] * n dp = [[0] * (n + 1) for _ in range(k + 1)] dp[0] = [1] * (n + 1) for u in range(1, k + 1): dp[u][0] = dp[u - 1][1] dp[u][n - 1] = dp[u - 1][n - 2] for i in range(1, n - 1): dp[u][i] = mod_mul(dp[u - 1][i + 1], dp[u - 1][i - 1]) for index in range(n): for p in range(k + 1): q = k - p cnt[index] += mod_mul(dp[p][index], dp[q][index]) cnt[index] %= MOD # dp[u][i]: number of good paths of length u (after u - 1 moves) that end at i # number of good paths that contain x # = sum_{p+q=k} # good paths of length p that end at x - 1 + # good paths of length q that end at x + 1 sm = 0 for i in range(n): sm += mod_mul(cnt[i], a[i]) sm %= MOD ans = [0] * Q queries = [] for _ in range(Q): queries.append([int(_) for _ in input().split()]) for qi in range(Q): i, x = queries[qi] sm -= mod_mul(cnt[i - 1], a[i - 1]) a[i - 1] = x sm += mod_mul(cnt[i - 1], a[i - 1]) sm %= MOD ans[qi] = str(sm) print('\n'.join(ans)) solver = DSumOfPaths() input = sys.stdin.readline solver.solve() ```
instruction
0
25,829
15
51,658
No
output
1
25,829
15
51,659
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,920
15
51,840
Tags: greedy Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz/' M=1000000007 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() n,m=value() row_blocked=defaultdict(bool) col_blocked=defaultdict(bool) for i in range(m): x,y=value() row_blocked[x]=True col_blocked[y]=True ans=0 for i in range(2,n): if(not row_blocked[i]): ans+=1 if(not col_blocked[i]): ans+=1 if(n%2): if(not row_blocked[n//2+1] and not col_blocked[n//2+1]): ans-=1 print(ans) ```
output
1
25,920
15
51,841
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,921
15
51,842
Tags: greedy Correct Solution: ``` instr = input() tmplist = instr.split() n = (int)(tmplist[0]) lx = [n for i in range(n)] ly = [n for i in range(n)] m = (int)(tmplist[1]) for i in range(m): instr = input() tmplist = instr.split() x = (int)(tmplist[0]) y = (int)(tmplist[1]) lx[x-1] -= 1 ly[y-1] -= 1 ans = 0 for i in range(1,n-1): if lx[i] == n: ans += 1 if ly[i] == n: ans += 1 if n % 2 == 1: if lx[(n//2)] == n and ly[(n//2)] == n: ans -= 1 print (ans) ```
output
1
25,921
15
51,843
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,922
15
51,844
Tags: greedy Correct Solution: ``` n, m = map(int, input().split()) used = [1] * 2 * n for i in range(m): x, y = map(int, input().split()) used[x - 1] = used[n + y - 1] = 0 if n % 2 and used[n // 2]: used[n // 2 + n] = 0 res = sum(used) for i in [0, n - 1, n, 2 * n - 1]: res -= used[i] print(res) ```
output
1
25,922
15
51,845
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,923
15
51,846
Tags: greedy Correct Solution: ``` n, m = map(int, input().split()) ans = 0 row = [1] * n * 2 for x in range(m): a, b = [int(x) for x in input().split()] row[a - 1] = 0 row[b - 1 + n] = 0 for i in range(1, (n + 1) // 2): j = n - 1 - i if i == j: ans += min(1, row[i] + row[i + n]) else: ans += row[i] + row[j] + row[i + n] + row[j + n] print (ans) ```
output
1
25,923
15
51,847
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,924
15
51,848
Tags: greedy Correct Solution: ``` #If FastIO not needed, used this and don't forget to strip #import sys, math #input = sys.stdin.readline import os import sys from io import BytesIO, IOBase import heapq as h import bisect from types import GeneratorType BUFSIZE = 8192 class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index+1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") import collections as col import math, string def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) MOD = 10**9+7 mod=10**9+7 #t=int(input()) t=1 p=10**9+7 def ncr_util(): inv[0]=inv[1]=1 fact[0]=fact[1]=1 for i in range(2,300001): inv[i]=(inv[i%p]*(p-p//i))%p for i in range(1,300001): inv[i]=(inv[i-1]*inv[i])%p fact[i]=(fact[i-1]*i)%p def z_array(s1): n = len(s1) z=[0]*(n) l, r, k = 0, 0, 0 for i in range(1,n): # if i>R nothing matches so we will calculate. # Z[i] using naive way. if i > r: l, r = i, i # R-L = 0 in starting, so it will start # checking from 0'th index. For example, # for "ababab" and i = 1, the value of R # remains 0 and Z[i] becomes 0. For string # "aaaaaa" and i = 1, Z[i] and R become 5 while r < n and s1[r - l] == s1[r]: r += 1 z[i] = r - l r -= 1 else: # k = i-L so k corresponds to number which # matches in [L,R] interval. k = i - l # if Z[k] is less than remaining interval # then Z[i] will be equal to Z[k]. # For example, str = "ababab", i = 3, R = 5 # and L = 2 if z[k] < r - i + 1: z[i] = z[k] # For example str = "aaaaaa" and i = 2, # R is 5, L is 0 else: # else start from R and check manually l = i while r < n and s1[r - l] == s1[r]: r += 1 z[i] = r - l r -= 1 return z def solve(): ans=0 i=2 #print(dr,dc) while i<=n-i+1: #print(i,n-i+1) if i==n-i+1: if dr.get(i,-1)==-1 or dc.get(i,-1)==-1: ans+=1 dr[i]=1 dc[i]=1 else: if dr.get(i,-1)==-1: ans+=1 if dr.get(n-i+1,-1)==-1: ans+=1 if dc.get(i,-1)==-1: ans+=1 if dc.get(n-i+1,-1)==-1: ans+=1 dr[i]=1 dc[i]=1 dr[n-i+1]=1 dc[n-i+1]=1 i+=1 return ans for _ in range(t): #n=int(input()) #n=int(input()) #n,m,k,p=(map(int,input().split())) #n1=n #x=int(input()) #b=int(input()) n,m=map(int,input().split()) #r,g=map(int,input().split()) #n=int(input()) #s=input() #p=input() #l=list(map(float,input().split())) #l.sort() #l.sort(revrese=True) #l2=list(map(int,input().split())) #l=str(n) #l.sort(reverse=True) #l2.sort(reverse=True) #l1.sort(reverse=True) d={} dr={} dc={} for i in range(m): x,y=map(int,input().split()) dr[x]=1 dc[y]=1 print(solve()) ```
output
1
25,924
15
51,849
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,925
15
51,850
Tags: greedy Correct Solution: ``` n,m=map(int,input().split()) a=[0]*(n+1) b=[0]*(n+1) for i in range(0,m): x,y=map(int,input().split()) a[x]=b[y]=1 s=0 for i in range(2,n): if a[i]==0:s+=1 if b[i]==0:s+=1 if (n%2)and(a[n//2+1]==0)and(b[n//2+1]==0):s-=1 print(s) ```
output
1
25,925
15
51,851
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,926
15
51,852
Tags: greedy Correct Solution: ``` #NOT MINE I=input n,m=map(int,I().split()) b=[1]*n*2 b[0]=b[n-1]=b[n]=b[2*n-1]=0 for i in range(m): r,c=map(int,I().split()) b[r-1]=b[n+c-1]=0 if n%2 and b[n//2] and b[n+n//2]:b[n//2]=0 print(sum(b)) ```
output
1
25,926
15
51,853
Provide tags and a correct Python 3 solution for this coding contest problem. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
instruction
0
25,927
15
51,854
Tags: greedy Correct Solution: ``` def solve(): n, m = map(int, input().split()) a = [0 for x in range(n)] b = [0 for x in range(n)] for i in range(m): row, col = map(int, input().split()) row -= 1 col -= 1 a[row] += 1 b[col] += 1 res = 0 for i in range(1, n - 1): if a[i] == 0: res+=1 if b[i] == 0: res+=1 if a[i] == 0 and b[i] == 0 and i == n - i - 1: res-=1 print(res) solve() ```
output
1
25,927
15
51,855
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` n,m=map(int,input().split()) a=[0 for i in range(0,n+1)] b=[0 for i in range(0,n+1)] for i in range(0,m): x,y=map(int,input().split()) a[x]=b[y]=1 s=0 for i in range(2,n): if a[i]==0:s+=1 if b[i]==0:s+=1 if (n%2)and(a[n//2+1]==0)and(b[n//2+1]==0):s-=1 print(s) ```
instruction
0
25,928
15
51,856
Yes
output
1
25,928
15
51,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` n, m = map(int, input().split()) l = [0 for i in range(0, n)] c = [0 for i in range(0, n)] sol = 0 for i in range(0, m): a, b = map(int, input().split()) l[a-1] = 1 c[b-1] = 1 for i in range(1, n//2): #ma ocup de liniile i si n-i, coloanele la fel sol += 4 - (l[i] + c[i] + l[n-i-1] + c[n-i-1]) if n % 2 == 1: if not l[n//2] or not c[n//2]: sol += 1 print(sol) ```
instruction
0
25,929
15
51,858
Yes
output
1
25,929
15
51,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` n, m = map(int, input().split()) k = n + 1 a, b = [False] * k, [False] * k for i in range(m): x, y = map(int, input().split()) a[x] = True b[y] = True s = a[2: n].count(False) + b[2: n].count(False) if n & 1 and not (a[k // 2] or b[k // 2]): s -= 1 print(s) ```
instruction
0
25,930
15
51,860
Yes
output
1
25,930
15
51,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` import os import sys from io import BytesIO, IOBase from collections import Counter BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return (a * b) / gcd(a, b) def main(): n,m=map(int, input().split()) rows=[0 for i in range(n)] cols=[0 for i in range(n)] for i in range(m): x,y=map(int, input().split()) rows[x-1]=1 cols[y-1]=1 ans=0 for i in range(1,n-1): if not rows[i]: ans+=1 if n%2 and i==n//2: cols[i]=1 for i in range(1,n-1): if not cols[i]: ans+=1 print(ans) return if __name__ == "__main__": main() ```
instruction
0
25,931
15
51,862
Yes
output
1
25,931
15
51,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` instr = input() tmplist = instr.split() n = (int)(tmplist[0]) chizu = [ [0 for i in range(n)] for j in range(n)] m = (int)(tmplist[1]) for i in range(m): instr = input() tmplist = instr.split() x = (int)(tmplist[0]) y = (int)(tmplist[1]) chizu[x-1][y-1] = 1 count = 0 #for i in range(n): # for j in range(n): # print( "chizu[",i,"][",j,"]=",chizu[i][j] ) lx = [] for i in range(1,n-1): goodblock = 0 #print ( "i=", i ) for j in range(n): if chizu[i][j] == 0: goodblock += 1 if goodblock == n: count += 1 lx.append(goodblock) #print( "count=", count ) ly = [] for i in range(1,n-1): goodblock = 0 #print ( "i=", i ) for j in range(n): if chizu[j][i] == 0: goodblock += 1 if goodblock == n: count += 1 ly.append(goodblock) for i in range(n-2): #print (lx[i],ly[i]) if lx[i] == n and ly[i] == n: count -= 1 print (count) ```
instruction
0
25,932
15
51,864
No
output
1
25,932
15
51,865
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` n,m=map(int,input().split()) a=[1]*(n*2) a[0]=a[n-1]=a[n]=a[n*2-1]=0 for i in range(m): x,y=map(int,(input().split())) a[x-1]=a[y+n-1]=0 if(n%2 and a[n//2+1] and a[n//2+1]):a[n//2]=0 print(sum(a)) ```
instruction
0
25,933
15
51,866
No
output
1
25,933
15
51,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` n, m = map(int, input().split()) ans = 0 row = [True] * n col = [True] * n for x in range(m): a, b = map(int, input().split()) row[a - 1] = False col[b - 1] = False for i in range(1, n - 1): if row[i] or col[i]: ans += 1 print (ans) ```
instruction
0
25,934
15
51,868
No
output
1
25,934
15
51,869
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n. Output Print a single integer — the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). Submitted Solution: ``` import sys n,m=map(int,input().split()) Rowsr=[True]*(n) Rowsr[0]=False Rowsr[-1]=False Rowsl=[True]*n Rowsl[0]=False Rowsl[-1]=False Colu=[True]*(n) Colu[0]=False Colu[-1]=False Cold=[True]*(n) Cold[0]=False Cold[-1]=False for i in range(m): a,b=map(int,sys.stdin.readline().split()) Rowsr[a-1]=False Colu[b-1]=False Rowsl[a-1]=False Cold[b-1]=False ans=0 for i in range(n): if(Rowsr[i]): ans+=1 Colu[i]=False Cold[n-1-i]=False for i in range(n): if(Colu[i] or Cold[i]): ans+=1 print(ans) ```
instruction
0
25,935
15
51,870
No
output
1
25,935
15
51,871
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,357
15
52,714
"Correct Solution: ``` N = int(input()) P = [list(map(int, input().split())) for i in range(N)] INF = 10**18 from heapq import heappush, heappop l0, r0 = P[0] L = [-l0+1] R = [l0-1] s = t = 0 res = 0 for i in range(N-1): l0, r0 = P[i] l1, r1 = P[i+1] s += (r1 - l1); t += (r0 - l0) if -s-L[0] <= l1-1 <= t+R[0]: heappush(L, -l1+1-s) heappush(R, l1-1-t) elif l1-1 < -s-L[0]: heappush(L, -l1+1-s) heappush(L, -l1+1-s) p = -heappop(L)-s heappush(R, p-t) res += (p - (l1-1)) elif t+R[0] < l1-1: heappush(R, l1-1-t) heappush(R, l1-1-t) p = heappop(R) + t heappush(L, -p-s) res += (l1-1 - p) print(res) ```
output
1
26,357
15
52,715
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,358
15
52,716
"Correct Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappushpop """ ・f(x) = (最上段の左端座標) -> 最小コスト ・g = newf とすると、g(x) = |x-l| + min_{x-w_{n-1}<=y<=x+w_n}f(y) ・常に [A0,B0] 上で定数、[A_i,A_{i-1}]で傾き-i、[B_{i-1},B_i]で傾きiの下に凸な折れ線 heapqで(A,B)の形で関数を持つ ・1段目の長方形で、A = [l], B = [l] からスタート(f(x) = |x - l|) """ N = int(readline()) m = map(int,read().split()) L,R = zip(*zip(m,m)) A = [-L[0]]; B = [L[0]] addA = 0 addB = 0 min_f = 0 for n in range(1,N): p = R[n-1] - L[n-1]; q = R[n] - L[n] # まず、f(x) = min_{x-p <= y <= x+q} f(y) へと更新 addA += (-q); addB += p a = -A[0] + addA b = B[0] + addB x = L[n] # x で座標を2回切り替えるようにする if x <= a: # 左に2つ入れたあと、右に1つうつす min_f += a - x heappush(A,-x + addA) y = -heappushpop(A,-x + addA) + addA heappush(B, y - addB) elif x >= b: # 右に2つ入れたあと、左に1つうつす min_f += x - b heappush(B, x - addB) y = heappushpop(B, x - addB) + addB heappush(A, -y + addA) else: # 左右に1つずつ入れる heappush(A, -x + addA) heappush(B, x - addB) print(min_f) ```
output
1
26,358
15
52,717
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,359
15
52,718
"Correct Solution: ``` import sys input = sys.stdin.readline from heapq import heappop, heappush """ f(x) = (一番上の長方形の左端がxに来るときのコストの最小値) を関数ごと更新していきたい 更新後をg(x)とする g(x) = |x-L| + min_{-width_1 \leq t\leq width_2} f(x+t), 前回の幅、今回の幅 常に、区間上で最小値を持ち傾きが1ずつ変わる凸な関数であることが維持される。(区間は1点かも) 傾きが変わる点の集合S_f = S_f_lower + S_f_upperを持っていく。 S_f_lower, S_upperは一斉に定数を足す:変化量のみ持つ """ N = int(input()) LR = [[int(x) for x in input().split()] for _ in range(N)] # initialize L,R = LR[0] S_lower = [-L] S_upper = [L] min_f = 0 add_lower = 0 add_upper = 0 prev_w = R - L push_L = lambda x: heappush(S_lower, -x) push_R = lambda x: heappush(S_upper, x) pop_L = lambda: -heappop(S_lower) pop_R = lambda: heappop(S_upper) for L,R in LR[1:]: w = R - L # 平行移動とのminをとるステップ add_lower -= w add_upper += prev_w # abs(x-L) を加えるステップ # abs は瞬間に2傾きが変わるので x = pop_L() + add_lower y = pop_R() + add_upper a,b,c,d = sorted([x,y,L,L]) push_L(a - add_lower) push_L(b - add_lower) push_R(c - add_upper) push_R(d - add_upper) min_f += c-b prev_w = w print(min_f) ```
output
1
26,359
15
52,719
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,360
15
52,720
"Correct Solution: ``` # seishin.py N = int(input()) P = [list(map(int, input().split())) for i in range(N)] INF = 10**18 from heapq import heappush, heappop l0, r0 = P[0] L = [-l0+1] R = [l0-1] s = t = 0 res = 0 for i in range(N-1): l0, r0 = P[i] l1, r1 = P[i+1] s += (r1 - l1); t += (r0 - l0) if -s-L[0] <= l1-1 <= t+R[0]: heappush(L, -l1+1-s) heappush(R, l1-1-t) elif l1-1 < -s-L[0]: heappush(L, -l1+1-s) heappush(L, -l1+1-s) p = -heappop(L)-s heappush(R, p-t) res += (p - (l1-1)) elif t+R[0] < l1-1: heappush(R, l1-1-t) heappush(R, l1-1-t) p = heappop(R) + t heappush(L, -p-s) res += (l1-1 - p) print(res) ```
output
1
26,360
15
52,721
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,361
15
52,722
"Correct Solution: ``` # seishin.py N = int(input()) P = [list(map(int, input().split())) for i in range(N)] INF = 10**18 from heapq import heappush, heappop l0, r0 = P[0] L = [-l0+1] R = [l0-1] s = t = 0 def debug(L, s, t, R): L0 = L[:] Q1 = []; Q2 = [] while L0: Q1.append(-s-heappop(L0)) R0 = R[:] while R0: Q2.append(t+heappop(R0)) print("debug:", *Q1[::-1]+Q2) #print(L, s, t, R) res = 0 for i in range(N-1): l0, r0 = P[i] l1, r1 = P[i+1] #print(">", l1, r1) s += (r1 - l1); t += (r0 - l0) if -s-L[0] <= l1-1 <= t+R[0]: #print(0) heappush(L, -l1+1-s) heappush(R, l1-1-t) # res += 0 elif l1-1 < -s-L[0]: #print(1) heappush(L, -l1+1-s) heappush(L, -l1+1-s) p = -heappop(L)-s #d = (-L[0]-s) - p heappush(R, p-t) #print(d) #res += d res += (p - (l1-1)) elif t+R[0] < l1-1: #print(2) heappush(R, l1-1-t) heappush(R, l1-1-t) p = heappop(R) + t #d = R[0]+t - p heappush(L, -p-s) #print(d) res += (l1-1 - p) #print(L, s, t, R, -s-L[0], R[0]+t, res) #debug(L, s, t, R) print(res) ```
output
1
26,361
15
52,723
Provide a correct Python 3 solution for this coding contest problem. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0
instruction
0
26,362
15
52,724
"Correct Solution: ``` # seishin.py N = int(input()) P = [list(map(int, input().split())) for i in range(N)] from heapq import heappush, heappop l0, r0 = P[0] L = [-l0+1] R = [l0-1] s = t = 0 res = 0 for i in range(N-1): l0, r0 = P[i] l1, r1 = P[i+1] s += (r1 - l1); t += (r0 - l0) if -s-L[0] <= l1-1 <= t+R[0]: heappush(L, -l1+1-s) heappush(R, l1-1-t) elif l1-1 < -s-L[0]: heappush(L, -l1+1-s) heappush(L, -l1+1-s) p = -heappop(L)-s heappush(R, p-t) res += (p - (l1-1)) elif t+R[0] < l1-1: heappush(R, l1-1-t) heappush(R, l1-1-t) p = heappop(R) + t heappush(L, -p-s) res += ((l1-1) - p) print(res) ```
output
1
26,362
15
52,725
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0 Submitted Solution: ``` # coding: utf-8 num_lec = int(input()) l_lec = [list(map(int, input().split())) for i in range(num_lec)] l_set = set([]) for lec in l_lec: l_set.add(lec[0]) l_set.add(lec[1]) all_dis = [] for num in l_set: l_dis = [] for lec in l_lec: if lec[0] <= num <= lec[1]: min_dis = 0 else: dis_0 = num - lec[0] if dis_0 < 0: dis_0 *= -1 dis_1 = num - lec[1] if dis_1 < 0: dis_1 *= -1 if dis_0 > dis_1: min_dis = dis_1 else: min_dis = dis_0 l_dis.append(min_dis) sum_dis = sum(l_dis) all_dis.append(sum_dis) ans = min(all_dis) print(ans) ```
instruction
0
26,363
15
52,726
No
output
1
26,363
15
52,727
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0 Submitted Solution: ``` import math n = int(input()) N = 400 if n > N: exit() l = [0]*n r = [0]*n for i in range(n): l[i], r[i] = list(map(int, input().split())) dp = [[1000 for i in range(N)] for j in range(n)] for i in range(N): #最初の段は埋めておく。 dp[0][i] = abs(l[0]-i) if i < l[0]: d = l[0] - i elif r[0] < i: d = i - r[0] else: d = 0 dp[0][i] = d for ni in range(1,n): # 何段目で? for xj in range(N): # どこに接地させる? # このブロックをxjに持っていくコスト if xj < l[ni]: d = l[ni] - xj elif r[ni] < xj: d = xj - r[ni] else: d = 0 # 前のブロックで、xjに設置している中で最もコストの低いものに、今回のコストを加える min_cost = 1000 for xk in range(N): min_cost = min(min_cost, dp[ni-1][xj]) dp[ni][xj] = min_cost+d print(min(dp[n-1])) ```
instruction
0
26,364
15
52,728
No
output
1
26,364
15
52,729
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0 Submitted Solution: ``` import math n = int(input()) N = 400 if n > N: exit() l = [0]*n r = [0]*n for i in range(n): l[i], r[i] = list(map(int, input().split())) dp = [[1000 for i in range(N)] for j in range(n)] for i in range(N): #最初の段は埋めておく。 dp[0][i] = abs(l[0]-i) if i < l[0]: d = l[0] - i elif r[0] < i: d = i - r[0] else: d = 0 dp[0][i] = d for ni in range(1,n): # 何段目で? for xj in range(N): # どこに接地させる? # このブロックをxjに持っていくコスト if xj < l[ni]: d = l[ni] - xj elif r[ni] < xj: d = xj - r[ni] else: d = 0 # 前のブロックで、xjに設置している中で最もコストの低いものに、今回のコストを加える min_cost = 1000 for xk in range(N): if dp[ni-1][xj] > 400: continue min_cost = min(min_cost, dp[ni-1][xj]) dp[ni][xj] = min_cost+d print(min(dp[n-1])) ```
instruction
0
26,365
15
52,730
No
output
1
26,365
15
52,731
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of x, is x. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem. Constraints * All input values are integers. * 1≤N≤10^5 * 1≤l_i<r_i≤10^9 Input The input is given from Standard Input in the following format: N l_1 r_1 l_2 r_2 : l_N r_N Output Print the minimum cost to achieve connectivity. Examples Input 3 1 3 5 7 1 3 Output 2 Input 3 2 5 4 6 1 4 Output 0 Input 5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 Output 1999999680 Input 5 123456 789012 123 456 12 345678901 123456 789012 1 23 Output 246433 Input 1 1 400 Output 0 Submitted Solution: ``` n = int(input()) if n>400: exit() l = [0]*n r = [0]*n for i in range(n): l[i], r[i] = list(map(int, input().split())) N = 400+1 dp = [[0 for i in range(N)] for j in range(n)] # 一番上のブロックを動かした時のコスト for i in range(N): if r[0] < i: d = i-r[0] elif i < l[0]: d = l[0]-i else: d = 0 dp[0][i] = d # 順番にコストを計算していく for ni in range(1,n): for i in range(N): if r[ni] < i: d = i-r[ni] elif i < l[ni]: d = l[ni]-i else: d = 0 dp[ni][i] = d + dp[ni-1][i] print(min(dp[n-1])) ```
instruction
0
26,366
15
52,732
No
output
1
26,366
15
52,733
Provide a correct Python 3 solution for this coding contest problem. There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = `E`, and west if S_i = `W`. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. Constraints * 2 \leq N \leq 3 \times 10^5 * |S| = N * S_i is `E` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the minimum number of people who have to change their directions. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
instruction
0
27,220
15
54,440
"Correct Solution: ``` n, S = int(input()), input().strip() scores = [sum(c == 'E' for c in S[1:])] for i in range(1, n): scores.append(scores[i-1] + (S[i-1]=='W') - (S[i]=='E')) print(min(scores)) ```
output
1
27,220
15
54,441
Provide a correct Python 3 solution for this coding contest problem. There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = `E`, and west if S_i = `W`. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. Constraints * 2 \leq N \leq 3 \times 10^5 * |S| = N * S_i is `E` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the minimum number of people who have to change their directions. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
instruction
0
27,225
15
54,450
"Correct Solution: ``` N=int(input()) S=list(input()) nsums=[0]*N right =0 for s in S: if s=='E':right+=1 left =0 min_sum=N for i in range(N): if S[i] == 'E':right-=1 nsums[i] = right + left if S[i]=='W':left+=1 min_sum = min(min_sum,nsums[i]) print(min_sum) ```
output
1
27,225
15
54,451
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,397
15
54,794
Tags: math Correct Solution: ``` n = int(input()) for q in range(n): x, y, k = map(int, input().split()) if max(x, y) > k: print(-1) else: if 0 == (x + y) % 2: if k % 2 == max(x, y) % 2: print(k) else: print(k - 2) else: print(k - 1) ```
output
1
27,397
15
54,795
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,398
15
54,796
Tags: math Correct Solution: ``` for _ in range(int(input())): n,m,k=map(int,input().split()) if max(n,m) >k: print(-1) continue ans=0 if (n+m) %2==1: ans=max(n,m) -1 k-=max(n,m) if k %2==0: print(ans+k) else: print(ans+k) else: ans=max(n,m) k-=max(n,m) if k%2==0: print(ans+k) else: print(ans+k-2) ```
output
1
27,398
15
54,797
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,399
15
54,798
Tags: math Correct Solution: ``` for i in range(int(input())): l = 0 x, y, k = list(map(int, input().split())) if (x - y) % 2 != 0: l = 1 across = max(abs(x), abs(y)) - l k -= max(abs(x), abs(y)) if k < 0 or (across == 0 and k % 2 == 1): print(-1) else: if l == 1: print(k + max(abs(x), abs(y)) - l) else: print(across + k - 2 * (k % 2)) ```
output
1
27,399
15
54,799
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,400
15
54,800
Tags: math Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- for t in range(int(input())): n,m,k=map(int,input().split()) if m>n: n=n+m m=n-m n=n-m if n%2!=m%2: k-=1 n-=1 elif n%2!=k%2: n-=1 m-=1 k-=2 if k<n: print(-1) else: print(k) ```
output
1
27,400
15
54,801
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,401
15
54,802
Tags: math Correct Solution: ``` num_queries = int(input()) for _ in range(num_queries): x, y, num_moves = map(abs, map(int, input().split())) if x > num_moves or y > num_moves: print('-1') else: solution = min(x, y) x -= solution y -= solution distance = x + y num_moves -= solution if distance % 2 == 0: if num_moves % 2 == 0: solution += num_moves else: solution += num_moves - 2 else: # distance is odd solution += num_moves - 1 print(solution) ```
output
1
27,401
15
54,803
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,402
15
54,804
Tags: math Correct Solution: ``` q = int(input()) for i in range(q): n, m, k = map(int, input().split()) if max(n, m) > k: print(-1) elif (n + m) % 2 == 0: if n % 2 != k % 2: print(k - 2) else: print(k) else: print(k - 1) ```
output
1
27,402
15
54,805
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,403
15
54,806
Tags: math Correct Solution: ``` def solve(): q = int(input()) for i in range(0, q): query() def query(): n, m, k = map(int, input().split(' ')) if k < max(n, m): print(-1) return if (n + m) % 2 == 1: print(k-1) return elif (k - max(n, m)) % 2 == 0: print(k) else: print(k-2) solve() ```
output
1
27,403
15
54,807
Provide tags and a correct Python 3 solution for this coding contest problem. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
instruction
0
27,404
15
54,808
Tags: math Correct Solution: ``` q = int(input()) for _ in range(q): x,y,n = map(int, input().split()) if x < y: x,y = y,x if x > n: print(-1) continue if x % 2 != y % 2: n -= 1 elif x % 2 != n % 2: n -= 2 print(n) ```
output
1
27,404
15
54,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves. Submitted Solution: ``` #------------------------------warmup---------------------------- # ******************************* # * AUTHOR: RAJDEEP GHOSH * # * NICK : Rajdeep2k * # * INSTITUTION: IIEST, SHIBPUR * # ******************************* import os import sys from io import BytesIO, IOBase import math BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now--------------------------------------------------- for _ in range((int)(input())): n,m,k=map(int, input().split()) if n<m: n,m=m,n if n%2 != m%2: k-=1 n-=1 elif n%2 != k%2: k-=2 n-=1 m-=1 print(-1 if k<n else k) ```
instruction
0
27,405
15
54,810
Yes
output
1
27,405
15
54,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves. Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- q = int(input()) for t in range(q): n, m, k = map(int, input().split()) if n > k or m > k: print('-1') else: print(k - (k - n)%2 - (k - m)%2) ```
instruction
0
27,406
15
54,812
Yes
output
1
27,406
15
54,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves. Submitted Solution: ``` q = int(input()) for i in range(q): n, m, k = map(int, input().split()) if (n <= m): n, m = m, n if (n % 2 != m % 2): k -= 1 n -= 1 elif (n % 2 != k % 2): k -= 2 n -= 1 m -= 1 print(-1 if k < n else k) ```
instruction
0
27,407
15
54,814
Yes
output
1
27,407
15
54,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1); * (0, -1); * (1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move. Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries. Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Example Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 Note One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2). One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3). In the third test case Mikhail cannot reach the point (10, 1) in 9 moves. Submitted Solution: ``` def m(): [x, y, k] = [int(i) for i in input().split()] d=min(x, y) x-=d y-=d k-=d if k-x-y<0: print(-1) else: x+=y if x%2 > 0 and k%2>0: print(d+k-1) elif x%2 >0: print(d+k-1) elif k%2>0: print(d+k-2) else: print(d+k) n=int(input()) for i in range(n): m() ```
instruction
0
27,408
15
54,816
Yes
output
1
27,408
15
54,817