message
stringlengths
2
28.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
21
109k
cluster
float64
7
7
__index_level_0__
int64
42
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working). Input The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels. Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once. We consider that pixel is already broken at moment ti. Output Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working. Examples Input 2 3 2 5 2 1 8 2 2 8 1 2 1 1 3 4 2 3 2 Output 8 Input 3 3 2 5 1 2 2 2 2 1 2 3 5 3 2 10 2 1 100 Output -1 Submitted Solution: ``` from sys import stdin, stdout m, n, k, q = map(int, stdin.readline().split()) events = [] for i in range(q): x, y, t = map(int, stdin.readline().split()) events.append((x, y, t)) events.sort(key = lambda x: x[-1]) l, r = -1, n * m + 1 while (r - l > 1): mid = (l + r) >> 1 ind = 0 maps = [[0 for i in range(m + 1)] for j in range(n + 1)] while ind < len(events) and events[ind][-1] <= mid: x, y, t = events[ind] maps[y][x] = 1 ind += 1 for i in range(1, m + 1): maps[0][i] += maps[0][i - 1] for i in range(1, n + 1): maps[i][0] += maps[i][0] for i in range(1, n + 1): for j in range(1, m + 1): maps[i][j] = maps[i - 1][j] + maps[i][j - 1] - maps[i - 1][j - 1] + maps[i][j] label = 0 for i in range(n, k - 1, -1): if label: break for j in range(m, k - 1, -1): if maps[i][j] + maps[i - k][j - k] - maps[i - k][j] - maps[i][j - k] == k * k: label = 1 break if label: r = mid else: l = mid if r == n * m + 1: stdout.write('-1') else: stdout.write(str(r)) ```
instruction
0
40,909
7
81,818
No
output
1
40,909
7
81,819
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,047
7
82,094
"Correct Solution: ``` Num = int(input()) a = list(map(int, input().split())) k = 0 for i in range(1, Num): if a[i-1] == a[i]: a[i] = 0 k += 1 print(k) ```
output
1
41,047
7
82,095
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,048
7
82,096
"Correct Solution: ``` N = int(input()) A = list(map(int,input().split())) ans = 0 for i in range(1,N): if A[i] == A[i-1]: A[i] = 0 ans += 1 print(ans) ```
output
1
41,048
7
82,097
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,049
7
82,098
"Correct Solution: ``` n=int(input());a=list(map(int,input().split()));i=l=0 while True: if i>n-1:break j=i while j<n-1 and a[j+1]==a[j]:j+=1 l+=(j-i+1)//2;i=j+1 print(l) ```
output
1
41,049
7
82,099
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,050
7
82,100
"Correct Solution: ``` n=int(input()) arr=list(map(int,input().split())) cnt=0 for i in range(1,n): if arr[i]==arr[i-1]: arr[i]='x' cnt+=1 print(cnt) ```
output
1
41,050
7
82,101
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,051
7
82,102
"Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) ans=0 now=0 for i in range(n): if now != a[i]: now=a[i] else: now=0 ans+=1 print(ans) ```
output
1
41,051
7
82,103
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,052
7
82,104
"Correct Solution: ``` N = int(input()) A = [int(i) for i in input().split()] cnt = 0 for i in range(1, N): if A[i-1] == A[i]: A[i] = -1 cnt += 1 print(cnt) ```
output
1
41,052
7
82,105
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,053
7
82,106
"Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = 101 cnt = 0 for i in range(n-1): if a[i] == a[i+1]: cnt += 1 a[i+1] = b b += 1 print (cnt) ```
output
1
41,053
7
82,107
Provide a correct Python 3 solution for this coding contest problem. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4
instruction
0
41,054
7
82,108
"Correct Solution: ``` #!/usr/bin/env python3 n = int(input()) a = list(map(int, input().split())) for i in range(1, n): if a[i] == a[i - 1]: a[i] = -1 print(a.count(-1)) ```
output
1
41,054
7
82,109
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` n = int(input()) l = input().split() cnt = 0 for i in range(1,n): if l[i] == l[i-1]: l[i] = "x" cnt += 1 print(cnt) ```
instruction
0
41,055
7
82,110
Yes
output
1
41,055
7
82,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` N = int(input()) a = list(input().split()) cnt = 0 for i in range(1, N): if a[i] == a[i-1]: a[i] = 'x' cnt += 1 print(cnt) ```
instruction
0
41,056
7
82,112
Yes
output
1
41,056
7
82,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` N=int(input()) a=[] a=input().split() sum=0 for i in range (1,N): if a[i-1]==a[i]: a[i]=i+10001 sum=sum+1 print(sum) ```
instruction
0
41,057
7
82,114
Yes
output
1
41,057
7
82,115
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) ans = 0 for i in range(1,n): if a[i-1] == a[i]: ans += 1 a[i] = "" print(ans) ```
instruction
0
41,058
7
82,116
Yes
output
1
41,058
7
82,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` import random N = int(input()) A = list(map(int, input().split())) cnt = 0 C = set(range(1, N + 1)) for idx in range(1, N - 1): B = set([A[idx - 1:-1][0], A[idx:][0], (A[idx + 1:] + [-1])[0]]) D = sorted(list(C - B)) if len(B) != 3: cnt += 1 A[idx] = random.choice(D) print(cnt) ```
instruction
0
41,059
7
82,118
No
output
1
41,059
7
82,119
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` N = int(input()) s = map(int, input().split()) c = 0 for i in range(1, N): if s[i] == s[i - 1]: c += 1 else: pass print(c) ```
instruction
0
41,060
7
82,120
No
output
1
41,060
7
82,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` n = input() a = list(map(int, input().split())) ans = 0 for i in range(1, len(a)-1): if a[i-1] == a[i]: a[i] = -1 ans += 1 print(ans) ```
instruction
0
41,061
7
82,122
No
output
1
41,061
7
82,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000. Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic. Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves? Constraints * 2 \leq N \leq 100 * 1 \leq a_i \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of spells required. Examples Input 5 1 1 2 2 2 Output 2 Input 3 1 2 1 Output 0 Input 5 1 1 1 1 1 Output 2 Input 14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 Output 4 Submitted Solution: ``` N = int(input()) L = list(map(int, input().split())) d = [(L[i+1]-L[i]) for i in range(len(L)-1)] b = False c = 0 ans = 0 for i in d: if i==0: b = True else : b = False if b: c += 1 else : ans += (-(-c//2)) c = 0 print(ans) ```
instruction
0
41,062
7
82,124
No
output
1
41,062
7
82,125
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,218
7
82,436
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` class UnionFind(): def __init__(self, size): self.table = [-1 for _ in range(size)] def find(self, x): if self.table[x] < 0: return x else: self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, x, y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: if self.table[s1] > self.table[s2]: self.table[s2] = s1 elif self.table[s1] < self.table[s2]: self.table[s1] = s2 else: self.table[s1] = s2 self.table[s2] -= 1 return n = int(input()) #d = [[[] for _ in range(4)] for _ in range(4)] d = [[] for _ in range(4)] uf = UnionFind(4) CVC = [list(map(int, input().split())) for _ in range(n)] vals = [] min_not_auto = float("inf") for i in range(n): cvc = CVC[i] vals.append(cvc[1]) #d[cvc[0] - 1][cvc[2] - 1] = cvc[1] #d[cvc[2] - 1][cvc[0] - 1] = cvc[1] d[cvc[0] - 1].append(cvc[1]) d[cvc[2] - 1].append(cvc[1]) if cvc[0] != cvc[2] and cvc[1] < min_not_auto: min_not_auto = cvc[1] uf.union(cvc[0] - 1, cvc[2] - 1) root_num = 0 for i in range(4): if uf.table[i] < 0: root_num += 1 if root_num == 1: odd = True for i in range(4): if len(d[i]) % 2 == 0: odd = False break if odd: print(sum(vals) - min_not_auto) else: answers = [0] * 4 for i in range(4): answers[uf.find(i)] += sum(d[i]) print(max(answers) // 2) else: answers = [0] * 4 for i in range(4): answers[uf.find(i)] += sum(d[i]) print(max(answers) // 2) ```
output
1
41,218
7
82,437
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,219
7
82,438
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` n=int(input()) score00=[0]*4 grid=[] for i in range(4): grid.append([]) for j in range(4): grid[-1].append([]) for i in range(n): a,x,b=map(int,input().split()) if a==b: score00[a-1]+=x continue if b<a: tmp=a a=b b=tmp grid[a-1][b-1].append(x) for i in range(4): for j in range(4): grid[i][j].sort() ans1=0 for i in range(2**16): tmp=0 score=[] fail=0 for j in range(4): score.append([0]*4) degrees=[0]*4 for j in range(4): for k in range(j+1,4): if i&(1<<(j*4+k)): if not grid[j][k]: fail=1 break score[j][k]=sum(grid[j][k])-grid[j][k][0] degrees[j]+=len(grid[j][k])-1 degrees[k]+=len(grid[j][k])-1 else: if grid[j][k]: score[j][k]=sum(grid[j][k]) degrees[j]+=len(grid[j][k]) degrees[k]+=len(grid[j][k]) if fail: break if fail: continue count=0 for j in range(4): if degrees[j]%2==1: count+=1 if count==4: continue totalscore=0 for j in range(4): for k in range(4): totalscore+=score[j][k] if score[0][1]+score[2][3]==totalscore: if score[0][1]==0: l=max(score00[0],score00[1]) else: l=score[0][1]+score00[0]+score00[1] if score[2][3]==0: r=max(score00[2],score00[3]) else: r=score[2][3]+score00[2]+score00[3] ans=max(l,r) elif score[0][2]+score[1][3]==totalscore: if score[0][2]==0: l=max(score00[0],score00[2]) else: l=score[0][2]+score00[0]+score00[2] if score[1][3]==0: r=max(score00[1],score00[3]) else: r=score[1][3]+score00[1]+score00[3] ans=max(l,r) elif score[0][3]+score[1][2]==totalscore: if score[0][3]==0: l=max(score00[0],score00[3]) else: l=score[0][3]+score00[0]+score00[3] if score[1][2]==0: r=max(score00[1],score00[2]) else: r=score[1][2]+score00[1]+score00[2] ans=max(l,r) else: tmp=[0]*4 for j in range(4): for k in range(4): if score[j][k]!=0: tmp[j]=1 tmp[k]=1 ans=totalscore for j in range(4): if tmp[j]: ans+=score00[j] ans1=max(ans1,ans) print(ans1) ```
output
1
41,219
7
82,439
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,220
7
82,440
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` # import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件 if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件 atexit.register(lambda :sys.stdout.close()) #idle 中不会执行 atexit N, = getIntList() zb = [] total = 0 de = [0,0,0,0,0] gr = [ [] for i in range(5)] minv = 10000000 for i in range(N): a,b,c = getIntList() de[a]+=1 de[c] +=1 zb.append( (a,b,c) ) total +=b if a!=c: minv = min(minv,b) vis = [0,0,0,0,0] def dfs( root): if vis[root]:return [] vis[root] = 1 r = [root,] for x in zb: a,b,c = x if a==root: r = r+ dfs(c) elif c==root: r = r+ dfs(a) return r res = 0 for i in range(1,5): if vis[i]:continue t = dfs(i) t = set(t) if len(t) ==4: for j in range(1,5): if de[j]%2==0: print(total) sys.exit() print(total-minv) sys.exit() tr = 0 for x in zb: a,b,c = x if a in t: tr +=b res = max(res,tr) print(res) ```
output
1
41,220
7
82,441
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,221
7
82,442
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` def min(a, b): if a < b: return a return b def max(a,b): return abs(min(-a,-b)) been = [0 for i in range(4)] ans = 0 minw = 10**18 degpar = [0 for i in range(4)] w = [0 for i in range(4)] gr = [list() for i in range(4)] rem = [[0 for i in range(4)] for j in range(4)] def dfs(x, l): l.append(w[x]) if been[x]: return been[x] = 1 for i in gr[x]: if been[i] == 0 and rem[x][i] > 0: dfs(i, l) n = int(input()) blocks = [] for i in range(n): a,v,b = map(int, input().split()) a -= 1 b -= 1 if a != b: blocks.append([a,v,b]) w[b] += v if a != b: minw = min(minw, v) degpar[a] += 1 degpar[a] %= 2 degpar[b] += 1 degpar[b] %= 2 gr[a].append(b) gr[b].append(a) rem[a][b] += 1 rem[b][a] += 1 ok = True for i in range(4): l = [] if been[i] == 0: dfs(i, l) if i == 0 and all(been) and all(degpar): ok = False break ans = max(ans, sum(l)) if ok: print(ans) exit() for p in blocks: i = p[0] j = p[2] w[j] -= p[1] been = [0 for i in range(4)] rem[i][j] -= 1 rem[j][i] -= 1 #print(i) #print(j) #print(':') for x in range(4): l = [] if been[x] == 0: dfs(x,l) ans = max(ans, sum(l)) #print(sum(l)) w[j] += p[1] rem[i][j] += 1 rem[j][i] += 1 print(ans) ```
output
1
41,221
7
82,443
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,222
7
82,444
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` n=int(input()) mv = 10**9 f = [0 for i in range(4)] adj = [[i == j for j in range(4)] for i in range(4)] w = [0 for i in range(4)] for _ in range(n): a,v,b=map(int,input().split()) a-=1 b-=1 if a!=b: mv = min(mv, v) f[a]^=1 f[b]^=1 w[a] += v adj[a][b] = adj[b][a] = True for k in range(4): for i in range(4): for j in range(4): if adj[i][k] and adj[k][j]: adj[i][j] = True if all(adj[0]) and all(f): print(sum(w) - mv) else: print(max(sum(w[j] for j in range(4) if adj[i][j]) for i in range(4))) ```
output
1
41,222
7
82,445
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,223
7
82,446
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` from random import randint values = [[] for _ in range(10)] types = [[0, 1, 2, 3], [1, 4, 5, 6], [2, 5, 7, 8], [3, 6, 8, 9]] b = [1]*10 B = 128 for i in range(9): b[i+1] = b[i]*B for i in range(4): values[types[i][i]].append(0) n = int(input()) for _ in range(n): i, v, j = map(int,input().split()) t = types[i-1][j-1] if i == j: values[t][0] += v else: values[t].append(v) for i in range(4): for j in range(i+1,4): t = types[i][j] values[t].sort() while len(values[t])>3: v=values[t].pop()+ values[t].pop() values[t][-1]+= v values[t].sort(reverse = True) cur=[{},{},{},{}] m = 0 for i in range(4): for j in range(i,4): t = types[i][j] if values[t]: cur[i][b[t]] = values[t][0] m = max(m,values[t][0]) def cnt(k,t): return k//b[t]%B for _ in range(n): next = [{},{},{},{}] for i in range(4): for j in range(4): t = types[i][j] for k, v in cur[i].items(): c = cnt(k,t) if len(values[t])>c: nk = k+b[t] if nk not in next[j]: next[j][nk] = values[t][c] + v m = max(m, values[t][c] + v) cur = next print (m) ```
output
1
41,223
7
82,447
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
instruction
0
41,224
7
82,448
Tags: bitmasks, brute force, dfs and similar, dp, graphs Correct Solution: ``` class Domino: def __init__(self,side1,side2,value,double,parts): self.side1=side1 self.side2=side2 self.value=value self.double=double #True/False self.parts=parts def __repr__(self): return str(self.side1)+'-'+str(self.side2)+'-'+str(self.double)+'-'+str(self.value)+'-'+'|'.join([str(i) for i in self.parts]) def __add__(self, other): if self.side1==other.side2: return Domino(self.side2,other.side1,self.value+other.value, self.side2==other.side1,self.parts+other.parts) elif self.side1==other.side1: return Domino(self.side2,other.side2,self.value+other.value, self.side2==other.side2,self.parts+other.parts) elif self.side2==other.side2: return Domino(self.side1,other.side1,self.value+other.value, self.side1==other.side1,self.parts+other.parts) elif self.side2==other.side1: return Domino(self.side1,other.side2,self.value+other.value, self.side1==other.side2,self.parts+other.parts) else: return Domino(0,0,0,True,[-1]) def SortByValue(Domino): return Domino.value def SortByDup(Domino): return Domino.double def addbycolor(self, other, color): if self.side1 == color and other.side2 == color: return Domino(self.side2, other.side1, self.value + other.value, self.side2 == other.side1, self.parts + other.parts) elif self.side1 == color and other.side1 == color: return Domino(self.side2, other.side2, self.value + other.value, self.side2 == other.side2, self.parts + other.parts) elif self.side2 == color and other.side2 == color: return Domino(self.side1, other.side1, self.value + other.value, self.side1 == other.side1, self.parts + other.parts) elif self.side2 == color and other.side1 == color: return Domino(self.side1, other.side2, self.value + other.value, self.side1 == other.side2, self.parts + other.parts) else: raise Exception def delcolor(l,num): p=1 while p==1: p=0 for i in range(len(l)-1): for j in range(i+1,len(l)): if (l[i].side1==num or l[i].side2==num) and (l[j].side1==num or l[j].side2==num): a=l.pop(i) b=l.pop(j-1) ab=addbycolor(a,b,num) l.append(ab) p=1 l.sort(key=SortByValue) l.sort(key=SortByDup) l.reverse() break if p==1: break l.sort(key=SortByValue) l.sort(key=SortByDup) l.reverse() return l def getvalue(l): value=0 for i in l: value+=i.value n=int(input()) l=[] l_dup=[] for i in range(n): q,w,e=[int(el) for el in input().split()] if q==e+100: l_dup.append(Domino(q,e,w,q==e,[i])) else: l.append(Domino(q,e,w,q==e,[i])) l.sort(key=SortByValue) l.sort(key=SortByDup) l.reverse() out=0 s1=[1,2,3,4] for i1 in s1: s2=s1.copy() s2.remove(i1) for i2 in s2: s3=s2.copy() s3.remove(i2) for i3 in s3: s4=s3.copy() s4.remove(i3) for i4 in s4: # l1_dup = l_dup.copy() l1 = l.copy() cc=len(l1) cont=1 while cont==1: cont=0 l1=delcolor(l1,i1) # for d in l1: # if d.side1==d.side2: # l1_dup.append(d) # l1.remove(d) l1=delcolor(l1,i2) # for d in l1: # if d.side1==d.side2: # l1_dup.append(d) # l1.remove(d) l1=delcolor(l1,i3) # for d in l1: # if d.side1==d.side2: # l1_dup.append(d) # l1.remove(d) l1=delcolor(l1,i4) # for d in l1: # if d.side1==d.side2: # l1_dup.append(d) # l1.remove(d) if len(l1)<cc: cc=len(l1) cont=1 #l1[0] - наша линия #вставим дупли # for i in l1[0].parts: # for j in l1_dup: # if l[i].side1==j.side1 or l[i].side2==j: # l1[0].value+=j.value # aa=l1_dup.append(j) l1.sort(key=SortByValue) l1.reverse() out=max(out,l1[0].value) print(out) ```
output
1
41,224
7
82,449
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value. Submitted Solution: ``` n=int(input()) l=[] s=set() ad={} numedges={} edge={} for i in range(n): l.append(list(map(int,input().split()))) s.add(l[i][0]) s.add(l[i][2]) numedges.setdefault((l[i][2],l[i][0]),0) numedges.setdefault((l[i][0],l[i][2]),0) edge.setdefault((l[i][2], l[i][0]), 0) edge.setdefault((l[i][0], l[i][2]), 0) numedges[(l[i][2],l[i][0])]+=1 if l[i][0] != l[i][2]: numedges[(l[i][0],l[i][2])]+=1 ad.setdefault(l[i][2],[]) ad.setdefault(l[i][0],[]) ad[l[i][2]].append([l[i][0],l[i][1]]) if l[i][0]!=l[i][2]: ad[l[i][0]].append([l[i][2],l[i][1]]) vis=dict() for i in s: vis[i]=False m=-1 def dfs(i): vis[i]=True s=0 for c,w in ad[i]: if c==i: if edge[(c,c)]>=numedges[(c,c)]: continue s+=w edge[(c,c)]+=1 continue if vis[c]: if edge[(i,c)]>=numedges[(i,c)]: continue s+=w edge[(i,c)]+=1 edge[(c,i)]+=1 continue if edge[(i,c)]>=numedges[(i,c)]: continue edge[(i, c)] += 1 edge[(c, i)] += 1 s=s+dfs(c)+w return s for i in s: if not vis[i]: s=dfs(i) #print(s) m=max(m,s) print(m) ```
instruction
0
41,225
7
82,450
No
output
1
41,225
7
82,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value. Submitted Solution: ``` n=int(input()) mv = 10**9 f = [0 for i in range(4)] adj = [[i == j for j in range(4)] for i in range(4)] w = [0 for i in range(4)] for _ in range(n): a,v,b=map(int,input().split()) a-=1 b-=1 if a!=b: mv = min(mv, v) f[a]^=1 f[b]^=1 w[a] += v adj[a][b] = adj[b][a] = True for k in range(4): for i in range(4): for j in range(4): if adj[i][k] and adj[k][j]: adj[i][j] = True if all(adj[0]) and all(f): print(sum(w) - mv) else: print(max(sum(w[j] for j in range(4) if adj[i][j]) for i in range(4))) ```
instruction
0
41,226
7
82,452
No
output
1
41,226
7
82,453
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value. Submitted Solution: ``` m = 0 m2 = 0 def f(a, b, c): global m global m2 l = len(b) if c > m2: m2 = c if l > m: m = l for i in a: k = a.index(i) d = a[:k]+a[k+1:] e = [i[::-1]] if not b: f(d, [i], c + i[2]) elif i[0] == b[-1][1]: f(d, b+[i], c + i[2]) elif i[0] == b[0][0]: f(d, e+b, c + i[2]) elif i[1] == b[0][0]: f(d,[i]+b, c + i[2]) elif i[1] == b[-1][1]: f(d, b+e, c + i[2]) def main(): n = int(input()) g = [] for _ in range(n): a, w, b = map(int, input().split()) g.append([a, b, w]) f(g, [], 0) print(m2) if __name__ == '__main__': main() ```
instruction
0
41,227
7
82,454
No
output
1
41,227
7
82,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks. Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000). Output Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value. Submitted Solution: ``` n=int(input()) score00=[0]*4 grid=[] for i in range(4): grid.append([]) for j in range(4): grid[-1].append([]) for i in range(n): a,x,b=map(int,input().split()) if a==b: score00[a-1]+=x continue if b<a: tmp=a a=b b=tmp grid[a-1][b-1].append(x) for i in range(4): for j in range(4): grid[i][j].sort() ans1=0 for i in range(2**16): tmp=0 score=[] fail=0 for j in range(4): score.append([0]*4) degrees=[0]*4 for j in range(4): for k in range(j+1,4): if i&(1<<(j*4+k)): if not grid[j][k]: fail=1 break score[j][k]=sum(grid[j][k])-grid[j][k][0] degrees[j]+=len(grid[j][k])-1 degrees[k]+=len(grid[j][k])-1 else: if grid[j][k]: score[j][k]=sum(grid[j][k]) degrees[j]+=len(grid[j][k]) degrees[k]+=len(grid[j][k]) if fail: break if fail: continue totalscore=0 for j in range(4): for k in range(4): totalscore+=score[j][k] if score[0][1]+score[2][3]==totalscore: if score[0][1]==0: l=max(score00[0],score00[1]) else: l=score[0][1]+score00[0]+score00[1] if score[2][3]==0: r=max(score00[2],score00[3]) else: r=score[2][3]+score00[2]+score00[3] ans=max(l,r) elif score[0][2]+score[1][3]==totalscore: if score[0][2]==0: l=max(score00[0],score00[2]) else: l=score[0][2]+score00[0]+score00[2] if score[1][3]==0: r=max(score00[1],score00[3]) else: r=score[1][3]+score00[1]+score00[3] ans=max(l,r) elif score[0][3]+score[1][2]==totalscore: if score[0][3]==0: l=max(score00[0],score00[3]) else: l=score[0][3]+score00[0]+score00[3] if score[1][2]==0: r=max(score00[1],score00[2]) else: r=score[1][2]+score00[1]+score00[2] ans=max(l,r) else: count=0 for j in range(4): if degrees[j]%2==1: count+=1 if count==4: continue ans=sum(score00)+totalscore ans1=max(ans1,ans) print(ans1) ```
instruction
0
41,228
7
82,456
No
output
1
41,228
7
82,457
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,252
7
82,504
Tags: greedy, sortings Correct Solution: ``` import sys input = sys.stdin.readline n,k=map(int,input().split()) A=list(map(int,input().split())) if n<k: print("NO") sys.exit() LIST=[[] for i in range(max(A)+1)] for i in range(n): LIST[A[i]].append(i) for li in LIST: if len(li)>k: print("NO") sys.exit() ANS=[None]*n i=1 for li in LIST: for num in li: ANS[num]=i i+=1 if i==k+1: i=1 print("YES") for a in ANS: print(a,end=" ") ```
output
1
41,252
7
82,505
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,253
7
82,506
Tags: greedy, sortings Correct Solution: ``` n, m = map(int, input().split()) tmp = list(map(int, input().split())) a = [] for i in range (n): a.append([tmp[i], i]) # a = list(map(int, input().split())) b = [0 for i in range (5003)] ans = [0 for i in range (n)] a.sort() cnt = 0 for i in a: if (b[i[0]] == 0): b[i[0]] = cnt + 1 ans[i[1]] = cnt + 1 elif (b[i[0]] == cnt + 1): print("NO") exit() else: ans[i[1]] = cnt + 1 cnt = (cnt + 1) % m print("YES") print(*ans) ```
output
1
41,253
7
82,507
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,254
7
82,508
Tags: greedy, sortings Correct Solution: ``` n,k = map(int,input().split()) wallList = list(map(int,input().split())) walls = {} for i,w in enumerate(wallList): temp = walls.get(w,[]) temp.append(i) walls[w] = temp if n >=k and max(map(len,walls.values())) <= k : r = ['1']*n c = 1 for w in walls: for i in walls[w]: r[i] = str(c) if (c+1)%k: c = (c+1)%k else: c = k print("YES") print(' '.join(r)) else: print("NO") ```
output
1
41,254
7
82,509
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,255
7
82,510
Tags: greedy, sortings Correct Solution: ``` n, k = list(map(int, input().split())) a = list(map(int, input().split())) ok, d = True, {} for item in a: d[item] = d.get(item, 0) + 1 if d[item] > k: ok = False break if not ok or n < k: print("NO") else: aa = zip(list(range(0, n)), a) aa = sorted(aa, key=lambda x: x[1]) c = 1 z, res = False, [1] * n for i, v in aa: res[i] = c if c >= k: z = True c += 1 c = c % (k + 1) if c == 0: c += 1 if z: print("YES") print(' '.join(map(str, res))) else: print('NO') ```
output
1
41,255
7
82,511
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,256
7
82,512
Tags: greedy, sortings Correct Solution: ``` from collections import Counter n,k = list(map(int,input().split())) a = list(map(int,input().split())) d = {} out = [] for i in a: d[i] = d.get(i, 0)+1 out.append(d[i]) mv = max(out) if mv>k or k>n: print("NO") else: print("YES") if k==mv: print(*out) else: d = Counter(out) k-=mv i = 0 while k: if d[out[i]]>1: d[out[i]]-=1 out[i]=mv+1 mv+=1 k-=1 else: i+=1 print(*out) ```
output
1
41,256
7
82,513
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,257
7
82,514
Tags: greedy, sortings Correct Solution: ``` from collections import defaultdict as dfd n, k = map(int, input().split()) arr = list(map(int, input().split())) d = dfd(int) for i in arr: d[i] += 1 if k>n: print("NO") elif k==n: print("YES") print(*[i for i in range(1, len(arr)+1)]) else: flag = 0 for i in d.values(): if i>k: flag = 1 print("NO") break brr = [0]*len(arr) element_covered = 0 mx = 0 fg = 0 if flag==0: for i in d.keys(): fd = 1 for j in range(len(arr)): if arr[j]==i: brr[j] = fd element_covered += 1 mx = max(mx, fd) if len(arr)-element_covered<=k-mx: #print(k, mx, element_covered) #print(*brr) fg = 1 break fd += 1 if fg==1: #print("mx==", mx) break if fg==1: #print("mx==", mx) for i in d.keys(): for j in range(len(arr)): if brr[j]==0: if arr[j]==i: brr[j] = mx + 1 mx += 1 #print(*brr) print("YES") print(*brr) ##n = int(input()) ##n %= 4 ##if n==1 or n==2: ## print(1) ##else: ## print(0) ```
output
1
41,257
7
82,515
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,258
7
82,516
Tags: greedy, sortings Correct Solution: ``` from collections import Counter from itertools import cycle def main(): n, k = map(int, input().split()) a = input().split() counts = Counter(a) if max(counts.values()) > k: print('NO') return print('YES') colors = cycle(range(1, k + 1)) value_colors = { value: {next(colors) for __ in range(repeat)} for (value, repeat) in counts.items() } array_colors = (value_colors[element].pop() for element in a) print(*array_colors) main() ```
output
1
41,258
7
82,517
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples.
instruction
0
41,259
7
82,518
Tags: greedy, sortings Correct Solution: ``` n,k=map(int,input().split()) lst=list(map(int,input().split())) d={} lst2=[];lst3=[] for c in range(n): lst2.append([lst[c],c]) for c in lst: d[c]=d.get(c,0)+1 if max(d.values())>k: print("NO") exit(0) print("YES") lst2.sort() p=0 for c in range(n): lst3.append(p+1) p=(p+1)%k lst4=[0 for c in range(n)] for c in range(n): lst4[lst2[c][1]]=lst3[c] print(*lst4) ```
output
1
41,259
7
82,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` n,k=[int(x) for x in input().split()] mas=[int(x) for x in input().split()] tot=[] dic={} for i in range(k): dic[i+1]=set() dic[i+1].add(mas[i]) tot.append(i+1) for item in range(k,len(mas)): for i in range(k): if mas[item] not in dic[i+1]: tot.append(i+1) dic[i+1].add(mas[item]) break else: print('NO') break else: total='' for item in tot: total+=str(item)+' ' print('YES') print(total[:-1]) ```
instruction
0
41,260
7
82,520
Yes
output
1
41,260
7
82,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` n, k = map(int,input().split()) tee = list(map(int,input().split())) arr = [] dic, flag = dict(), True #for i in input.split() : if (k > n) : flag = False for i in tee : temp = int(i) if (temp in dic) : dic[temp] += 1 else : dic[temp] = 1 arr.append(temp) if (dic[temp] > k) : flag = False newdic, ans = dict(), [] coldic = dict() temparr = [] if (flag) : for j in range(len(arr)) : i = arr[j] if (i in newdic) : newdic[i] += 1 else : newdic[i] = 1 ans.append(dic[i] - newdic[i] + 1) if ((dic[i]-newdic[i]+1) in coldic) : coldic[dic[i]-newdic[i]+1] += 1 temparr.append(j) else : coldic[dic[i]-newdic[i]+1] = 1 for iarr in temparr : for col in range(1, k+1) : if (col not in coldic) : ans[iarr] = col coldic[col] = 1 break print("YES") for j in ans : print(j, end=" ") else : print("NO") ```
instruction
0
41,261
7
82,522
Yes
output
1
41,261
7
82,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` n,k=list(map(int,input().split())) a=list(map(int,input().split())) d=[0]*(5001) for i in a: d[i]+=1 for i in range(1,5001): if(d[i]>k): print("NO") break else: done=[] for i in range(5001): done.append(set()) for i in range(k): done[a[i]].add(i+1) a[i]=i+1 for i in range(k,n): for j in range(1,k+1): if(j not in done[a[i]]): done[a[i]].add(j) a[i]=j break print("YES") print(*a) ```
instruction
0
41,262
7
82,524
Yes
output
1
41,262
7
82,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` n, k = map(int, input().split()) ar = list(map(int, input().split())) m = {} for x in ar: m[x] = m.get(x, 0) + 1 for x in m.values(): if x > k: print('NO') exit(0) ans = [0 for i in range(n)] s = [set() for i in range(k)] for i in range(k): ans[i] = i + 1 s[i].add(ar[i]) for i in range(k, len(ar)): for t in range(k): if ar[i] not in s[t]: ans[i] = t + 1 s[t].add(ar[i]) break print('YES') print(*ans) ```
instruction
0
41,263
7
82,526
Yes
output
1
41,263
7
82,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` def colors(a,k): s = [] res = [] if len(a) == len(set(a)): print('YES') print(' '.join(str(e) for e in a)) return for i in a: if a.count(i) > k: print('NO') return for i in a: if i in s and s.count(i)<k: k = k-s.count(i) res.append(k) s.append(i) else: s.append(i) # print(s) res.append(k) print('YES') print(' '.join(str(e) for e in res)) return n,k = map(int, input().split()) a = input().split() colors(a,k) # a = [3,2,1,2,3] # colors(a,5) ```
instruction
0
41,264
7
82,528
No
output
1
41,264
7
82,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` from collections import * from math import * n,k = list(map(int,input().split())) a = list(map(int,input().split())) c = Counter(a) if(max(c.values()) > k): print("NO") else: print("YES") for key,val in c.items(): c[key] = 1 ans = [0 for i in range(n)] for i in range(n): ans[i] = c[a[i]] c[a[i]] += 1 mx = max(ans) c = Counter(ans) for i in range(n): if(mx == k): break if(c[ans[i]] > 1): ans[i] = mx+1 mx += 1 print(*ans) ```
instruction
0
41,265
7
82,530
No
output
1
41,265
7
82,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` from collections import defaultdict import sys n, k = [int(i) for i in input().split()] numbers = list(map(int, input().split())) records = defaultdict(int) required_numbers = [i + 1 for i in range(k)] answer = [] for number in numbers: potential_k = records[number] + 1 if potential_k <= k: answer.append(potential_k) else: print("NO") sys.exit() records[number] += 1 if records[number] in required_numbers: required_numbers.remove(records[number]) uniques = list(set(numbers)) indicies = 0 for rn in required_numbers: # answer[numbers.index(uniques[indicies])] = rn answer[indicies] = rn indicies += 1 print('YES') print(*answer) ```
instruction
0
41,266
7
82,532
No
output
1
41,266
7
82,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. You have to color this array in k colors in such a way that: * Each element of the array should be colored in some color; * For each i from 1 to k there should be at least one element colored in the i-th color in the array; * For each i from 1 to k all elements colored in the i-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the length of the array a and the number of colors, respectively. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 5000) — elements of the array a. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c_1, c_2, ... c_n, where 1 ≤ c_i ≤ k and c_i is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Examples Input 4 2 1 2 2 3 Output YES 1 1 2 2 Input 5 2 3 2 1 2 3 Output YES 2 1 1 2 1 Input 5 2 2 1 1 2 1 Output NO Note In the first example the answer 2~ 1~ 2~ 1 is also acceptable. In the second example the answer 1~ 1~ 1~ 2~ 2 is also acceptable. There exist other acceptable answers for both examples. Submitted Solution: ``` import sys s = input().split(' ') n = int(s[0]) k = int(s[1]) m = list(map(int, input().split(' '))) count_ = [0 for l in range(100000)] rask = [[] for _ in range(k)] b = True l = 0 for i in m: count_[i] += 1 if count_[i] > k: b = False break rask[count_[i] % k].append(i) print('YES' if b else 'NO') if b: for l in range(k): for a in rask[l]: print(l + 1, ' ', end='') if __name__ == "__main__": pass ```
instruction
0
41,267
7
82,534
No
output
1
41,267
7
82,535
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,284
7
82,568
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) c1 = l.count(1) c2 = l.count(2) if(c1 == 0): ans = ["2"]*c2 elif(c1 < 3 and c2 > 0): ans = ["2"]+["1"]+["2"]*(c2-1)+["1"]*(c1-1) elif((c1%2) == 1): ans = ["1"]*c1+["2"]*c2 else: ans = ["1"]*(c1-1)+["2"]*c2+["1"] print(" ".join(ans)) ```
output
1
41,284
7
82,569
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,285
7
82,570
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) r = input().split() a = r.count('1') b = r.count('2') if a == 0: rez = b*'2' elif b == 0: rez = a*'1' else: rez = '21'+(b-1)*'2' + (a-1)*'1' print(' '.join(list(rez))) ```
output
1
41,285
7
82,571
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,286
7
82,572
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` # Allahumma SalliI Ala Muhammadiw Wa Ala Aali Muhammadin # Kamaa Sal’laita Ala Ibrahima Wa Ala Aali Ibrahima Inna’ka Hamidum Majid. # Allahumma Baarik Ala Muhammadiw Wa Ala Aali Muhammadin # Kamaa Baarakta Ala Ibrahima Wa Ala Aali Ibrahima Inna’ka Hamidum Majid. n = int(input()) a = [int (x) for x in input().split()] odd=0 #even=0 #a.append(0) for i in range(0,n): if(a[i]==1): odd+=1 #print(odd) even=n-odd if(even>0): print("2",end=' ') even=even-1 #print(even) if(odd>0): print("1",end=' ') odd=odd-1 #print(odd) for i in range(0,even): print("2",end=' ') for i in range(0,odd): print("1",end=' ') ```
output
1
41,286
7
82,573
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,287
7
82,574
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n=int(input()) m=list(map(int,input().split())) a,b=0,0 for i in m: if i==1: a+=1 else: b+=1 if a>=3: print("1 1 1 "+"2 "*b+"1 "*(a-3)) elif a==2 and b>0: print("2 "+"1 "+"2 "*(b-1)+"1") elif a==2 and b==0: print("1 1") elif a==1 and b>0: print("2 1 "+"2 "*(b-1)) elif a==1 and b==0: print(1) elif a==0: print("2 "*b) ```
output
1
41,287
7
82,575
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,288
7
82,576
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` from collections import Counter from sys import exit N = int(input()) A = list(map(int, input().split())) CA = Counter(A) if N == 1: print(*A) exit() if N == 2: A.sort(reverse = True) print(*A) exit() if CA[1] == 0: print(*A) exit() if CA[2] == 0: print(*A) exit() print(*([2, 1] + [2]*(CA[2]-1) + [1]*(CA[1]-1))) ```
output
1
41,288
7
82,577
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
instruction
0
41,289
7
82,578
Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) aset = set(a) if len(aset) == 1: print(' '.join(map(str, a))) else: count = [-1, -1] for elem in a: count[elem - 1] += 1 print(2, 1, end = ' ') for i in range(count[1]): print(2, end = ' ') for i in range(count[0]): print(1, end = ' ') ```
output
1
41,289
7
82,579