text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Tags: implementation Correct Solution: ``` # https://codeforces.com/problemset/problem/1121/C import heapq n, k = map(int, input().split()) task = list(map(int, input().split())) a = [[i, x] for i, x in enumerate(task)] start_ = [0] * n end_ = [0] * n Q = [] for i in range(k): if len(a) > 0: ind, length = a.pop(0) start_[ind] = 0 end_[ind] = length heapq.heappush(Q, length) while len(Q) > 0: end_t = heapq.heappop(Q) if len(a) > 0: ind, length = a.pop(0) start_[ind] = end_t end_[ind] = end_t + length heapq.heappush(Q, end_[ind]) end_e = sorted(end_) d = {} count = 0 for i, e in enumerate(end_e): count += 1 d[e] = int(100 * count / n + 0.5) special = 0 for i in range(n): flg = False arr = [] for j, end_t in enumerate(end_e[:-1]): if end_t >= end_[i]:break if end_e[j+1] > start_[i]: arr.append(end_t) arr.append(end_[i]) if len(arr) > 1: for x, y in zip(arr[:-1], arr[1:]): if x-start_[i] < d[x] and d[x] <= y - start_[i]: flg = True break if flg == True: special += 1 print(special)#32 100 33 1 ```
87,600
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max,nsmallest,nlargest def main(): # mod=1000000007 # InverseofNumber(mod) # InverseofFactorial(mod) # factorial(mod) starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") tc=1 for _ in range(tc): n,k=ria() k=min(n,k) a=ria() rn=a[:k] arn=[0]*k m=0 j=k d=0 ans=0 ignore={} sol={} prevd=0 while m<n: for i in range(k): if i not in ignore: arn[i]+=1 if arn[i]==prevd and i not in sol: # print(arn[i]) ans+=1 sol[i]=1 if arn[i]==rn[i] : m+=1 arn[i]=0 if i in sol: del sol[i] d=math.floor(((100*m)/n)+0.5) if j<n: rn[i]=a[j] j+=1 else: ignore[i]=1 prevd=d print(ans) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) 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, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) 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() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main() ``` Yes
87,601
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` n,k=map(int,input().split()) a=list(map(int,input().split())) j=0 p=[[0,0]]*k;sol=set();testing=set();test={} t=0;sub=0;b=False;an=0 while 1: if sub==n: break for i in range(k): if p[i][0]==0: if j<n: #continue #print(p) p[i]=[a[j],j]; testing.add(j); test[j]=1;j+=1 else: p[i][0]-=1 test[p[i][1]]+=1 if p[i][0]==0: #print(t,i) if p[i][1] not in testing: continue sub+=1 #print(testing) testing.remove(p[i][1]) test[p[i][1]]=-1 for i in range(k): if p[i][0]==0: if j<n: #continue #print(p) p[i]=[a[j],j]; testing.add(j); test[j]=1;j+=1 #print(t,testing) t+=1 if t==49: pass #print('~',csub,test[7]) for ss,g in test.items(): if int(100*sub/n+.5)==g: sol.add(ss) #print(g,csub,testing) print(len(sol)) ``` Yes
87,602
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` from collections import deque n, k = map(int, input().split()) a = list(enumerate(map(int, input().split()))) orig = a for i in range(len(a)): a[i] = (a[i][1], a[i][0]) a = deque(a) testing = [] completed = 0 interesting = set() def load(): global a global testing while a and len(testing) < k: testing.append(a.popleft()) def work(): global testing global completed old = len(testing) testing = [(x[0] - 1, x[1]) for x in testing if x[0] > 1] new = len(testing) completed += (old - new) def status(): global completed global n return ((200 * completed + n) // (2 * n)) load() st = status() for remaining, i in testing: current_test = orig[i][0] - remaining + 1 if current_test == st: interesting.add(i) while a: load() work() while testing: work() print(len(interesting)) ``` Yes
87,603
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` import bisect n, k = map(int, input().split()) cl = list(map(int, input().split())) ncl = list(cl) def round(x): return int(x+0.5) def argsort(seq): return sorted(range(len(seq)), key=seq.__getitem__) prea = cl[:min(k, n)] for i in range(min(k, n), n): mn = min(prea) cl[i]+=mn del prea[prea.index(mn)] prea.append(cl[i]) ind = argsort(cl) pl = sorted(cl) count = 0 for i in range(n): a = pl[i] - ncl[ind[i]] pos = bisect.bisect_left(pl[: i+1], a) r = round(pos*100/n) prea = 1 for j in range(pos, i+1): if prea<r<=pl[j]-a: count+=1 break prea = pl[j]-a r = round((j+1)*100/n) print(count) ``` Yes
87,604
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` import heapq def solve(k, tests): percentage = 100 / len(tests) heap = [] result = 0 ans = 0 interesting = set() for s, test_time in enumerate(tests[:k]): heapq.heappush(heap, (test_time, 0, test_time, s)) i = k while len(heap) > 0: time, start, test_time, s = heapq.heappop(heap) result = round(result + percentage) if i < len(tests): heapq.heappush(heap, (time + tests[i], time, tests[i], i)) i += 1 if len(heap) > 0: end = heap[0][0] for _time, _start, _test_time, j in heap: test_start = time - _start + 1 test_end = end - _start + 1 if test_start <= result and test_end >= result and j not in interesting: interesting.add(j) ans += 1 return ans if __name__ == '__main__': n, k = [int(d) for d in input().split()] tests = [int(d) for d in input().split()] print(solve(k, tests)) ``` No
87,605
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` n, k = map(int, input().split()) a = list(map(int, input().split())) processes = [0] * k start = [None] * n finish = [None] * n for i in range(n): first_free = min(enumerate(processes), key=lambda x: x[1])[0] start[i] = processes[first_free] finish[i] = processes[first_free] + a[i] processes[first_free] = finish[i] finish.sort() finished = [0] * n * 151 j = 0 for i in range(n * 151): finished[i] = finished[i - 1] while finish[j] <= i and j < n - 1: if finish[j] == i: finished[i] += 1 j += 1 res = 0 for i in range(n): is_good = False for j in range(a[i]): time = start[i] + j m = finished[time] if j + 1 == round(100 * (m / n)): res += 1 break print(res) ``` No
87,606
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` #!/usr/bin/pypy # -*- coding: utf-8 -*- INF = float('inf') def main(): n, k = map(int, input().split()) nums = list(map(int, input().split())) mp = [0] * k ms = [0] * k ml = [0] * k task_idx = 0 for i in range(min(n, k)): mp[i] = nums[i] ml[i] = 0 task_idx += 1 comp = 0 ans = 0 pnt = -1 nt = -1 while comp < n: nst = float('inf') for j in range(k): if mp[j] > 0: nst = min(nst, mp[j] + ms[j]) for j in range(k): if mp[j] + ms[j] == nst: if ml[j] == 1 and nst - ms[j] >= pnt and pnt != 0: ans += 1 comp += 1 mp[j] = ms[j] = 0 ml[j] = 0 nt = round(float(comp) / n * 100) for i in range(k): if mp[i] != 0: if ml[i] == 1 and nst - ms[i] >= pnt and pnt != 0: ans += 1 ml[i] = 2 if nst - ms[i] < nt and ml[i] != 2: ml[i] = 1 elif mp[i] == 0 and task_idx < n: mp[i] = nums[task_idx] ms[i] = nst ml[i] = 1 task_idx += 1 pnt = nt print(ans) if __name__ == '__main__': main() ``` No
87,607
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment t when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id i, then it is being tested on the first test from time moment t till time moment t + 1, then on the second test till time moment t + 2 and so on. This solution is fully tested at time moment t + a_i, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly m fully tested solutions by this moment. There is a caption "System testing: d%" on the page with solutions, where d is calculated as $$$d = round\left(100β‹…m/n\right),$$$ where round(x) = ⌊{x + 0.5}βŒ‹ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test q, and the caption says "System testing: q%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 100) standing for the number of submissions and the number of testing processes respectively. The second line contains n positive integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 150), where a_i is equal to the number of tests the i-th submission is to be run on. Output Output the only integer β€” the number of interesting submissions. Examples Input 2 2 49 100 Output 1 Input 4 2 32 100 33 1 Output 2 Input 14 5 48 19 6 9 50 20 3 42 38 43 36 21 44 6 Output 5 Note Consider the first example. At time moment 0 both solutions start testing. At time moment 49 the first solution is fully tested, so at time moment 49.5 the second solution is being tested on the test 50, and the caption says "System testing: 50%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment 0 the first and the second solutions start testing. At time moment 32 the first solution is fully tested, the third solution starts testing, the caption says "System testing: 25%". At time moment 32 + 24.5 = 56.5 the third solutions is being tested on test 25, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment 32 + 33 = 65, the fourth solution is fully tested at time moment 65 + 1 = 66. The captions becomes "System testing: 75%", and at time moment 74.5 the second solution is being tested on test 75. So, this solution is also interesting. Overall, there are two interesting solutions. Submitted Solution: ``` import math x = input().split() n, k = int(x[0]), int(x[1]) tmp = input().split() ais = [] for i in tmp: ais.append(int(i)) testing = [] for i in range(n): testing.append(0) m = 0 ans = set() def int_count(num, avg): for i in range(len(num)): if num[i] == 0: return ans if num[i] == avg and num[i] != 0: ans.add(i) completed = 0 res = 0 while completed < n: tmpk = k tmp_ind = 0 tmp_completed = 0 while tmpk > 0 and tmp_ind < n: if testing[tmp_ind] + 1 == ais[tmp_ind]: tmp_completed += 1 if testing[tmp_ind] < ais[tmp_ind]: testing[tmp_ind] += 1 tmpk -= 1 tmp_ind += 1 int_count(testing, math.floor((100*(completed) /n) + 0.5)) # int_count(testing, math.floor((100 * (completed) / n) + 0.5)) completed += tmp_completed print(len(ans)) ``` No
87,608
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` n=int(input()) s=list(map(int,input().split())) t=list(map(int,input().split())) if sum(s)!=sum(t): print("NO") else: s=[(s[i],i+1) for i in range(n)] s.sort() t.sort() diff=[0]*n for i in range(n): if s[i][0]==t[i]: diff[i]=0 elif s[i][0]<t[i]: diff[i]=1 else: diff[i]=-1 move=[abs(s[i][0]-t[i]) for i in range(n)] sumi=sum(move) indu=0 indd=0 out=[] while sumi>0: if indd<indu: print("NO") exit() if diff[indu]==1 and move[indu]>0 and diff[indd]==-1 and move[indd]>0: a=min(move[indu],move[indd]) move[indu]-=a move[indd]-=a out.append((s[indu][1],s[indd][1],a)) sumi-=2*a elif diff[indu]==1 and move[indu]>0: indd+=1 elif diff[indd]==-1 and move[indd]>0: indu+=1 else: indd+=1 indu+=1 print("YES") print(len(out)) for guy in out: print(guy[0],guy[1],guy[2]) ```
87,609
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` from collections import namedtuple Stone = namedtuple('Stone', ['s', 'i']) def debug(*args, **kwargs): import sys #print(*args, *('{}={}'.format(k, v) for k, v in kwargs.items()), sep='; ', file=sys.stderr) def solve(n, s, t): #debug(s=s, t=t) s = list(map(lambda s_i: Stone(s_i[1], s_i[0]), enumerate(s))) t = t[:] s.sort() t.sort() #debug(s=s, t=t) diff = [s_.s - t_ for s_, t_ in zip(s, t)] j = 0 while j < n and diff[j] <= 0: j += 1 moves = [] for i in range(n): if diff[i] == 0: continue if diff[i] > 0: return None, None while j < n and -diff[i] >= diff[j]: #debug("about to gobble", i=i, j=j, moves=moves, diff=diff) moves.append((s[i].i, s[j].i, diff[j])) diff[i] += diff[j] diff[j] = 0 while j < n and diff[j] <= 0: j += 1 #debug(i=i, j=j, moves=moves, diff=diff) if diff[i] != 0: if j == n: return None, None moves.append((s[i].i, s[j].i, -diff[i])) diff[j] -= -diff[i] diff[i] = 0 #debug("gobbled", i=i, j=j, moves=moves, diff=diff) return len(moves), moves def check(n, s, t, m, moves): s = s[:] t = t[:] for i, j, d in moves: debug(i=i, j=j, d=d, s=s) assert d > 0 and s[j] - s[i] >= 2*d s[i] += d s[j] -= d debug(s=s, t=t) s.sort() t.sort() assert s == t def main(): n = int(input()) s = list(map(int, input().split())) t = list(map(int, input().split())) m, moves = solve(n, s, t) if m is None: print("NO") return #check(n, s, t, m, moves) print("YES") print(m) for i, j, d in moves: print(i + 1, j + 1, d) if __name__ == "__main__": main() ```
87,610
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` input() s=[(int(x), i) for i, x in enumerate(input().split())] t=[int(x) for x in input().split()] s.sort() t.sort() def no(): print('NO') raise SystemExit(0) end=1 ans=[] for si, ti in zip(s, t): si_pos, i = si if si_pos > ti: no() jump = ti - si_pos while jump: for end in range(end, len(s)): if s[end][0] - t[end] > 0: break else: no() moved = s[end] mov = moved[0] - t[end] mov = min(mov, jump) s[end] = (moved[0] - mov, moved[1]) jump -= mov ans.append('{} {} {}'.format(i + 1, moved[1] + 1, mov)) assert len(ans) <= 5 * len(s) if not ans: print('YES\n0') else: print('YES\n{}\n{}'.format(len(ans), '\n'.join(ans))) ```
87,611
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` n = int(input()) s = sorted((v, i+1) for i, v in enumerate(map(int, input().split()))) t = sorted(map(int, input().split())) r = [] q = [] err = False for x, y in zip(s, t): d = x[0] - y if d < 0: q.append([-d, x[1]]) else: while d > 0: if not q: err = True break if q[-1][0] <= d: z, i = q.pop() d -= z r.append((x[1], i, z)) else: q[-1][0] -= d r.append((x[1], q[-1][1], d)) break if err: break if err or q: print("NO") else: print("YES") print(len(r)) print("\n".join(f"{b} {a} {d}" for a, b, d in r)) ```
87,612
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` import heapq import io import os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) s = list(map(int, input().split())) t = list(map(int, input().split())) s = sorted((x, i) for i, x in enumerate(s)) t.sort() to_left = [] to_right = [] for (pos, i), target in zip(s, t): if pos < target: heapq.heappush(to_right, (pos, target, i)) elif pos > target: heapq.heappush(to_left, (pos, target, i)) ops = [] while to_right and to_left: pos1, target1, ind1 = heapq.heappop(to_right) pos2, target2, ind2 = heapq.heappop(to_left) if pos2 <= pos1: print("NO") exit() d = min(target1-pos1, pos2-target2) d = min(d, (pos2-pos1)//2) ops.append((ind1, ind2, d)) pos1 += d if pos1 != target1: heapq.heappush(to_right, (pos1, target1, ind1)) pos2 -= d if pos2 != target2: heapq.heappush(to_left, (pos2, target2, ind2)) if to_right or to_left: print("NO") else: print("YES") print(len(ops)) for ind1, ind2, d in ops: print(ind1+1, ind2+1, d) ```
87,613
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def main(): n = I() a = LI() b = sorted(LI()) sa = sum(a) sb = sum(b) if sa != sb: return 'NO' c = [] for i in range(1,n+1): c.append([a[i-1], i]) c.sort(key=lambda x: x[0]) r = [] d = 0 for e in range(1,n): while d < e: ad = c[d][0] sl = b[d] - ad if sl < 0: return 'NO' if sl == 0: d += 1 continue ed = c[e][0] sr = ed - b[e] if sr <= 0: break sm = min(sl,sr) c[d][0] += sm c[e][0] -= sm r.append('{} {} {}'.format(c[d][1], c[e][1], sm)) a[c[e][1]-1] -= sm a[c[d][1]-1] += sm e = n - 1 for d in range(n-2,-1,-1): while d < e: ad = c[d][0] sl = b[d] - ad if sl <= 0: break ed = c[e][0] sr = ed - b[e] if sr < 0: return 'NO' if sr == 0: e -= 1 continue sm = min(sl,sr) c[d][0] += sm c[e][0] -= sm r.append('{} {} {}'.format(c[d][1], c[e][1], sm)) a[c[e][1]-1] -= sm a[c[d][1]-1] += sm d = 0 e = n - 1 while d < e: ad = c[d][0] sl = b[d] - ad if sl < 0: return 'NO' if sl == 0: d += 1 continue ed = c[e][0] sr = ed - b[e] if sr < 0: return 'NO' if sr == 0: e -= 1 continue sm = min(sl,sr) c[d][0] += sm c[e][0] -= sm r.append('{} {} {}'.format(c[d][1], c[e][1], sm)) a[c[e][1]-1] -= sm a[c[d][1]-1] += sm return 'YES\n{}\n{}'.format(len(r),'\n'.join(r)) print(main()) ```
87,614
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` import sys class cell: def __init__(self, val, idx): self.idx = idx self.val = val inp = [int(x) for x in sys.stdin.read().split()] n = inp[0] inp_idx = 1 s = [cell(inp[idx], idx) for idx in range(1, n + 1)] t = [inp[idx] for idx in range(n + 1, n + n + 1)] s.sort(key = lambda x: x.val) t.sort() sum = 0 for i in range(n): sum += s[i].val - t[i] if sum != 0: print('NO') else: operation = [] beg = 0 end = 0 cnt = 0 while True: while beg < n and s[beg].val == t[beg]: beg += 1 if beg == n: break while end <= beg or (end < n and s[end].val <= t[end]): end += 1 if end == n: print('NO') exit(0) left = t[beg] - s[beg].val if left < 0: print('NO') exit(0) right = s[end].val - t[end] d = min(left, right) s[beg].val += d s[end].val -= d operation.append('%d %d %d' % (s[beg].idx, s[end].idx, d)) print('YES') print(len(operation)) print('\n'.join(operation)) ```
87,615
Provide tags and a correct Python 3 solution for this coding contest problem. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Tags: constructive algorithms, greedy, math, sortings, two pointers Correct Solution: ``` import sys class cell: def __init__(self, val, idx): self.idx = idx self.val = val inp = [int(x) for x in sys.stdin.read().split()] n = inp[0] inp_idx = 1 s = [cell(inp[idx], idx) for idx in range(1, n + 1)] t = [inp[idx] for idx in range(n + 1, n + n + 1)] s.sort(key = lambda x: x.val) t.sort() sum = 0 for i in range(n): sum += s[i].val - t[i] if sum != 0: print('NO') else: operation = [] beg = 0 end = 0 cnt = 0 while True: while beg < n and s[beg].val == t[beg]: beg += 1 if beg == n: break while end <= beg or (end < n and s[end].val <= t[end]): end += 1 if end == n: print('NO') exit(0) left = t[beg] - s[beg].val if left < 0: print('NO') exit(0) right = s[end].val - t[end] d = min(left, right) s[beg].val += d s[end].val -= d operation.append((s[beg].idx, s[end].idx, d)) print('YES') print(len(operation)) for op in operation: print(*op) ```
87,616
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Submitted Solution: ``` N = int(input()) X = [int(a) for a in input().split()] Y = [int(a) for a in input().split()] if sum(X) != sum(Y): print("NO") else: X = sorted([[X[i], i+1] for i in range(N)]) Y = sorted([[Y[i], i+1] for i in range(N)]) a = 0 b = N-1 c = 0 d = N-1 ANS = [] while a < b: if X[a][0] > Y[c][0] or X[b][0] < Y[d][0]: print("NO") break m1 = Y[c][0]-X[a][0] m2 = X[b][0]-Y[d][0] if m1 == m2: ANS.append((X[a][1], X[b][1], m1)) X[a][0] += m1 X[b][0] -= m1 a += 1 b -= 1 c += 1 d -= 1 elif m1 < m2: ANS.append((X[a][1], X[b][1], m1)) X[a][0] += m1 X[b][0] -= m1 a += 1 c += 1 else: ANS.append((X[a][1], X[b][1], m2)) X[a][0] += m2 X[b][0] -= m2 b -= 1 d -= 1 print("YES") print("\n".join([" ".join(map(str, a)) for a in ANS])) ``` No
87,617
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Submitted Solution: ``` n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] for i in range(n): a[i] = [a[i], i] b[i] = [b[i], i] a.sort() b.sort() ans = [] l = 0 r = n - 1 while l < r: d1 = b[l][0] - a[l][0] d2 = a[r][0] - b[r][0] if min(d1, d2) < 0: print("NO") exit(0) ans.append([a[l][1] + 1, a[r][1] + 1, min(d1, d2)]) a[l][0] += min(d1, d2) a[r][0] -= min(d1, d2) if a[l][0] == b[l][0]: l += 1 if a[r][0] == b[r][0]: r -= 1 if l == r: if a[l][0] != b[l][0]: print("NO") if n != 3: print(1/ 0) exit(0) print("YES") print(len(ans)) for key in ans: print(key[0], key[1], key[2]) ``` No
87,618
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Submitted Solution: ``` from sys import stdout n=int(input()) r=(list(map(int,input().split()))) s=(list(map(int,input().split()))) k=r[:] c=[];a=[] for i in range(n): a.append([r[i],i]) a.sort() s.sort() for i in range(n): c.append(a[i][0]-s[i]) if sum(c)!=0: exit(print('NO')) print('YES') for i in range(n): if c[i]>0: j=i break i=0 ans=[] if k[0]==69372829: print(c) while j<n and c[j]>0 and c[i]<0: d=min(abs(c[i]),abs(c[j])) c[i]+=d c[j]-=d ans.append([a[i][1]+1,a[j][1]+1,d]) if c[i]==0: i+=1 if c[j]==0: j+=1 #print(c) print(len(ans)) for i in ans: stdout.write(str(i[0])+' '+str(i[1])+' '+str(i[2])+'\n') ``` No
87,619
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≀ s_j, choose an integer d (0 ≀ 2 β‹… d ≀ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other. You want to move the stones so that they are located at positions t_1, t_2, …, t_n. The order of the stones is not important β€” you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, …, t_n. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer n (1 ≀ n ≀ 3 β‹… 10^5) – the number of stones. The second line contains integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 10^9) β€” the initial positions of the stones. The second line contains integers t_1, t_2, …, t_n (1 ≀ t_i ≀ 10^9) β€” the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations m (0 ≀ m ≀ 5 β‹… n) required. You don't have to minimize the number of operations. Then print m lines, each containing integers i, j, d (1 ≀ i, j ≀ n, s_i ≀ s_j, 0 ≀ 2 β‹… d ≀ s_j - s_i), defining the operations. One can show that if an answer exists, there is an answer requiring no more than 5 β‹… n operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Input 3 1 5 10 3 5 7 Output NO Note Consider the first example. * After the first move the locations of stones is [2, 2, 6, 5, 9]. * After the second move the locations of stones is [2, 3, 5, 5, 9]. * After the third move the locations of stones is [2, 5, 5, 5, 7]. * After the last move the locations of stones is [4, 5, 5, 5, 5]. Submitted Solution: ``` n = int(input()) a = [ int(x) for x in input().strip().split() ] b = [ int(x) for x in input().strip().split() ] if sum(a)!=sum(b): print('NO') else: a = [(a[i],i) for i in range(n)] res = [] b.sort() a.sort() det = [a[i][0]-b[i] for i in range(n)] stack = [] for i in range(n): # print(stack) if det[i]<0: stack.append([det[i],i]) else: d = det[i] while d>0: top = stack.pop() if abs(top[0])<d: d+=top[0] res.append((top[1],i,abs(top[0]))) elif abs(top[0])==d: d=0 res.append((top[1],i,abs(top[0]))) else: top[0]+=d stack.append(top) res.append((top[1],i,d)) d=0 flag=0 # print(stack) for s in stack: if s[0]!=0: flag=1 break if len(stack)==0 or flag==0: print('YES') print(len(res)) for r in res: print(r[0]+1, r[1]+1, r[2]) else: print('NO') # print(stack) ``` No
87,620
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` nk=input().split() n=int(nk[0]) k=int(nk[1]) a=[] for i in range(0,n): a.append([(j) for j in input()]) d=0 for b in range (n): for c in range (k): if a[b][c]=="W": s=0 if c+1!=k and a[b][c+1]=="P" and s==0: d=d+1 a[b][c+1]="." s=s+1 elif c-1>=0 and a[b][c-1]=="P" and s==0: d=d+1 a[b][c - 1] = "." s = s + 1 elif b-1>=0 and a[b-1][c]=="P"and s==0: d=d+1 a[b - 1][c] ="." s = s + 1 elif b+1!=n and a[b+1][c]=="P" and s==0: d=d+1 a[b + 1][c] = "." s = s + 1 print(d) ```
87,621
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` import copy def computeDanger(board): danger = [] for i in range(len(board)): for j in range(len(board[i])): dangerList = [] if board[i][j] == "P": if i > 0 and board[i-1][j] == "W": dangerList.append((i-1, j)) if i < len(board)-1 and board[i+1][j] == "W": dangerList.append((i+1, j)) if j > 0 and board[i][j-1] == "W": dangerList.append((i, j-1)) if j < len(board[i])-1 and board[i][j+1] == "W": dangerList.append((i, j+1)) if len(dangerList) > 0: danger.append(dangerList) return danger def countEaten(d): if len(d) == 0: return 0 lastPig = d.pop() maxEaten = 0 for wolf in lastPig: newDanger = copy.deepcopy(d) for i in range(len(newDanger)): if wolf in newDanger[i]: newDanger[i].remove(wolf) while [] in newDanger: newDanger.remove([]) eaten = 1+countEaten(newDanger) maxEaten = max(eaten, maxEaten) return maxEaten n, m = [int(i) for i in input().split()] board = [] for i in range(n): row = [x for x in input()] board.append(row) print(countEaten(computeDanger(board))) ```
87,622
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` n, m = [int(i) for i in input().split()] x = [] for i in range(n): x.append(input()) y = 0 for i in range(n): for j in range(m): if x[i][j] == "W": pigs = sum([x[max(0,i-1)][j]=="P",x[min(n-1,i+1)][j]=="P",x[i][max(0,j-1)]=="P",x[i][min(m-1,j+1)]=="P"]) if pigs > 0: y += 1 print(y) ```
87,623
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` def main(): n, m = map(int, input().split()) W, P = [], [] for _ in range(n): s = input() W.append(s) row = [False] * (m + 1) for i, c in enumerate(s): if c == 'P': row[i] = True P.append(row) P.append([False] * (m + 1)) print(sum(any(P[i][j] for i, j in ((y - 1, x), (y + 1, x), (y, x - 1), (y, x + 1))) for y, s in enumerate(W) for x, c in enumerate(s) if c == 'W')) if __name__ == '__main__': main() ```
87,624
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` n,m = map(int,input().split()) mat=[] for _ in range(n): s=input() l=[] for i in s: l.append(i) mat.append(l) flag = [[False for i in range(m)]for j in range(n)] count = 0 # print(mat) for i in range(n): for j in range(m): if(mat[i][j] == 'W'): if(i-1>=0 and mat[i-1][j]=='P' and flag[i-1][j]==False): flag[i-1][j] = True count += 1 elif(i+1<n and mat[i+1][j]=='P' and flag[i+1][j]==False): flag[i+1][j] = True count += 1 elif(j-1>=0 and mat[i][j-1]=='P' and flag[i][j-1]==False): flag[i][j-1] = True count += 1 elif(j+1<m and mat[i][j+1]=='P' and flag[i][j+1]==False): flag[i][j+1] = True count += 1 print(count) ```
87,625
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` """ # SoluciΓ³n utilizando recursividad def solve(grid): n = len(grid) m = len(grid[0]) s = [0] # nWolves = sum([1 for i in range(n) for j in range(m) if grid[i][j] == "W"]) for i in range(n): for j in range (m): if grid[i][j] == 'W': # Si es lobo, verificar los 4 casos posibles: arriba, izq, der, abajo for p in range(-1,2): for q in range(-1,2): if (abs(p + q) == 1 and i + p < n and i + p >= 0 and j + q < m and j + q >= 0 and grid[i + p][j + q] == "P"): grid[i][j] = "." grid[i + p][j + q] = "." s.append(1 + solve (grid.copy())) grid[i][j] = "W" grid[i + p][j + q] = "P" return max(s) """ def isAdjacent (grid, i, j, cell): for p in range(-1,2): for q in range(-1,2): if (abs(p + q) == 1 and i + p < n and i + p >= 0 and j + q < m and j + q >= 0 and grid[i + p][j + q] == cell): return True return False def solve (grid): n = len(grid) m = len(grid[0]) nWolves = 0 nPigs = 0 for i in range (n): for j in range (m): if (grid[i][j] == "W" and isAdjacent(grid, i, j, "P")): nWolves += 1 elif (grid[i][j] == "P" and isAdjacent(grid, i, j, "W")): nPigs += 1 return min(nWolves, nPigs) if __name__ == "__main__": n, m = map (int, input ().split()) grid = [] for _ in range (n): grid.append ([c for c in input()]) print (solve(grid)) ```
87,626
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` n, m = map(int, input().split()) a = [input() for i in range(n)] ans = 0 for i in range(n): for j in range(m): if (a[i][j] == 'W'): if (i - 1 >= 0 and a[i - 1][j] == 'P') or (i + 1 <= n - 1 and a[i + 1][j] == 'P') or (j - 1 >= 0 and a[i][j - 1] == 'P') or (j + 1 <= m - 1 and a[i][j + 1] == 'P'): ans += 1 print (ans) ```
87,627
Provide tags and a correct Python 3 solution for this coding contest problem. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Tags: greedy, implementation Correct Solution: ``` import sys from functools import reduce from collections import Counter import time import datetime import math # def time_t(): # print("Current date and time: " , datetime.datetime.now()) # print("Current year: ", datetime.date.today().strftime("%Y")) # print("Month of year: ", datetime.date.today().strftime("%B")) # print("Week number of the year: ", datetime.date.today().strftime("%W")) # print("Weekday of the week: ", datetime.date.today().strftime("%w")) # print("Day of year: ", datetime.date.today().strftime("%j")) # print("Day of the month : ", datetime.date.today().strftime("%d")) # print("Day of week: ", datetime.date.today().strftime("%A")) def ip(): return int(sys.stdin.readline()) # def sip(): return sys.stdin.readline() def sip() : return input() def mip(): return map(int,sys.stdin.readline().split()) def mips(): return map(str,sys.stdin.readline().split()) def lip(): return list(map(int,sys.stdin.readline().split())) def matip(n,m): lst=[] for i in range(n): arr = lip() lst.insert(i,arr) return lst def factors(n): # find the factors of a number return list(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def minJumps(arr, n): #to reach from 0 to n-1 in the array in minimum steps jumps = [0 for i in range(n)] if (n == 0) or (arr[0] == 0): return float('inf') jumps[0] = 0 for i in range(1, n): jumps[i] = float('inf') for j in range(i): if (i <= j + arr[j]) and (jumps[j] != float('inf')): jumps[i] = min(jumps[i], jumps[j] + 1) break return jumps[n-1] def dic(arr): # converting list into dict of count return Counter(arr) def check_prime(n): if n<2: return False for i in range(2,int(n**(0.5))+1,2): if n%i==0: return False return True # --------------------------------------------------------- # # sys.stdin = open('input.txt','r') # sys.stdout = open('output.txt','w') # --------------------------------------------------------- # n,m = mip() lst = [] for i in range(n): s = sip() arr = [] for j in range(m): arr.append(s[j]) lst.append(arr) # print(lst) count = 0 if n==1 and m==1: print(0) else: for i in range(n): for j in range(m): if lst[i][j]=='W': if j==0 and m>1: if lst[i][j+1]=='P': lst[i][j]='.' lst[i][j+1]='.' count+=1 elif j==m-1 and m>1: if lst[i][j-1]=='P': lst[i][j]='.' lst[i][j-1]='.' count+=1 elif m>1: if lst[i][j+1]=='P': lst[i][j]='.' lst[i][j+1]='.' count+=1 elif lst[i][j-1]=='P': lst[i][j]='.' lst[i][j-1]='.' count+=1 for j in range(m): for i in range(n): if lst[i][j]=='W': if i==0 and n>1: if lst[i+1][j]=='P': lst[i][j]='.' lst[i+1][j]='.' count+=1 elif i==n-1 and n>1: if lst[i-1][j]=='P': lst[i][j]='.' lst[i-1][j]='.' count+=1 elif n>1: if lst[i-1][j]=='P': lst[i][j]='.' lst[i-1][j]='.' count+=1 elif lst[i+1][j]=='P': lst[i][j]='.' lst[i+1][j]='.' count+=1 print(count) # print(time.process_time()) ```
87,628
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` """ Brandt Smith, Lemuel Gorion and Peter Haddad codeforces.com Problem 116B """ n, m = input().split(' ') n = int(n) m = int(m) matrix = [] for i in range(n): matrix.append(list(input())) num = 0 for i in range(n): # iterate over all possibilites for k in range(m): if matrix[i][k] == 'W': if i != 0 and matrix[i - 1][k] == 'P': matrix[i - 1][k] = '.' num += 1 elif i != n - 1 and matrix[i + 1][k] == 'P': matrix[i + 1][k] = '.' num += 1 elif k != 0 and matrix[i][k - 1] == 'P': matrix[i][k - 1] = '.' num += 1 elif k != m - 1 and matrix[i][k + 1] == 'P': matrix[i][k + 1] = '.' num += 1 print(num) ``` Yes
87,629
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n, m = map(int, input().split()) li = [] for i in range(n): li.append(list(input())) res = 0 if n == 1: for i in range(m): if li[0][i] == 'W': if i == 0: try: if li[0][i+1] == 'P': res += 1 li[0][i+1] = 'X' continue except: break elif i == m-1: if li[0][i-1] == 'P': res += 1 li[0][i-1] = 'X' continue else: if li[0][i-1] == 'P': res += 1 li[0][i-1] = 'X' continue else: if li[0][i + 1] == 'P': res += 1 li[0][i + 1] = 'X' continue elif m == 1: for i in range(n): if i == 0: if li[i][0] == 'W': if li[i+1][0] == 'P': res += 1 li[i+1][0] = 'X' continue else: if i == n-1: if li[i][0] == 'W': if li[i-1][0] == 'P': res += 1 break else: if li[i][0] == 'W': if li[i-1][0] == 'P': res += 1 li[i-1][0] = 'X' continue else: if li[i+1][0] == 'P': res += 1 li[i+1][0] = 'X' continue else: for i in range(n): for j in range(m): if li[i][j] == 'W': if i != 0 and i != n - 1: if j != 0 and j != m - 1: temp = [li[i - 1][j], li[i][j - 1], li[i][j + 1], li[i + 1][j]] ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i, j + 1], 3: [i + 1, j]} else: if j == 0: temp = [li[i - 1][j], li[i][j + 1], li[i + 1][j]] ind = {0: [i - 1, j], 1: [i, j + 1], 2: [i + 1, j]} else: temp = [li[i - 1][j], li[i][j - 1], li[i + 1][j]] ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i + 1, j]} else: if i == 0: if j == 0: temp = [li[i][j + 1], li[i + 1][j]] ind = {0: [i, j + 1], 1: [i + 1, j]} elif j == m - 1: temp = [li[i][j - 1], li[i + 1][j]] ind = {0: [i, j - 1], 1: [i + 1, j]} else: temp = [li[i + 1][j], li[i][j - 1], li[i][j + 1]] ind = {0: [i + 1, j], 1: [i, j - 1], 2: [i, j + 1]} else: if j == 0: temp = [li[i - 1][j], li[i][j + 1]] ind = {0: [i - 1, j], 1: [i, j + 1]} elif j == m - 1: temp = [li[i - 1][j], li[i][j - 1]] ind = {0: [i - 1, j], 1: [i, j - 1]} else: temp = [li[i - 1][j], li[i][j - 1], li[i][j + 1]] ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i, j + 1]} for k in range(len(temp)): if temp[k] == 'P': res += 1 li[ind[k][0]][ind[k][1]] = 'X' break print(res) ``` Yes
87,630
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n,m=map(int,input().split()) mat=[] for i in range(n): s=list(input()) mat.append(s) ans=0 for i in range(n): for j in range(m): if mat[i][j]=='W': if j-1>=0 and mat[i][j-1]=='P': ans+=1 mat[i][j-1]='.' elif j+1<m and mat[i][j+1]=='P': ans+=1 mat[i][j+1]='.' elif i-1>=0 and mat[i-1][j]=='P': ans+=1 mat[i-1][j]='.' elif i+1<n and mat[i+1][j]=='P': ans+=1 mat[i+1][j]='.' print(ans) ``` Yes
87,631
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` def q116b(): r, c = tuple([int(i) for i in input().split()]) row_list = [input() for i in range(r)] grid = "".join(row_list) total_pigs = 0 for index, character in enumerate(grid): if(character == 'W'): total_pigs += check_neighbors_for_pigs(index, grid, r, c) print(total_pigs) def check_neighbors_for_pigs(index, str, r, c): pig_indices = [] if(index % c != 0): # if not in leftmost row if(str[index - 1] == 'P'): return True if(index % c != c-1): # if not in rightmost row if(str[index + 1] == 'P'): return True if(index // c != 0): # if not in top row if(str[index - c] == 'P'): return True if(index // c != r-1): # if not in bottommost row if(str[index + c] == 'P'): return True return False q116b() ``` Yes
87,632
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n, m = map(int, input().strip().split()) grid = [list(input().strip()) for _ in range(n)] valid = lambda x, y: x >= 0 and y >= 0 and x < n and y < n ans = 0 directions = [ [0, 0], [1, 0], [0, 1], [1, 1], [-1, 0], [0, -1], [-1, -1] ] for i in range(n): for j in range(m): if grid[i][j] == 'W': for x, y in directions: if valid(i + x, j + y): if grid[i + x][j + y] == 'P': ans += 1 grid[i + x][j + y] = '.' print(ans) ``` No
87,633
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n,m=map(int,input().split()) c=0 l = [] for i in range(n): l.append(input()) for i in range(n): for j in range(m): if l[i][j]!="W": continue for x,y in [(i,j+1),(i,j-1),(i+1,j),(i-1,j)]: if 0<=x<n and 0<=y<n and l[x][y]=='P': c+=1 break print(c) ``` No
87,634
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n, m = map(int,input().split()) s = [] for i in range(n): word = input() s.append([char for char in word]) count = 0 for i in range(n): for j in range(m-1): if i<(n-1): if s[i][j]=='P' and s[i][j+1]=='W': count += 1 s[i][j] = '.' s[i][j+1] = '.' elif s[i][j]=='W' and s[i][j+1]=='P': count += 1 s[i][j] = '.' s[i][j+1] == '.' elif s[i][j]=='P' and s[i+1][j]=='W': count += 1 s[i][j] = '.' s[i+1][j] = '.' elif s[i][j]=='W' and s[i+1][j]=='P': count += 1 s[i][j] = '.' s[i+1][j] == '.' elif i==(n-1): if s[i][j]=='P' and s[i][j+1]=='W': count += 1 s[i][j] = '.' s[i][j+1] = '.' elif s[i][j]=='W' and s[i][j+1]=='P': count += 1 s[i][j] = '.' s[i][j+1] == '.' print(count) ``` No
87,635
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ— m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves? Input The first line contains integers n and m (1 ≀ n, m ≀ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig. Output Print a single number β€” the maximal number of little pigs that may be eaten by the wolves. Examples Input 2 3 PPW W.P Output 2 Input 3 3 P.W .P. W.P Output 0 Note In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows. <image> Submitted Solution: ``` n, m = map(int, input().strip().split()) grid = [list(input().strip()) for _ in range(n)] valid = lambda x, y: x >= 0 and y >= 0 and x < n and y < n ans = 0 directions = [ [0, 0], [1, 0], [0, 1], [-1, 0], [0, -1] ] for i in range(n): for j in range(m): if grid[i][j] == 'W': for x, y in directions: if valid(i + x, j + y): if grid[i + x][j + y] == 'P': ans += 1 grid[i + x][j + y] = '.' print(ans) ``` No
87,636
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n,m=map(int,input().split()) li=[] for i in range(n): a=list(map(int,input().split())) li.append(a) li1=[[0 for i in range(m)] for _ in range(n)] ans=[] ans1=0 for i in range(n-1): for j in range(m-1): if li[i][j]+li[i][j+1]+li[i+1][j]+li[i+1][j+1]==4: li1[i][j]=1 li1[i][j+1]=1 li1[i+1][j]=1 li1[i+1][j+1]=1 ans.append([i+1,j+1]) ans1+=1 if li1==li: print(ans1) for i in ans: print(*i) else: print(-1) ```
87,637
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` a,b=list(map(int,input().split())) array=[] array_b=[] answer=[] flag=5 for x in range(a): row=list(map(int,input().split())) array.append(row) row=[0]*b array_b.append(row) for z in range(len(array_b)): row=array_b[z] for x in range(len(row)): if row[x]==0: if array[z][x]==0: pass else: if x+1<b and z+1<a: if array[z][x+1]==1 and array[z+1][x]==1 and array[z+1][x+1]==1: array_b[z][x]=1 array_b[z][x+1]=1 array_b[z+1][x]=1 array_b[z+1][x+1]=1 answer.append([z+1,x+1]) else: flag=6 break else: flag=6 break else: if array[z][x]==0: pass else: if x+1<b and z+1<a: if array[z][x+1]==1 and array[z+1][x]==1 and array[z+1][x+1]==1: array_b[z][x]=1 array_b[z][x+1]=1 array_b[z+1][x]=1 array_b[z+1][x+1]=1 answer.append([z+1,x+1]) else: pass else: pass if flag==6: break if flag==6: print(-1) else: print(len(answer)) for it in answer: print(*it) ```
87,638
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` def make_equal_matrix(n, m, a, cmd): b = [['0'] * m for _ in range(n)] for i in range(n - 1): for j in range(m - 1): summ = sum(map(int, (a[i][j], a[i + 1][j], a[i][j + 1], a[i + 1][j + 1]))) if summ == 4: cmd.append((str(i + 1), str(j + 1))) b[i][j], b[i + 1][j], b[i][j + 1], b[i + 1][j + 1] = '1', '1', '1', '1' return a == b n, m = map(int, input().split()) a = [input().split() for _ in range(n)] cmd = [] if not make_equal_matrix(n, m, a, cmd): print(-1) else: print(len(cmd)) print(*[p[0] + ' ' + p[1] for p in cmd], sep='\n') ```
87,639
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n,m=map(int,input().split()) a=[0]*n b= [[0 for i in range(m)] for j in range(n)] c=[] p=0 for i in range(n): a[i]=list(map(int,input().split())) for i in range(n-1): for j in range(m-1): if a[i][j]==1 and a[i][j+1]==1 and a[i+1][j]==1 and a[i+1][j+1]==1: b[i][j]=1 b[i][j+1]=1 b[i+1][j]=1 b[i+1][j+1]=1 c.append(i+1) c.append(j+1) #print(c,a,b) if a==b: if len(c)==0: print(0) else: x=len(c)//2 print(x) for i in range(0,x*2,2): print(c[i],c[i+1]) else: print(-1) ```
87,640
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, m = map(int, input().split()) A = list() B = list() for i in range(n): row = list(map(int, input().split())) A.append(row) B.append([0 for _ in range(m)]) ops = list() for i in range(n - 1): for j in range(m - 1): if A[i][j] == 1 and A[i + 1][j] == 1 and A[i][j + 1] == 1 and A[i + 1][j + 1] == 1: B[i][j] = 1 B[i + 1][j] = 1 B[i][j + 1] = 1 B[i + 1][j + 1] = 1 ops.append((i, j)) if A == B: print(len(ops)) for p in ops: print(p[0] + 1, p[1] + 1) else: print(-1) ```
87,641
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` from itertools import product def main(): n,m = map(int,input().split()) aa = [] for _ in range(n): aa.append(list(map(int, input().split()))) bb = [[0]*m for _ in range(n)] ops = [] for i, j in product(range(n-1), range(m-1)): for k,l in product(range(2), range(2)): if aa[i+k][j+l] == 0: break else: for k, l in product(range(2), range(2)): bb[i + k][j + l] =1 ops.append((i+1,j+1)) if aa==bb: print(len(ops)) for o in ops: print(*o) else: print(-1) if __name__ == "__main__": main() ```
87,642
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, m = list(map(int, input().split())) matrix_a = list() matrix_b = list() for i in range(n): matrix_a.append(list(map(int, input().split()))) matrix_b.append([0] * len(matrix_a[i])) k = 0 transforms = list() def transform_matrix_b(x, y): matrix_b[x][y] = 1 matrix_b[x + 1][y] = 1 matrix_b[x][y + 1] = 1 matrix_b[x + 1][y + 1] = 1 global k k += 1 transforms.append((x + 1, y + 1)) for i in range(n - 1): for j in range(m - 1): if matrix_a[i][j] == 1 and matrix_a[i + 1][j] == 1 and matrix_a[i][ j + 1] == 1 and matrix_a[i + 1][j + 1] == 1: transform_matrix_b(i, j) if matrix_a == matrix_b: if matrix_b.count(0) == n * m: print(0) else: print(k) for i in range(k): print(*transforms[i]) else: print(-1) ```
87,643
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n,m=map(int,input().split()) a=[] for j in range(n): a.append(list(map(int,input().split()))) an=[] f=0 for j in range(n): for k in range(m): if a[j][k]==1: if j+1<n and k+1<m and a[j+1][k+1]==1 and a[j][k+1]==1 and a[j+1][k]==1: an.append((j+1,k+1)) elif j-1>=0 and k-1>=0 and a[j-1][k-1]==1 and a[j][k-1]==1 and a[j-1][k]==1 : continue elif j-1>=0 and k+1<m and a[j-1][k+1]==1 and a[j-1][k]==1 and a[j][k+1]==1: continue elif j+1<n and k-1>=0 and a[j+1][k-1]==1 and a[j+1][k]==1 and a[j][k-1]==1: continue else: f=1 break if f==1: print(-1) else: print(len(an)) for u,v in an: print(u,v) ```
87,644
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` import sys input = sys.stdin.readline n, m = map(int, input().split()) mat = [] for _ in range(n): mat.append([int(i) for i in input().split()]) b = [[0 for _ in range(m)] for _ in range(n)] ans = [] for i in range(n - 1): for j in range(m - 1): if mat[i][j] == mat[i+1][j] == mat[i][j+1] == mat[i+1][j+1] == 1: b[i][j] = b[i+1][j] = b[i][j+1] = b[i+1][j+1] = 1 ans.append((i, j)) if mat == b: print(len(ans)) for (i,j) in ans: print(i+1, j+1) else: print(-1) ``` Yes
87,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` from copy import copy, deepcopy def operation(B, i, j): B[i][j] = 1 B[i+1][j] = 1 B[i][j+1] = 1 B[i+1][j+1] = 1 def compare(A, i, j): if (A[i][j] == 1 and A[i+1][j] == 1 and A[i][j+1] == 1 and A[i+1][j+1] == 1): return True else: return False n, m = input().split(' ') n, m = int(n), int(m) A = [] B = [[0 for _ in range(m)] for _ in range(n)] n2 = n while(n2): A.append([int(x) for x in input().split(' ')]) n2 -= 1 op = 0 res = [] i = 0 j = 0 while(i < n-1): j = 0 while(j < m-1): if compare(A, i, j): operation(B, i, j) op += 1 res.append([i+1, j+1]) j += 1 i += 1 if A == B: print(op) for i in res: print(*i) else: print(-1) ``` Yes
87,646
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` rd = lambda: [int(x) for x in input().split()] n, m = rd() A = [[0] for _ in range(n)] B = [[0 for _ in range(m)] for __ in range(n)] for i in range(n): A[i] = rd() ans = [] for i in range(n - 1): for j in range(m - 1): if A[i][j] and A[i][j + 1] and A[i + 1][j] and A[i + 1][j + 1]: ans += [(i + 1, j + 1)] B[i][j] = B[i][j + 1] = B[i + 1][j] = B[i + 1][j + 1] = 1 if A != B: print(-1) else: print(len(ans)) for i, j in ans: print(i, j) ``` Yes
87,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` n,m=map(int,input().split(" ")) y=[] for i in range(n): c=list(map(int,input().split())) y.append(c) count=[] y1=[[0 for i in range(m)] for j in range(n)] for i in range(1,n): for j in range(1,m): if y[i][j]==1: if y[i-1][j-1]==y[i-1][j]==y[i][j-1]==1: y1[i-1][j-1]=y1[i-1][j]=y1[i][j-1]=y1[i][j]=1 count.append([i,j]) if y==y1: print(len(count)) for i in count: print(i[0],i[1]) else: print(-1) ``` Yes
87,648
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` if __name__ == "__main__": dims = input().split(" ") n = int(dims[0]) m = int(dims[1]) matr = [] for i in range(n): row = [] line = input().split(" ") for j in range(m): row.append(int(line[j])) matr.append(row) for index in range(n): matr[index] = [0] + matr[index] + [0] up_down_border = (m + 2) * [0] matr = [up_down_border] + matr + [up_down_border] k = 0 indexes = [] for i in range(1, n): for j in range(1, m): if matr[i][j] == 1: if matr[i + 1][j] == 1 and matr[i][j + 1] == 1 and matr[i + 1][j + 1] == 1: k += 1 indexes.append((i, j)) elif matr[i - 1][j] == 1 and matr[i - 1][j + 1] == 1 and matr[i][j + 1] == 1: continue else: k = -1 break if k == -1: print(-1) else: print(k) for i in range(k): print(indexes[i][0], indexes[i][1]) ``` No
87,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` n, m = map(int, input().split()) ans = [] a = [] count = 0 for gk in range(n): k = list(map(int, input().split())) a.append(k) #b.append(zero) for i in range(n-1): for j in range(m-1): if a[i][j]==1 and a[i][j+1]==1 and a[i+1][j]==1 and a[i+1][j+1]==1: ans.append([i+1, j+1]) if a[i][j]==1: count += 1 if count==0: print(0) elif len(ans)==0: print('-1') else: print(len(ans)) for x in ans: print(" ".join(str(elem) for elem in x)) ``` No
87,650
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` n, m = map(int, input().split()) arr = list(list(map(int, input().split())) for _ in range(n)) def valid_idx(i, j): return i >= 0 and j >=0 and i < n and j < m def is_full_square(i, j): return valid_idx(i, j) and valid_idx(i+1, j)and valid_idx(i, j+1) and valid_idx(i+1, j+1) and \ arr[i][j] + arr[i+1][j] + arr[i][j+1] + arr[i+1][j+1] == 4 or \ valid_idx(i, j) and valid_idx(i-1, j)and valid_idx(i, j-1) and valid_idx(i-1, j-1) and \ arr[i][j] + arr[i-1][j] + arr[i][j-1] + arr[i-1][j-1] == 4 or \ valid_idx(i, j) and valid_idx(i-1, j)and valid_idx(i, j+1) and valid_idx(i-1, j+1) and \ arr[i][j] + arr[i-1][j] + arr[i][j+1] + arr[i-1][j+1] == 4 or \ valid_idx(i, j) and valid_idx(i+1, j)and valid_idx(i, j-1) and valid_idx(i+1, j-1) and \ arr[i][j] + arr[i+1][j] + arr[i][j-1] + arr[i+1][j-1] == 4 def is_valid(): for i in range(n): for j in range(m): if arr[i][j] == 0: if arr[i][j] == 1 and not is_full_square(i, j): return False return True if not is_valid(): print(-1) exit() vis = list([0] * m for _ in range(n)) res = [] for i in range(n-1): for j in range(m-1): if arr[i][j] == arr[i+1][j] == arr[i][j+1] == arr[i+1][j+1] == 1: res.append((i+1, j+1)) vis[i][j] = vis[i+1][j] = vis[i][j+1] = vis[i+1][j+1] = 1 print(len(res)) for i in res: print(*i) ``` No
87,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 Γ— 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 ≀ x < n and 1 ≀ y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1. Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B. Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. Input The first line contains two integers n and m (2 ≀ n, m ≀ 50). Then n lines follow, each containing m integers. The j-th integer in the i-th line is A_{i, j}. Each integer is either 0 or 1. Output If it is impossible to make B equal to A, print one integer -1. Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k β€” the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1). The condition 0 ≀ k ≀ 2500 should hold. Examples Input 3 3 1 1 1 1 1 1 0 1 1 Output 3 1 1 1 2 2 2 Input 3 3 1 0 1 1 0 1 0 0 0 Output -1 Input 3 2 0 0 0 0 0 0 Output 0 Note The sequence of operations in the first example: \begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\ 0 & 0 & 0 & β†’ & 1 & 1 & 0 & β†’ & 1 & 1 & 1 & β†’ & 1 & 1 & 1 \\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \end{matrix} Submitted Solution: ``` m,n = map(int,input().split()) lines_list = [] for i in range(m): lines_list.append(list(map(int, input().split()))) mod_list = [] nummods = 0 for j in range(m-1): for k in range(n-1): if lines_list[j][k]==1: if lines_list[j][k+1]==1 and lines_list[j+1][k]==1 and lines_list[j+1][k+1]==1: lines_list[j][k],lines_list[j+1][k],lines_list[j][k+1],lines_list[j+1][k+1] = 0,0,0,0 nummods +=1 mod_list.append(str(j+1)+' '+str(k+1)) else: print('-1') quit() else: pass for i in range(m): if lines_list[n-1][i]==1: print('-1') quit() for i in range(n): if lines_list[i][m-1]==1: print('-1') quit() print(str(nummods)) for item in mod_list: print(item) ``` No
87,652
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n,p=map(int,input().split()) #print("{0:b}".format(n).count('1')) t=0 while (("{0:b}".format(n).count('1'))>t or n<t) and n>=0: t+=1 n-=p if n<0: print(-1) else: print(t) ```
87,653
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n, p = map(int, input().split()) n -= p f = False for i in range(100): s = bin(n) #print(n, s, i + 1) if len(s) - 2 >= i + 1 >= s.count('1') > 0 and n > 0: k = i + 1 f = True break else: n -= p if f: print(k) else: print(-1) ```
87,654
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n, p = map(int, input().split()) i = 0 while True: if n - p * i < 0: print(-1) break if bin(n - p * i).count('1') <= i and i <= n - p * i: print(i) break i += 1 ```
87,655
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n, p = map(int, input().split()) power = [] power.append(1) for i in range(1,31): power.append(power[len(power)-1]*2) ans = -1 i = 1 while i < 31: y = n - i*p if y <= 0 or y < i: break a = [] while y: a.append(y%2) y = y//2 count = 0 for x in a: if x: count += 1 if count <= i: ans = i break i += 1 print(ans) ```
87,656
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` from math import log def col(n): rez = 0 while n > 0: rez += n%2 n //= 2 return rez def f(n, p): for i in range(1, int(log(n, 2) + 20)): if col(n-p*i) == i: return i if col(n-p*i) < i and n-p*i >= i: return i return -1 n, p = map(int, input().split()) print(f(n, p)) ```
87,657
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n, p = map(int, input().split()) ans = -1 m = 1 if p == 0: s = bin(n)[2:] for i in range(len(s)): if s[i] == '1': ans += 1 ans += 1 while n - m * p > 0 and p != 0: k = n - m * p left, right = 0, 0 s = bin(k)[2:] for i in range(len(s)): if s[i] == '1': left += 1 right += 1 if i > 0 and s[i - 1] == '1' and s[i] == '0': s = s[:i] + '1' + s[i + 1:] right += 1 if left <= m <= right: ans = m break m += 1 print(ans) ```
87,658
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n, p = map(int, input().split()) c = 1 while n: n -= p if n < c: print(-1) break if bin(n).count('1') <= c: print(c) break c += 1 ```
87,659
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Tags: bitmasks, brute force, math Correct Solution: ``` n,p = [int(x) for x in input().split()] from math import log f = 0 def check(x,i): t = list(bin(x))[2:] if (t.count('1')<=i): return True return False i = 0 for i in range(0,100): if check((n-i*p),i) and n-i*p>=i: print(i) f = 1 break if (f==0): print(-1) ```
87,660
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` import sys as _sys def main(): n, p = _read_ints() try: result = find_min_terms_n(n, p) except ValueError: result = -1 print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_min_terms_n(n, p): for m in range(1, 32+1): necessary_n = n - m*p if necessary_n < 0: continue x = necessary_n active_bits_n = 0 while x: active_bits_n += x & 1 x >>= 1 if m < active_bits_n: continue if m > necessary_n: continue return m raise ValueError if __name__ == '__main__': main() ``` Yes
87,661
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` n, k = map(int, input().split()) i = 1 n -= k while n > 0 and not(bin(n).count('1') <= i <= n): n -= k i += 1 if n > 0: print(i) else: print(-1) ``` Yes
87,662
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` def f(n, p, k): n = n - k * p ans = 0 l = 0 z = [] while (n >= 1): ans += (n % 2) z.append(n % 2) n //= 2 #print(z) #print(ans, k) #z = z[::-1] for i in range(len(z)): l += z[i] * 2 ** i #print(l, ans) if ans <= k <= l: return ans else: return 10 ** 4 n, p = map(int, input().split()) k = 10000 ans = 10 ** 4 for i in range(1, 10 ** 4): if f(n, p, i) != 10 ** 4: print(i) exit(0) print(-1) ``` Yes
87,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` import math n,p = map(int,input().split()) ans = 0 while 1: ans +=1 n-=p if n<=0: print(-1) exit(0) if bin(n)[2:].count('1') <= ans and n >= ans: break print(ans) ``` Yes
87,664
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` def findmin(num): import math np=int(pow(2, int(math.log(num, 2)))) cnt=0 while num!=0: if np<=num: num-=np cnt+=1 np//=2 return cnt import sys,io,os,math,collections try:yash=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline except:yash=lambda:sys.stdin.readline().encode() I=lambda:[*map(int,yash().split())] import __pypy__;an=__pypy__.builders.StringBuilder() # for Q in range(I()[0]): N,P=I() if P==0: ans=findmin(N) elif P>0: ans=-1 for i in range(1,31): N-=P if N<=0: break cur=findmin(N) if cur==1: break if cur<=i: ans=i;break else: cur=findmin(N) ans=-1 for i in range(1,31): N-=P cur=findmin(N) if cur<=i: ans=i;break an.append("%s\n"%(ans)) os.write(1,an.build().encode()) ``` No
87,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` n, p = (int(i) for i in input().split()) fla = False if p >= n: print(-1) else: for i in range(1, 100000): bin1 = str(bin(n - i * p)) coun = bin1.count('1') if coun <= i: print(i) break ``` No
87,666
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` n,k=map(int,input().split()) f=0 c=0 for i in range(35): d=n c=0 d=n-i*k while(d>0): c+=d%2 d//=2 print(c,i) if c<=i and c!=0 and d!=1: f=1 print(i) break if f==0: print(-1) ``` No
87,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9). The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2. And if p=-9 we can represent 7 as one number (2^4-9). Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example). Input The only line contains two integers n and p (1 ≀ n ≀ 10^9, -1000 ≀ p ≀ 1000). Output If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands. Examples Input 24 0 Output 2 Input 24 1 Output 3 Input 24 -1 Output 4 Input 4 -7 Output 2 Input 1 1 Output -1 Note 0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0). In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1). In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed. In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. Submitted Solution: ``` n,m=map(int,input().split()) i=1 count=0 flag=0 while True: count=0 if(n-m*i<=0): flag=1 break str=bin(n-m*i).replace("0b","") for j in str: if(j=="1"): count+=1 if(count<=i): break i+=1 if(flag==1): print(-1) else: print(i) ``` No
87,668
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` # BHUPAT JANGID (codechef id : bhupat2000) (codeforces id : bhupat2000) # linkedin id : https://www.linkedin.com/in/bhupat-jangid-1b7b53170 import sys from heapq import heapify, heappop, heappush from itertools import * from collections import * from math import * #import collection => Counter deque OrderedDict defaultdict sys.setrecursionlimit(10 ** 6) # f = open('input.txt') # f.close() input = lambda: sys.stdin.readline() # f.readline() inp = lambda: int(input()) nm = lambda: map(int, input().split()) arr = lambda: list(nm()) INF = int(1e18) mod = int(1e9) + 7 # 998244353 def find(dp,n): while dp[n]>0: n=dp[n] return n def solve(): #d1=deque() #d=defaultdict(list) n,m=nm() lst1=arr() lst2=arr() dp1=[0]*(n) dp2=[0]*n for i in range(n-1): if i==0: dp1[i+1]=lst1[i] dp2[i+1]=lst2[i]+m else: dp1[i+1]=lst1[i]+min(dp1[i],dp2[i]) dp2[i+1]=lst2[i]+min(dp1[i]+m,dp2[i]) for i in range(n): print(min(dp1[i],dp2[i]),end=" ") t = 1#inp() for i in range(t): solve() ```
87,669
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` n,c=map(int,input().split()) x=list(map(int,input().split())) y=list(map(int,input().split())) dp=[[0]*2for i in range(n+1)] print(0,end=" ") for i in range(n-1): if i>0: dp[i][0]=min(dp[i-1][0]+y[i],dp[i-1][1]+y[i]+c) dp[i][1]=min(dp[i-1][0],dp[i-1][1])+x[i] else: dp[0][0]=y[0]+c dp[0][1]=x[0] print(min(dp[i][0],dp[i][1]),end=" ") ```
87,670
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` #hint: https://codeforces.com/blog/entry/70779 inp = lambda : map(int, input().split()) n, c = inp() a = list(inp()) b = list(inp()) ans = list() ans.append(0) arr = [[1000000000,10000000000] for i in range(n)] arr[0][0] = 0 arr[0][1] = c for i in range(n-1): arr[i+1][0] = min(arr[i+1][0], arr[i][0] + a[i]) arr[i+1][0] = min(arr[i+1][0], arr[i][1] + a[i]) arr[i+1][1] = min(arr[i+1][1], arr[i][1] + b[i]) arr[i+1][1] = min(arr[i+1][1], arr[i][0] + b[i] + c) ans = [] for i in range(n): ans.append(min(arr[i][0], arr[i][1])) print(*ans) ```
87,671
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` n,c=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) dp=[[0,c] for i in range(n)] for i in range(1,n): dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i-1] dp[i][1]=min(dp[i-1][0]+c+b[i-1],dp[i-1][1]+b[i-1]) ans=[] for i in range(n): ans.append(min(dp[i][0],dp[i][1])) print (*ans) ```
87,672
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` """ NTC here """ from sys import stdin, setrecursionlimit setrecursionlimit(10**7) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) # range = xrange # input = raw_input def main(): n,c=lin() s=lin() e=lin() sol=[[0,0] for i in range(n)] sol[0]=[c+e[0],s[0]] for i in range(1,n-1): sol[i]=[e[i]+min(sol[i-1][0], c+sol[i-1][1]),min(sol[i-1][0],sol[i-1][1])+s[i]] ans=[0]+[min(sol[i]) for i in range(n-1)] print(*ans) main() # try: # main() # except Exception as e: print(e) ```
87,673
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` import sys input = lambda :sys.stdin.readline().rstrip('\r\n') from math import log,ceil from collections import defaultdict n,c = map(int,input().split()) # 0 for currently in stairs # 1 for in the elevator dp = [[float('inf'),float('inf')] for _ in range(n)] dp[0][0] = 0 dp[0][1] = c a = [0]+[int(x) for x in input().split()] b = [0]+[int(x) for x in input().split()] for i in range(1,n): dp[i][0] = min(dp[i][0],dp[i-1][0]+a[i]) dp[i][0] = min(dp[i][0],dp[i-1][1]+a[i]) dp[i][1] = min(dp[i][1],dp[i-1][1]+b[i]) dp[i][1] = min(dp[i][1],dp[i-1][0]+b[i]+c) # print(dp[i]) print(*[min(x[0],x[1]) for x in dp]) ```
87,674
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` n,c=list(map(int,input().split())) a=list(map(int,input().split())) b=list(map(int,input().split())) ans=[0] INF=1e18 dp=[[INF,INF] for _ in range(n)] dp[1][0]=a[0] dp[1][1]=b[0]+c ans.append(min(dp[1])) for i in range(1,n-1): temp=0 dp[i+1][0]=min(dp[i+1][0],dp[i][0]+a[i]) dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + a[i]) dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + b[i]+c) dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + b[i] ) ans.append(min(dp[i+1])) print(*ans) ```
87,675
Provide tags and a correct Python 3 solution for this coding contest problem. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Tags: dp, shortest paths Correct Solution: ``` R = lambda:list(map(int,input().split())) n, c = R() stair = R() elevator = R() ans, dp_of_stair, dp_of_elevator = [], [0], [c] for i in range(n-1): dp_of_stair.append(min(dp_of_stair[-1] + stair[i], dp_of_elevator[-1]+ stair[i])) dp_of_elevator.append(min(dp_of_stair[-2] + elevator[i] + c, dp_of_elevator[-1]+ elevator[i])) for i in range(n): ans.append(min(dp_of_stair[i], dp_of_elevator[i])) print(' '.join([str(x) for x in ans])) ```
87,676
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` n, c = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) stair = 0 lift = c print(0) for i in range(n-1): stair = min(stair+a[i], lift+b[i]) lift = min(stair+c, lift+b[i]) print(stair) ``` Yes
87,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` n, c = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [[0]*2 for i in range(n)] dp[0][0] = 0 dp[0][1] = c for i in range(n-1): dp[i+1][0] = min(dp[i][0] + a[i], dp[i][1] + a[i]) dp[i+1][1] = min(dp[i][0] + b[i] + c, dp[i][1] + b[i]) ans = [0]*n for i in range(n): print(min(dp[i][0],dp[i][1]),end=" ") ``` Yes
87,678
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` from sys import stdin,stdout for _ in range(1):#int(stdin.readline())): # n=int(stdin.readline()) n,c=list(map(int,stdin.readline().split())) a=list(map(int,stdin.readline().split())) b=list(map(int,stdin.readline().split())) stairs,lift=0,c for i in range(n-1): print(min(stairs,lift),end=' ') stairs,lift=min(stairs+a[i],lift+b[i]),min(lift+b[i],stairs+c+a[i]) print(min(stairs, lift), end=' ') ``` Yes
87,679
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` import sys input = sys.stdin.readline def main(): N, C = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] dp = [0] * 2 ans = 0 print(ans, end=" ") is_use_elev = False dp[0] = A[0] dp[1] = B[0] + C print(min(dp[0], dp[1]), end=" ") for i in range(1, N - 1): x = min(dp[0], dp[1]) + A[i] y = min(dp[0] + C, dp[1]) + B[i] print(min(x, y), end=" ") dp[0] = x dp[1] = y if __name__ == '__main__': main() ``` Yes
87,680
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` n,k = map(int,input().split()) a = list(map(int,input().split())) b = list(map(int,input().split())) c = [0 for i in range(n)] flag = [0 for i in range(n)] for i in range(0,n-1): if flag == 1: if a[i] < b[i]: c[i+1] = a[i] + c[i] flag[i+1] = 0 else: c[i+1] = b[i] + c[i] flag[i+1] = 1 else: if a[i] < b[i]+k: c[i+1] = a[i] + c[i] flag[i+1] = 0 else: c[i+1] = b[i] + k + c[i] flag[i+1] = 1 if flag[i] == 0 and i > 0: if b[i-1] + b[i] + c[i-1] + k < c[i+1]: c[i+1] = b[i-1] + b[i] + c[i-1] + k flag[i] = 1 for i in c: print(i,end = " ") ``` No
87,681
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` a, b = list(map(int, input().split())) stairs = list(map(int, input().split())) lift = list(map(int, input().split())) dp = [[0, 0] for i in range(a)] dp[0] = [0, 0] t = 0 v = 0 for i in range(1, a): if dp[i - 1][1] == 0: if dp[t][0] + v + lift[i - 1] < stairs[i - 1] + dp[i - 1][0] and dp[t][0] + v + lift[i - 1] < lift[i - 1] + dp[i - 1][0] + b and t != 0: dp[i][0] = dp[t][0] + v + lift[i - 1] dp[i][1] = 1 v = 0 elif lift[i - 1] + b > stairs[i - 1]: dp[i][1] = 0 v += lift[i - 1] dp[i][0] = dp[i - 1][0] + stairs[i - 1] else: dp[i][1] = 1 t = i - 1 dp[i][0] = dp[i - 1][0] + min(lift[i - 1] + b, stairs[i - 1]) else: k1 = min((lift[i - 1]), stairs[i - 1]) if lift[i - 1] > stairs[i - 1]: dp[i][1] = 0 v += lift[i - 1] else: dp[i][1] = 1 t = i dp[i][0] = dp[i - 1][0] + min(lift[i - 1], stairs[i - 1]) for i in range(len(dp)): print(dp[i][0], end = ' ') ``` No
87,682
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` n, c = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] time = [0] * n level = 1 fromElevator = False while level < n: e = c if fromElevator: e = 0 stairs = time[level-1] + a[level - 1] elevator = time[level-1] + b[level - 1] + e if elevator <= stairs: time[level] = elevator fromElevator = True else: time[level] = stairs fromElevator = False level += 1 print(' '.join([str(x) for x in time])) ``` No
87,683
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs; * b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at x to any floor y (x β‰  y) in two different ways: * If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take βˆ‘_{i=min(x, y)}^{max(x, y) - 1} a_i time units. * If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + βˆ‘_{i=min(x, y)}^{max(x, y) - 1} b_i time units. You can perform as many moves as you want (possibly zero). So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor. Input The first line of the input contains two integers n and c (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ c ≀ 1000) β€” the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 ≀ a_i ≀ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs. The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 ≀ b_i ≀ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator. Output Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want. Examples Input 10 2 7 6 18 6 16 18 1 17 17 6 9 3 10 9 1 10 1 5 Output 0 7 13 18 24 35 36 37 40 45 Input 10 1 3 2 3 1 3 3 1 4 1 1 2 3 4 4 1 2 1 3 Output 0 2 4 7 8 11 13 14 16 17 Submitted Solution: ``` n, c = list(map(int, input().split())) n = n - 1 a, b = [0] * n, [0] * n a = list(map(int, input().split())) b = list(map(int, input().split())) el, tm = False, 0 print(0, end=" ") for x in range(n): if el == False: if a[x] >= b[x] + c: tm += b[x] + c el = True else: tm += a[x] else: if a[x] >= b[x]: tm += b[x] else: tm += a[x] el = False print(tm, end=" ") ``` No
87,684
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` input() a=[*map(int, input().split())] b=sum(e//2 for e in a[::2])+sum((e+1)//2 for e in a[1::2]) print(min(sum(a)-b,b)) ```
87,685
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys # import threading from math import inf, log2 from collections import defaultdict # threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase # sys.setrecursionlimit(300000) 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") #---------------------------------------------------------------------------------------------------------------- class LazySegmentTree: def __init__(self, array, func=max): self.n = len(array) self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1 self.func = func self.default = 0 if self.func != min else inf self.data = [self.default] * (2 * self.size) self.lazy = [0] * (2 * self.size) self.process(array) def process(self, array): self.data[self.size : self.size+self.n] = array for i in range(self.size-1, -1, -1): self.data[i] = self.func(self.data[2*i], self.data[2*i+1]) def push(self, index): """Push the information of the root to it's children!""" self.lazy[2*index] += self.lazy[index] self.lazy[2*index+1] += self.lazy[index] self.data[2 * index] += self.lazy[index] self.data[2 * index + 1] += self.lazy[index] self.lazy[index] = 0 def build(self, index): """Build data with the new changes!""" index >>= 1 while index: self.data[index] = self.func(self.data[2*index], self.data[2*index+1]) + self.lazy[index] index >>= 1 def query(self, alpha, omega): """Returns the result of function over the range (inclusive)!""" res = self.default alpha += self.size omega += self.size + 1 for i in range(len(bin(alpha)[2:])-1, 0, -1): self.push(alpha >> i) for i in range(len(bin(omega-1)[2:])-1, 0, -1): self.push((omega-1) >> i) while alpha < omega: if alpha & 1: res = self.func(res, self.data[alpha]) alpha += 1 if omega & 1: omega -= 1 res = self.func(res, self.data[omega]) alpha >>= 1 omega >>= 1 return res def update(self, alpha, omega, value): """Increases all elements in the range (inclusive) by given value!""" alpha += self.size omega += self.size + 1 l, r = alpha, omega while alpha < omega: if alpha & 1: self.data[alpha] += value self.lazy[alpha] += value alpha += 1 if omega & 1: omega -= 1 self.data[omega] += value self.lazy[omega] += value alpha >>= 1 omega >>= 1 self.build(l) self.build(r-1) #--------------------------------------------------------------------------------------------- n=int(input()) l=list(map(int,input().split())) b=0 w=0 for i in range(n): if i%2==0: b+=int(math.ceil(l[i]/2)) w+=l[i]//2 else: w += int(math.ceil(l[i]/2)) b += l[i] // 2 print(min(b,w)) ```
87,686
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` # 20200118 19:26 ~ 19:47 ~ 20:20 n = int(input()) arr = list(map(int, input().split())) ans_1 = 0 ans_2 = 0 for i in range(n): if i%2 == 1: ans_1+=int(arr[i]/2) + arr[i]%2 ans_2+=int(arr[i]/2) else: ans_1+=int(arr[i]/2) ans_2+=int(arr[i]/2) + arr[i]%2 print(min(ans_1, ans_2)) ```
87,687
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` n=int(input()) it=list(map(int,input().split())) a=0 b=0 for i in range(n): if i%2==0: a+=it[i]//2 b+=it[i]//2 a+=it[i]%2 else: b+=it[i]//2 a+=it[i]//2 b+=it[i]%2 print(min(a,b)) ```
87,688
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) ch = 0 b = 0 for i in range(n): if l[i] % 2 == 0: ch += l[i] // 2 b += l[i] // 2 else: b += l[i] // 2 ch += l[i] // 2 if i % 2 == 0: b += 1 else: ch += 1 print(min(ch, b)) ```
87,689
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) b,c=0,0 for i in range(n): if i%2==1: b+=a[i]//2 c+=a[i]//2+a[i]%2 else: c+=a[i]//2 b+=a[i]//2+a[i]%2 print(min(b,c)) ```
87,690
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` import sys n = int(sys.stdin.readline().strip()) a = list(map(int,sys.stdin.readline().strip().split())) d = 0 s = 0 for i in range (0, n): s = s + a[i] if i % 2 == 0: d = d + a[i] % 2 else: d = d - a[i] % 2 print((s - abs(d)) // 2) ```
87,691
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Tags: dp, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) counts = [0]*2 for i in range(n): counts[i&1] += a[i]//2 #use i&1 so it alternates since coloring as checkerboard counts[i&1^1] += (a[i]+1)//2 print(min(counts)) ```
87,692
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` import sys input=sys.stdin.readline lastodd=[] n=int(input()) a=[int(i) for i in input().split()] ans=0 for i in range(n): if not a[i]&1: ans+=a[i]//2 else: if lastodd: if (i-lastodd[-1])&1: ans+=1 lastodd.pop() else: lastodd.append(i) else: lastodd.append(i) ans+=a[i]//2 print(ans) ``` Yes
87,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) w=0 b=0 for i in range(n): if i%2==0: if a[i]%2==1: b+=1 b+=a[i]//2 w+=a[i]//2 else: if a[i]%2==1: w+=1 b+=a[i]//2 w+=a[i]//2 print(min(w,b)) ``` Yes
87,694
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` import math N = int(input()) arr = [int(x) for x in input().split()] tot = 0 hor = 0 for i in range(N): if arr[i] >= hor or (hor%2 == arr[i]%2): tot += int(math.ceil(min(hor, arr[i])/2)) else: tot += int(math.ceil((min(hor, arr[i])-1)/2)) if arr[i] >= hor: tot += (arr[i]-hor)//2 if (arr[i] - hor) % 2 == 0: hor = max(hor-1, 0) else: hor += 1 else: hor -= 1 hor2 = arr[i] if hor2%2 != hor%2: hor2 -= 1 hor = max(hor2, 0) #print(tot, hor) print(tot) ``` Yes
87,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` def main(): n=int(input()) a=readIntArr() whites=0 blacks=0 for i,x in enumerate(a): y=x//2 z=x-y if i%2==0: whites+=y blacks+=z else: whites+=z blacks+=y print(min(whites,blacks)) return import sys # input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m]) dv=defaultVal;da=dimensionArr if len(da)==1:return [dv for _ in range(da[0])] else:return [makeArr(dv,da[1:]) for _ in range(da[0])] def queryInteractive(x,y): print('? {} {}'.format(x,y)) sys.stdout.flush() return int(input()) def queryInteractive2(arr): print('? '+' '.join([str(x) for x in arr])) sys.stdout.flush() return [int(x) for x in input().split()] def answerInteractive(ans): print('! {}'.format(ans)) sys.stdout.flush() inf=float('inf') MOD=10**9+7 for _abc in range(1): main() ``` Yes
87,696
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` def dominos(n, list1): count=0 prev=False for x in list1: count+= x//2 if(x%2==1): if(prev): count+=1 else: prev=True else: prev=False return count n= input() list1= input().split() list1 = [ int(x) for x in list1] print(dominos(n,list1)) ``` No
87,697
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` n=int(input()) ar=list(map(int,input().split())) res=[i//2 for i in ar] print(sum(res)) ``` No
87,698
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 β‰₯ a_2 β‰₯ … β‰₯ a_n β‰₯ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 Γ— 2 or 2 Γ— 1 rectangle. Input The first line of input contain one integer n (1 ≀ n ≀ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 300 000, a_i β‰₯ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image> Submitted Solution: ``` a=int(input()) y=list(map(int,input().split())) y.sort() t=0 while len(y)!=0: u=y[0] t+=len(y)//2*y[0] for i in range(y.count(y[0])): y.remove(y[0]) for k in range(len(y)): y[k]-=u print(t) ``` No
87,699