message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,645
12
41,290
Tags: brute force, sortings Correct Solution: ``` n, k = map(int, input().split()) a = list(map(int, input().split())) res = a[0] for l in range(n): for r in range(l, n): inside = sorted(a[l:r+1]) outside = sorted(a[:l] + a[r+1:], reverse=True) new_res = sum(inside) for i in range(min(k, len(inside), len(outside))): if outside[i] > inside[i]: new_res += outside[i]-inside[i] else: break if new_res > res: res = new_res print(res) ```
output
1
20,645
12
41,291
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,646
12
41,292
Tags: brute force, sortings Correct Solution: ``` n, k = map(int, input().split()) a = list(map(int, input().split())) def solve(i, j): cur_res = sum(a[i:j+1]) a1 = sorted(a[i:j+1]) a2 = sorted(a[:i] + a[j+1:], reverse=True) for t in range(min(k, len(a1), len(a2))): m = min(a1) if a2[t] > m: cur_res += a2[t] - m a1[a1.index(m)] = a2[t] return cur_res print(max(solve(i, j) for i in range(n) for j in range(i, n))) ```
output
1
20,646
12
41,293
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,647
12
41,294
Tags: brute force, sortings Correct Solution: ``` n, k = [int(c) for c in input().split()] a = [int(c) for c in input().split()] best = -1000001 seq = [] other = [] for l in range(n): for r in range(l + 1, n + 1): seq = sorted(a[l:r]) other = a[:l] + a[r:] other.sort() other.reverse() seq_sum = sum(seq) best = max(best, seq_sum) for sw in range(0, min(k, len(seq), len(other))): seq_sum = seq_sum - seq[sw] + other[sw] best = max(best, seq_sum) print(best) # 200 10 # -933 947 859 -503 947 -767 121 469 214 -381 -962 807 59 -702 -873 -747 -233 77 -853 -39 243 902 909 612 -248 238 -511 -897 933 536 732 322 -155 247 340 145 681 -469 -906 -768 -368 -356 -168 -466 -398 -528 -515 968 107 929 178 29 -938 766 -173 -544 128 905 -877 -134 469 214 788 530 984 -738 805 -317 619 -596 -170 799 -276 -53 -211 663 619 -951 -616 -117 -574 774 127 -532 69 210 901 668 517 -354 280 -746 369 -357 696 570 -918 -912 -23 405 -414 -962 504 -390 165 -767 -259 442 -523 38 910 -956 62 -665 -933 947 859 -503 947 -767 121 469 214 -381 -962 807 59 -702 -873 -747 -233 77 -853 -39 243 902 909 612 -248 238 -511 -897 933 536 732 322 -155 247 340 145 681 -469 -906 -768 -368 -356 -168 -466 -398 -528 -515 968 107 929 178 29 -938 766 -173 -544 128 905 -877 -134 469 214 788 530 984 -738 805 -317 619 -596 -170 799 -276 -53 -211 663 619 -951 -616 -117 -574 774 127 -933 947 859 -503 947 -767 121 469 214 -381 -962 807 59 -702 -873 -747 -233 77 -853 -39 243 902 909 612 -248 238 -511 -897 933 536 732 322 -155 247 340 145 681 -469 -906 -768 -368 -356 -168 -466 -398 -528 -515 968 107 929 178 29 -938 766 -173 -544 128 905 -877 -134 469 214 788 530 984 -738 805 -317 619 -596 -170 799 -276 -53 -211 663 619 -951 -616 -117 -574 774 127 -532 69 210 901 668 517 -354 280 -746 369 -357 696 570 -918 -912 -23 405 -414 -962 504 -390 165 -767 -259 442 -523 38 910 -956 62 -665 -933 947 859 -503 947 -767 121 469 214 -381 -962 807 59 -702 -873 -747 -233 77 -853 -39 243 902 909 612 -248 238 -511 -897 933 536 732 322 -155 247 340 145 681 -469 -906 -768 -368 -356 -168 -466 -398 -528 -515 968 107 929 178 29 -938 766 -173 -544 128 905 -877 -134 469 214 788 530 984 -738 805 -317 619 -596 -170 799 -276 -53 -211 663 619 -951 -616 -117 -574 774 127 ```
output
1
20,647
12
41,295
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,648
12
41,296
Tags: brute force, sortings Correct Solution: ``` R = lambda:map(int, input().split()) n, k = R() a = list(R()) def f(l, r): x = sorted(a[:l] + a[r + 1:], reverse=True) y = sorted(a[l:r + 1]) return sum(y + [max(0, x[i] - y[i]) for i in range(min(k, len(x), len(y)))]) print(max(f(l, r) for l in range(n) for r in range(l, n))) ```
output
1
20,648
12
41,297
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,649
12
41,298
Tags: brute force, sortings Correct Solution: ``` n,m=map(int,input().split()) lis=list(map(int,input().split())) k=-100000000 for l in range(n): for r in range(l+1,n+1): k=max(k,sum(sorted(lis[l:r] + sorted(lis[:l]+lis[r:])[-m:])[l-r:])) print(k) ```
output
1
20,649
12
41,299
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,650
12
41,300
Tags: brute force, sortings Correct Solution: ``` __author__ = 'Lipen' def main(): n, k = map(int, input().split()) a = list(map(int, input().split())) s = a[0] for l in range(n): for r in range(l,n): out = sorted(a[:l] + a[r+1:], reverse=True) inside = sorted(a[l:r+1]) temp = sum(a[l:r+1]) for i in range(min(k, len(out), len(inside))): if out[i] > inside[i]: temp += out[i] - inside[i] else: break if temp > s: s = temp print(s) main() ```
output
1
20,650
12
41,301
Provide tags and a correct Python 3 solution for this coding contest problem. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
instruction
0
20,651
12
41,302
Tags: brute force, sortings Correct Solution: ``` read_line = lambda: [int(i) for i in input().split()] n, k = read_line() x = read_line() print(max(sum(sorted(x[l:r] + sorted(x[:l] + x[r:])[-k:])[l-r:]) for l in range(n) for r in range(l + 1, n + 1))) # Made By Mostafa_Khaled ```
output
1
20,651
12
41,303
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1 Submitted Solution: ``` #!/usr/local/bin/python3 n, k = map(int, input().split()) a = list(map(int, input().split())) r_sum = a[0] for l in range(n): for r in range(l, n): inside = sorted(a[l:r+1]) outside = sorted(a[:l] + a[r+1:], reverse=True) t_sum = sum(inside) for i in range(min(k, len(inside), len(outside))): if outside[i] > inside[i]: t_sum += (outside[i] - inside[i]) else: break if t_sum > r_sum: r_sum = t_sum print(r_sum) ```
instruction
0
20,652
12
41,304
Yes
output
1
20,652
12
41,305
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1 Submitted Solution: ``` n, k = [int(c) for c in input().split()] a = [int(c) for c in input().split()] best = -1000001 for l in range(n): for r in range(l - 1, n): seq = sorted(a[l:r]) other = sorted(a[:l] + a[r:]) for sw in range(-1, min(k, len(seq))): tmp = sum(seq[sw:]) + sum(other[0 - sw:]) best = max(best, tmp) print(best) ```
instruction
0
20,653
12
41,306
No
output
1
20,653
12
41,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1 Submitted Solution: ``` __author__ = 'Lipen' def main(): n, k = map(int, input().split()) a = list(map(int, input().split())) si = -1 ei = -1 for i in range(n): if a[i]>=0: si = i break for i in range(n-1, -1, -1): if a[i]>=0: ei = i break if si==-1: print(max(a)) return u = 0 while u<k: e = min(a[si:ei+1]) if e < 0: w = 0 for yy in range(n): if a[yy]==e and yy>=si: w = yy break a[w], a[si] = a[si], a[w] u+=1 tempsi = si for i in range(tempsi, ei+1): if a[i]>=0: si = i break tempei = ei for i in range(tempei, si-1, -1): if a[i]>=0: ei = i break else: break m = -300000 b = True for i in range(si, ei+1): for j in range(i, ei+1): temp = sum(a[i:j+1]) if b: m = temp b = False elif temp>m: m = temp print(m) main() ```
instruction
0
20,654
12
41,308
No
output
1
20,654
12
41,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1 Submitted Solution: ``` n, k = map(int, input().split()) a = list(map(int, input().split())) s = a[0] sm = l = r = 0 p = -1 for i in range(n): sm += a[i] if sm > s: s = sm l = p + 1 r = i if sm < 0: sm = 0 p = i x = sorted(a[:l] + a[r + 1:], reverse=True) y = sorted(a[l:r + 1]) for i in range(min(k, len(x), len(y))): if x[i] - y[i] > 0: s += x[i] - y[i] x[i], y[i] = y[i], x[i] else: break if i < k: for j in range(0, min(k - i, len(y))): if y[j] < 0: s -= y[j] else: break print(s) ```
instruction
0
20,655
12
41,310
No
output
1
20,655
12
41,311
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i β‰  j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? Input The first line contains two integers n and k (1 ≀ n ≀ 200; 1 ≀ k ≀ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≀ a[i] ≀ 1000). Output In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. Examples Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1 Submitted Solution: ``` __author__ = 'Lipen' def main(): n, k = map(int, input().split()) a = list(map(int, input().split())) si = -1 ei = -1 for i in range(n): if a[i]>=0: si = i break for i in range(n-1, -1, -1): if a[i]>=0: ei = i break if si==-1: print(max(a)) return c = 0 negatedata = [] for i in range(si, ei+1): if a[i]<0: c+=1 negatedata.append([a[i], i]) negatedata = sorted(negatedata, key=lambda x:x[0]) for z in range(min(c, k)): v = negatedata[z][1] a[si], a[v] = a[v], a[si] si+=1 print(sum(a[si:ei+1])) main() ```
instruction
0
20,656
12
41,312
No
output
1
20,656
12
41,313
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,261
12
42,522
Tags: sortings Correct Solution: ``` import sys,math,string input=sys.stdin.readline from collections import deque L=lambda : list(map(int,input().split())) Ls=lambda : list(input().split()) M=lambda : map(int,input().split()) n=int(input()) a=L() od=0 ev=0 for i in range(n): if(a[i]%2==0): ev+=1 else: od+=1 if(od==0 or ev==0): print(*a) else: a.sort() print(*a) ```
output
1
21,261
12
42,523
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,262
12
42,524
Tags: sortings Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) od = 0 for el in l: od += (el&1) if od > 0 and (n-od) != 0: l.sort() print(*l) ```
output
1
21,262
12
42,525
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,263
12
42,526
Tags: sortings Correct Solution: ``` size = int(input()) a = list(map(int, input().split())) f1 = f2 = False for i in a: if i % 2 != 0: f1 = True else: f2 = True if f1 and f2: print(*sorted(a)) else: print(*a) ```
output
1
21,263
12
42,527
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,264
12
42,528
Tags: sortings Correct Solution: ``` n = int(input()) a1 = list(map(int, input().split())) a, b = 0, 0 for i in a1: if i % 2 == 0: a += 1 else: b+=1 if a != 0 and b != 0: a1.sort() print(*a1) else: print(*a1) ```
output
1
21,264
12
42,529
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,265
12
42,530
Tags: sortings Correct Solution: ``` if __name__ == '__main__': n = int(input()) aa = list(map(int, input().split())) odd, even = False, False for a in aa: if a%2==0: odd = True else: even = True if odd and even: break if odd and even: aa.sort() print(" ".join(map(str, aa))) ```
output
1
21,265
12
42,531
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,266
12
42,532
Tags: sortings Correct Solution: ``` from math import * n = int(input()) l = list(map(int,input().split())) a = [0,0] for i in l: a[i%2] += 1 if(a[0] == 0 or a[1] == 0): for i in l: print(i,end = " ") print() else: l.sort() for i in l: print(i,end = " ") print() ```
output
1
21,266
12
42,533
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,267
12
42,534
Tags: sortings Correct Solution: ``` n = int(input()) l = [int(x) for x in input().split(" ")] odd,even = 0,0 for i in l: if i%2: odd += 1 else: even += 1 if odd and even: l.sort() print(*l) ```
output
1
21,267
12
42,535
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd.
instruction
0
21,268
12
42,536
Tags: sortings Correct Solution: ``` n = int(input()) ls = list(map(int,input().split())) b = [0,0] for i in range(n): b[ls[i]%2]=1 if b[0] and b[1]: ls.sort() for j in range(n): print (ls[j],end = ' ') ```
output
1
21,268
12
42,537
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` def solve(arr): val = arr[0]%2 for i in arr: if i%2!=val: arr.sort() return arr return arr def main(): t= int(input().strip()) lineinp=input().strip() arr= list(map(int,lineinp.split())) gp=solve(arr) for i in gp: print("{} ".format(i),end="") main() ```
instruction
0
21,269
12
42,538
Yes
output
1
21,269
12
42,539
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` from sys import stdin,stdout from math import gcd,sqrt from collections import deque input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\n') P=lambda x:stdout.write(x) hg=lambda x,y:((y+x-1)//x)*x pw=lambda x:1 if x==1 else 1+pw(x//2) chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False N=10**9+7 n=I() a=list(R()) x=[i%2 for i in a] if 1 in x and 0 in x:print(*sorted(a)) else:print(*a) ```
instruction
0
21,270
12
42,540
Yes
output
1
21,270
12
42,541
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) if len([x for x in a if x % 2 ==0]) in (0, n): print(*[x for x in a]) else: print(*[x for x in sorted(a)]) ```
instruction
0
21,271
12
42,542
Yes
output
1
21,271
12
42,543
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` def printf(a): for num in a: print(num,end=' ') n=int(input()) a=list(map(int,input().split())) count=0 for num in a: if num%2==0: count+=1 if count!=0 and count!=n: a.sort() printf(a) ```
instruction
0
21,272
12
42,544
Yes
output
1
21,272
12
42,545
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` def solve(n, nums): nums.sort() return ' '.join(map(str, nums)) if __name__ == '__main__': n = int(input()) nums = list(map(int, input().split())) print(solve(n, nums)) ```
instruction
0
21,273
12
42,546
No
output
1
21,273
12
42,547
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` n = int(input()) A = list(map(int,input().split())) o = 0 for i in A: if(i&1): o+=1 if(o==n and o%2==0) or o==0: print(*A) else: A.sort() print(*A) ```
instruction
0
21,274
12
42,548
No
output
1
21,274
12
42,549
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` n = int(input()) l = list(map(int,input().split())) l.sort() print(*l) ```
instruction
0
21,275
12
42,550
No
output
1
21,275
12
42,551
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operation on it as many times as you want: * Pick two integers i and j (1 ≀ i,j ≀ n) such that a_i+a_j is odd, then swap a_i and a_j. What is lexicographically the smallest array you can obtain? An array x is [lexicographically smaller](https://en.wikipedia.org/wiki/Lexicographical_order) than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 ≀ j < i. Less formally, at the first index i in which they differ, x_i<y_i Input The first line contains an integer n (1 ≀ n ≀ 10^5) β€” the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, …, a_{n} (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output The only line contains n space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap 1 and 4 since 1+4=5, which is odd. Submitted Solution: ``` import sys input = lambda: sys.stdin.readline().strip("\r\n") n = int(input()) a = sorted(list(map(int, input().split()))) print(*a) ```
instruction
0
21,276
12
42,552
No
output
1
21,276
12
42,553
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,347
12
42,694
Tags: greedy, implementation, sortings Correct Solution: ``` n = int(input()) array = list(map(int, input().split(" "))) if (max(array) == 1): del array[0] array.append(2) else: array.remove(max(array)) array.insert(0, 1) array.sort() for i in range (n): array[i] = str(array[i]) print(" ".join(array)) ```
output
1
21,347
12
42,695
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,348
12
42,696
Tags: greedy, implementation, sortings Correct Solution: ``` import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush # from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction import copy import time starttime = time.time() mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") n=L()[0] A=sorted(L()) if A==[1]*n: print(*A[:n-1],2) else: print(1,*A[:-1]) endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}") ```
output
1
21,348
12
42,697
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,349
12
42,698
Tags: greedy, implementation, sortings Correct Solution: ``` n=int(input()) l=[int(i) for i in input().split()] ind=l.index(max(l)) if l[ind]==1: l[ind]=2 else: l[ind]=1 l.sort() print(*l) ```
output
1
21,349
12
42,699
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,350
12
42,700
Tags: greedy, implementation, sortings Correct Solution: ``` import sys import math n=int(input()) lista=[int(x) for x in input().strip().split()] pap=lista[:] pap.sort() if(pap[-1]==1): pap[-1]=2 else: pap=[1]+pap[:-1] for i in range(n): print(pap[i], end=" ") ```
output
1
21,350
12
42,701
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,351
12
42,702
Tags: greedy, implementation, sortings Correct Solution: ``` a=int(input()) z=list(map(int,input().split())) z.sort() if(z.count(1)==len(z)): z[-1]=2 print(*z) exit() ans=[0 for i in range(len(z))] ans[0]=1 for i in range(1,len(z)): ans[i]=z[i-1] print(*ans) #1 1 1 1 1 ```
output
1
21,351
12
42,703
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,352
12
42,704
Tags: greedy, implementation, sortings Correct Solution: ``` import sys from math import log2,floor,ceil,sqrt,gcd import bisect # from collections import deque # sys.setrecursionlimit(10**5) Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 1000000007 n = int(ri()) a = Ri() a.sort() ans = [] # ans.append(1) i = 0 flag = False for i in range(0,len(a)): if a[i] == 1: if i == len(a)-1: ans.append(2) else: ans.append(1) else: flag = True break if flag : for i in range(i,len(a)): if i == 0: ans.append(1) continue if a[i] != a[i-1]: ans.append(a[i-1]) else: ans.append(a[i]) print(*ans) else: print(*ans) ```
output
1
21,352
12
42,705
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,353
12
42,706
Tags: greedy, implementation, sortings Correct Solution: ``` input() a=list(map(int,input().split())) t=max(a) a[a.index(t)]=[1,2][not t-1] print(' '.join(map(str,sorted(a)))) ```
output
1
21,353
12
42,707
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2
instruction
0
21,354
12
42,708
Tags: greedy, implementation, sortings Correct Solution: ``` n = int(input()) num = list(map(int, input().split())) mx = max(num) for i in range(n): if num[i] == mx: if mx != 1: num[i] = 1 else: num[i] = 2 break num.sort() for i in range(n): print(num[i], end=" ") ```
output
1
21,354
12
42,709
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` #!/usr/bin/env python #pyrival orz import os import sys from io import BytesIO, IOBase """ for _ in range(int(input())): n,m=map(int,input().split()) n=int(input()) a = [int(x) for x in input().split()] """ def main(): n=int(input()) a = [int(x) for x in input().split()] a.sort() if a[-1]==1: a[-1]=2 else: a[-1]=1 a.sort() print(*a) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
21,355
12
42,710
Yes
output
1
21,355
12
42,711
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` import math n=int(input()) lst = list(map(int, input().strip().split(' '))) #n,r = map(int, input().strip().split(' ')) p=max(lst) ind=lst.index(p) if p==1: lst[ind]=2 else: lst[ind]=1 lst.sort() for j in range(n): print(lst[j],end=" ") ```
instruction
0
21,356
12
42,712
Yes
output
1
21,356
12
42,713
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` input() a=sorted(list(map(int,input().split()))) a[-1]=[1,2][a[-1]==1] print(*(sorted(a))) ```
instruction
0
21,357
12
42,714
Yes
output
1
21,357
12
42,715
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` n=input() a=list(map(int,input().split())) if a.count(1)==len(a): print(*a[:len(a)-1],2) else: maxim=0 pos=0 for i in range(len(a)): if a[i]>maxim: pos=i maxim=a[i] a[pos]=1 a.sort() print(*a) ```
instruction
0
21,358
12
42,716
Yes
output
1
21,358
12
42,717
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) if a[-1]==1: a[-1]=2 else: a=[1]+a[1:] print(*a) ```
instruction
0
21,359
12
42,718
No
output
1
21,359
12
42,719
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` a=int(input()) z=list(map(int,input().split())) z.sort() ans=[0 for i in range(len(z))] ans[0]=1 for i in range(1,len(z)): ans[i]=z[i-1] print(*ans) ```
instruction
0
21,360
12
42,720
No
output
1
21,360
12
42,721
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` x=int(input()) arr=list(map(int,input().split())) if x==1: print(1) else: arr.sort() print(1,end=" ") print(*(arr[0:len(arr)-2]),end=" ") if arr[len(arr)-2]==1: print(2) else: print(arr[len(arr)-2]) ```
instruction
0
21,361
12
42,722
No
output
1
21,361
12
42,723
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting. Input The first line contains a single integer n (1 ≀ n ≀ 105), which represents how many numbers the array has. The next line contains n space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive. Output Print n space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed. Examples Input 5 1 2 3 4 5 Output 1 1 2 3 4 Input 5 2 3 4 5 6 Output 1 2 3 4 5 Input 3 2 2 2 Output 1 2 2 Submitted Solution: ``` import sys input = lambda:sys.stdin.readline() MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: list(map(int, input().split())) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) n=ii() l=il() l2=sorted(l) for i in range(n): if l[i]==l2[i]: l2[i]=1 break l2.sort() print(*l2) ```
instruction
0
21,362
12
42,724
No
output
1
21,362
12
42,725
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,405
12
42,810
Tags: constructive algorithms, dp, greedy Correct Solution: ``` import io import os from collections import Counter, defaultdict, deque def solve(N, A): # Decreasing can only be inverted by having left moves in between # Increasing can only be inverted by having right moves in between # WLOG, want to make increasing with left moves then delete all remaining with right moves lefts = [0] * N for i, (x, y) in enumerate(zip(A, A[1:])): if x > y: lefts[i] = x - y # apply left cumulatively and check that it's always positive check = [0] * N for i in range(N)[::-1]: if i != N - 1: lefts[i] += lefts[i + 1] check[i] = A[i] - lefts[i] if all(x >= 0 for x in check): assert all(x <= y for x, y in zip(check, check[1:])) return "YES" return "NO" if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline TC = int(input()) for tc in range(1, TC + 1): (N,) = [int(x) for x in input().split()] A = [int(x) for x in input().split()] ans = solve(N, A) print(ans) ```
output
1
21,405
12
42,811
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,406
12
42,812
Tags: constructive algorithms, dp, greedy Correct Solution: ``` def solve(): n = int(input()) a = [0] + list(map(int,input().split())) v = -1 for i in range(1,n+1): if i == 1: v = 0 else: v = max(a[i] - a[i-1] + v,v) if a[i]< v: print("NO") return print("YES") return def main(): t = int(input()) for i in range(t): solve() return if __name__ == "__main__": main() ```
output
1
21,406
12
42,813
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,407
12
42,814
Tags: constructive algorithms, dp, greedy Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=list(map(int,input().split(" "))) x=0 while x<n-1 and a[x]>=a[x+1]: x+=1 if x==n-1: print("YES") continue else: onhold=a[x] #print(onhold) chk=0 x+=1 a[x]-=onhold for y in range(x+1,n): if a[y-1]>a[y]: chk=1 break if a[y]-onhold<a[y-1]: onhold=a[y]-a[y-1] a[y]=a[y-1] else: a[y]-=onhold # print(a) if chk==1: print("NO") else: print("YES") ```
output
1
21,407
12
42,815
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,408
12
42,816
Tags: constructive algorithms, dp, greedy Correct Solution: ``` import sys from math import gcd,sqrt,ceil,log2 from collections import defaultdict,Counter,deque from bisect import bisect_left,bisect_right import math sys.setrecursionlimit(2*10**5+10) import heapq from itertools import permutations # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 aa='abcdefghijklmnopqrstuvwxyz' 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") # import sys # import io, os # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_sum(bit,i): s = 0 i+=1 while i>0: s+=bit[i] i-=i&(-i) return s def update(bit,n,i,v): i+=1 while i<=n: bit[i]+=v i+=i&(-i) def modInverse(b,m): g = math.gcd(b, m) if (g != 1): return -1 else: return pow(b, m - 2, m) def primeFactors(n): sa = [] # sa.add(n) while n % 2 == 0: sa.append(2) n = n // 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: sa.append(i) n = n // i # sa.add(n) if n > 2: sa.append(n) return sa def seive(n): pri = [True]*(n+1) p = 2 while p*p<=n: if pri[p] == True: for i in range(p*p,n+1,p): pri[i] = False p+=1 return pri def check_prim(n): if n<0: return False for i in range(2,int(sqrt(n))+1): if n%i == 0: return False return True def getZarr(string, z): n = len(string) # [L,R] make a window which matches # with prefix of s l, r, k = 0, 0, 0 for i in range(1, n): # if i>R nothing matches so we will calculate. # Z[i] using naive way. if i > r: l, r = i, i # R-L = 0 in starting, so it will start # checking from 0'th index. For example, # for "ababab" and i = 1, the value of R # remains 0 and Z[i] becomes 0. For string # "aaaaaa" and i = 1, Z[i] and R become 5 while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 else: # k = i-L so k corresponds to number which # matches in [L,R] interval. k = i - l # if Z[k] is less than remaining interval # then Z[i] will be equal to Z[k]. # For example, str = "ababab", i = 3, R = 5 # and L = 2 if z[k] < r - i + 1: z[i] = z[k] # For example str = "aaaaaa" and i = 2, # R is 5, L is 0 else: # else start from R and check manually l = i while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 def search(text, pattern): # Create concatenated string "P$T" concat = pattern + "$" + text l = len(concat) z = [0] * l getZarr(concat, z) ha = [] for i in range(l): if z[i] == len(pattern): ha.append(i - len(pattern) - 1) return ha # n,k = map(int,input().split()) # l = list(map(int,input().split())) # # n = int(input()) # l = list(map(int,input().split())) # # hash = defaultdict(list) # la = [] # # for i in range(n): # la.append([l[i],i+1]) # # la.sort(key = lambda x: (x[0],-x[1])) # ans = [] # r = n # flag = 0 # lo = [] # ha = [i for i in range(n,0,-1)] # yo = [] # for a,b in la: # # if a == 1: # ans.append([r,b]) # # hash[(1,1)].append([b,r]) # lo.append((r,b)) # ha.pop(0) # yo.append([r,b]) # r-=1 # # elif a == 2: # # print(yo,lo) # # print(hash[1,1]) # if lo == []: # flag = 1 # break # c,d = lo.pop(0) # yo.pop(0) # if b>=d: # flag = 1 # break # ans.append([c,b]) # yo.append([c,b]) # # # # elif a == 3: # # if yo == []: # flag = 1 # break # c,d = yo.pop(0) # if b>=d: # flag = 1 # break # if ha == []: # flag = 1 # break # # ka = ha.pop(0) # # ans.append([ka,b]) # ans.append([ka,d]) # yo.append([ka,b]) # # if flag: # print(-1) # else: # print(len(ans)) # for a,b in ans: # print(a,b) def mergeIntervals(arr): # Sorting based on the increasing order # of the start intervals arr.sort(key = lambda x: x[0]) # array to hold the merged intervals m = [] s = -10000 max = -100000 for i in range(len(arr)): a = arr[i] if a[0] > max: if i != 0: m.append([s,max]) max = a[1] s = a[0] else: if a[1] >= max: max = a[1] #'max' value gives the last point of # that particular interval # 's' gives the starting point of that interval # 'm' array contains the list of all merged intervals if max != -100000 and [s, max] not in m: m.append([s, max]) return m class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def sol(n): seti = set() for i in range(1,int(sqrt(n))+1): if n%i == 0: seti.add(n//i) seti.add(i) return seti def lcm(a,b): return (a*b)//gcd(a,b) # # n,p = map(int,input().split()) # # s = input() # # if n <=2: # if n == 1: # pass # if n == 2: # pass # i = n-1 # idx = -1 # while i>=0: # z = ord(s[i])-96 # k = chr(z+1+96) # flag = 1 # if i-1>=0: # if s[i-1]!=k: # flag+=1 # else: # flag+=1 # if i-2>=0: # if s[i-2]!=k: # flag+=1 # else: # flag+=1 # if flag == 2: # idx = i # s[i] = k # break # if idx == -1: # print('NO') # exit() # for i in range(idx+1,n): # if # t = int(input()) for _ in range(t): n = int(input()) l = list(map(int,input().split())) if n == 1: print('YES') continue flag = 0 for i in range(1,n): if l[i-1]>l[i]: z = l[i-1]-l[i] if l[0]-z<0: flag = 1 break else: l[0]-=z if flag: print('NO') else: print('YES') ```
output
1
21,408
12
42,817
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,409
12
42,818
Tags: constructive algorithms, dp, greedy Correct Solution: ``` import sys import os from io import BytesIO, IOBase #Fast IO Region 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") T = int(input()) for _ in range(T): n = int(input()) arr = list(map(int, input().split())) dec, inc = 10**10, 0 ok = True for i, v in enumerate(arr): now = min(v - inc, dec) if now < 0: print('NO') break dec = now inc = v - dec else: print('YES') ```
output
1
21,409
12
42,819
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n positive integers. You can use the following operation as many times as you like: select any integer 1 ≀ k ≀ n and do one of two things: * decrement by one k of the first elements of the array. * decrement by one k of the last elements of the array. For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below): * decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4]; * decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3]; * decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3]; Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations. Input The first line contains one positive integer t (1 ≀ t ≀ 30000) β€” the number of test cases. Then t test cases follow. Each test case begins with a line containing one integer n (1 ≀ n ≀ 30000) β€” the number of elements in the array. The second line of each test case contains n integers a_1 … a_n (1 ≀ a_i ≀ 10^6). The sum of n over all test cases does not exceed 30000. Output For each test case, output on a separate line: * YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. * NO, otherwise. The letters in the words YES and NO can be outputed in any case. Example Input 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 Output YES YES NO YES
instruction
0
21,410
12
42,820
Tags: constructive algorithms, dp, greedy Correct Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def prod(a, mod=10**9+7): ans = 1 for each in a: ans = (ans * each) % mod return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if True else 1): n = int(input()) #n, k = map(int, input().split()) #a, b = map(int, input().split()) #c, d = map(int, input().split()) a = list(map(int, input().split())) print("YES" if a[0]-sum(max(0, a[i-1]-a[i])for i in range(1,n)) >= 0 else "NO") ```
output
1
21,410
12
42,821