message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.
Obviously, the game ends when the entire sheet is cut into 1 × 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.
You are given an n × m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 109, 0 ≤ k ≤ 105) — the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 ≤ xbi, xei ≤ n, 0 ≤ ybi, yei ≤ m) — the coordinates of the ends of the existing cuts.
It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.
The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.
Output
If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).
Examples
Input
2 1 0
Output
FIRST
1 0 1 1
Input
2 2 4
0 1 2 1
0 1 2 1
1 2 1 0
1 1 1 2
Output
SECOND
Submitted Solution:
```
from functools import reduce
from itertools import groupby
from operator import xor
width, height, cuts = list(map(int, input().split()))
if width == 1 and height == 1:
print("SECOND")
exit()
c = [height] * (width - 1) + [width] * (height - 1)
cu = []
for i in range(cuts):
x1, y1, x2, y2 = list(map(int, input().split()))
if x1 == x2:
cu.append((x1 - 1,) + tuple(sorted((y1, y2))))
else:
cu.append((width - 1 + y1 - 1,) + tuple(sorted((x1, x2))))
cu.sort()
for k, g in groupby(cu, key=lambda x: x[0]):
lastEnd = 0
for cur in g:
c[k] -= max(cur[2] - max(cur[1], lastEnd), 0)
lastEnd = cur[2]
x = reduce(xor, c)
if x > 0:
print("FIRST")
for j, i in enumerate(c):
if i >= x:
for k, g in groupby(cu, key=lambda x: x[0]):
if k != j:
continue
lastEnd = 0
num = 0
for cur in g:
if cur[1] > lastEnd:
num += cur[1] - lastEnd
lastEnd = cur[2]
if num >= x:
if k >= width - 1:
print("aa%s %s %s %s" % (k - width + 2, 0, k - width + 2, cur[1] - num + x))
else:
print("aa%s %s %s %s" % (0, k + 1, cur[1] - num + x, k + 1))
exit()
if k >= width - 1:
print("AA%s %s %s %s" % (k - width + 2, 0, k - width + 2, height - c[k] + x))
else:
print("AA%s %s %s %s" % (0, k + 1, width - c[k] + x, k + 1))
exit()
if i >= width - 1:
print("%s %s %s %s" % (i - width + 2, 0, i - width + 2, x))
else:
print("%s %s %s %s" % (0, i + 1, x, i + 1))
exit()
else:
print("SECOND")
``` | instruction | 0 | 103,170 | 19 | 206,340 |
No | output | 1 | 103,170 | 19 | 206,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.
Obviously, the game ends when the entire sheet is cut into 1 × 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.
You are given an n × m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 109, 0 ≤ k ≤ 105) — the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 ≤ xbi, xei ≤ n, 0 ≤ ybi, yei ≤ m) — the coordinates of the ends of the existing cuts.
It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.
The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.
Output
If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).
Examples
Input
2 1 0
Output
FIRST
1 0 1 1
Input
2 2 4
0 1 2 1
0 1 2 1
1 2 1 0
1 1 1 2
Output
SECOND
Submitted Solution:
```
from functools import reduce
from itertools import groupby
from operator import xor
width, height, cuts = list(map(int, input().split()))
if width == 1 and height == 1:
print("SECOND")
exit()
cache = {}
widths = 0
heights = 0
def get(a):
if a in cache:
return cache[a]
if a < width - 1:
return height
return width
def set(a, b):
global widths, heights
if a not in cache:
if a < width - 1:
widths += 1
else:
heights += 1
cache[a] = b
cu = []
for i in range(cuts):
x1, y1, x2, y2 = list(map(int, input().split()))
if x1 == x2:
cu.append((x1 - 1,) + tuple(sorted((y1, y2))))
else:
cu.append((width - 1 + y1 - 1,) + tuple(sorted((x1, x2))))
cu.sort()
for k, g in groupby(cu, key=lambda x: x[0]):
lastEnd = 0
for cur in g:
set(k, get(k) - max(cur[2] - max(cur[1], lastEnd), 0))
lastEnd = cur[2]
x = 0
for i in cache.values():
x ^= i
if (width - 1 - widths) % 2 == 1:
x ^= height
if (height - 1 - heights) % 2 == 1:
x ^= width
if x > 0:
print("FIRST")
for j in range(width + height - 2):
i = get(j)
if i ^ x < i:
x = i - (i ^ x)
for k, g in groupby(cu, key=lambda z: z[0]):
if k != j:
continue
lastEnd = 0
num = 0
for cur in g:
if cur[1] > lastEnd:
num += cur[1] - lastEnd
lastEnd = max(cur[2], lastEnd)
if num >= x:
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, cur[1] - num + x))
else:
print("%s %s %s %s" % (0, k - width + 2, cur[1] - num + x, k - width + 2))
exit()
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, height - get(k) + x))
else:
print("%s %s %s %s" % (0, k - width + 2, width - get(k) + x, k - width + 2))
exit()
if j < width - 1:
print("%s %s %s %s" % (j + 1, 0, j + 1, x))
else:
print("%s %s %s %s" % (0, j - width + 2, x, j - width + 2))
exit()
else:
print("SECOND")
``` | instruction | 0 | 103,171 | 19 | 206,342 |
No | output | 1 | 103,171 | 19 | 206,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.
Obviously, the game ends when the entire sheet is cut into 1 × 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.
You are given an n × m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 109, 0 ≤ k ≤ 105) — the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 ≤ xbi, xei ≤ n, 0 ≤ ybi, yei ≤ m) — the coordinates of the ends of the existing cuts.
It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.
The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.
Output
If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).
Examples
Input
2 1 0
Output
FIRST
1 0 1 1
Input
2 2 4
0 1 2 1
0 1 2 1
1 2 1 0
1 1 1 2
Output
SECOND
Submitted Solution:
```
from functools import reduce
from itertools import groupby
from operator import xor
width, height, cuts = list(map(int, input().split()))
if width == 1 and height == 1:
print("SECOND")
exit()
cache = {}
widths = 0
heights = 0
def get(a):
if a in cache:
return cache[a]
if a < width - 1:
return height
return width
def set(a, b):
global widths, heights
if a < width - 1:
widths += 1
else:
heights += 1
cache[a] = b
cu = []
for i in range(cuts):
x1, y1, x2, y2 = list(map(int, input().split()))
if x1 == x2:
cu.append((x1 - 1,) + tuple(sorted((y1, y2))))
else:
cu.append((width - 1 + y1 - 1,) + tuple(sorted((x1, x2))))
cu.sort()
for k, g in groupby(cu, key=lambda x: x[0]):
lastEnd = 0
for cur in g:
set(k, get(k) - max(cur[2] - max(cur[1], lastEnd), 0))
lastEnd = cur[2]
x = 0
for i in cache.values():
x ^= i
if (width - 1 - widths) % 2 == 1:
x ^= height
if (height - 1 - heights) % 2 == 1:
x ^= width
if x > 0:
print("FIRST")
for j in range(width + height - 2):
i = get(j)
if i ^ x <= i:
x = i - (i ^ x)
for k, g in groupby(cu, key=lambda z: z[0]):
if k != j:
continue
lastEnd = 0
num = 0
for cur in g:
if cur[1] > lastEnd:
num += cur[1] - lastEnd
lastEnd = max(cur[2], lastEnd)
if num >= x:
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, cur[1] - num + x))
else:
print("%s %s %s %s" % (0, k - width + 2, cur[1] - num + x, k - width + 2))
exit()
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, height - get(k) + x))
else:
print("%s %s %s %s" % (0, k - width + 2, width - get(k) + x, k - width + 2))
exit()
if j < width - 1:
print("%s %s %s %s" % (j + 1, 0, j + 1, x))
else:
print("%s %s %s %s" % (0, j - width + 2, x, j - width + 2))
exit()
else:
print("SECOND")
``` | instruction | 0 | 103,172 | 19 | 206,344 |
No | output | 1 | 103,172 | 19 | 206,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.
Obviously, the game ends when the entire sheet is cut into 1 × 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.
You are given an n × m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 109, 0 ≤ k ≤ 105) — the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 ≤ xbi, xei ≤ n, 0 ≤ ybi, yei ≤ m) — the coordinates of the ends of the existing cuts.
It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.
The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.
Output
If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).
Examples
Input
2 1 0
Output
FIRST
1 0 1 1
Input
2 2 4
0 1 2 1
0 1 2 1
1 2 1 0
1 1 1 2
Output
SECOND
Submitted Solution:
```
from functools import reduce
from itertools import groupby
from operator import xor
width, height, cuts = list(map(int, input().split()))
if width == 1 and height == 1:
print("SECOND")
exit()
cache = {}
widths = 0
heights = 0
def get(a):
if a in cache:
return cache[a]
if a < width - 1:
return height
return width
def set(a, b):
global widths, heights
if a < width - 1:
widths += 1
else:
heights += 1
cache[a] = b
cu = []
for i in range(cuts):
x1, y1, x2, y2 = list(map(int, input().split()))
if x1 == x2:
cu.append((x1 - 1,) + tuple(sorted((y1, y2))))
else:
cu.append((width - 1 + y1 - 1,) + tuple(sorted((x1, x2))))
cu.sort()
for k, g in groupby(cu, key=lambda x: x[0]):
lastEnd = 0
for cur in g:
set(k, get(k) - max(cur[2] - max(cur[1], lastEnd), 0))
lastEnd = cur[2]
x = 0
for i in cache.values():
x ^= i
if width - 1 - widths % 2 == 1:
x ^= height
if height - 1 - heights % 2 == 1:
x ^= width
if x > 0:
print("FIRST")
for j in range(width + height - 2):
i = get(j)
if i ^ x <= i:
x = i - (i ^ x)
for k, g in groupby(cu, key=lambda z: z[0]):
if k != j:
continue
lastEnd = 0
num = 0
for cur in g:
if cur[1] > lastEnd:
num += cur[1] - lastEnd
lastEnd = cur[2]
if num >= x:
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, cur[1] - num + x))
else:
print("%s %s %s %s" % (0, k - width + 2, cur[1] - num + x, k - width + 2))
exit()
if k < width - 1:
print("%s %s %s %s" % (k + 1, 0, k + 1, height - get(k) + x))
else:
print("%s %s %s %s" % (0, k - width + 2, width - get(k) + x, k - width + 2))
exit()
if j < width - 1:
print("%s %s %s %s" % (j + 1, 0, j + 1, x))
else:
print("%s %s %s %s" % (0, j - width + 2, x, j - width + 2))
exit()
else:
print("SECOND")
``` | instruction | 0 | 103,173 | 19 | 206,346 |
No | output | 1 | 103,173 | 19 | 206,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,191 | 19 | 206,382 |
Tags: binary search, math, sortings
Correct Solution:
```
import math
n=int(input())
arr=list(map(int,input().split()))
print(max(max(arr),math.ceil(sum(arr)/(n-1))))
``` | output | 1 | 103,191 | 19 | 206,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,192 | 19 | 206,384 |
Tags: binary search, math, sortings
Correct Solution:
```
"""
pppppppppppppppppppp
ppppp ppppppppppppppppppp
ppppppp ppppppppppppppppppppp
pppppppp pppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppp pppppppp
ppppppppppppppppppppp ppppppp
ppppppppppppppppppp ppppp
pppppppppppppppppppp
"""
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var))
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
s = sum(arr)
answer = max(arr)
out(max(answer, ceil(s / (n-1))))
``` | output | 1 | 103,192 | 19 | 206,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,193 | 19 | 206,386 |
Tags: binary search, math, sortings
Correct Solution:
```
#!/usr/bin/env python
#coding:utf-8
import sys
if __name__ == "__main__":
n = int( input() )
l = list(map(int, input().split()))
s = sum(l)
n = n - 1
res = (s%n == 0) and s // n or s // n + 1
res = max(res, max(l))
print(res)
``` | output | 1 | 103,193 | 19 | 206,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,194 | 19 | 206,388 |
Tags: binary search, math, sortings
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
res = max((sum(a)+(n-2))//(n-1), max(a))
print(res)
``` | output | 1 | 103,194 | 19 | 206,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,195 | 19 | 206,390 |
Tags: binary search, math, sortings
Correct Solution:
```
n = int(input())
a = [int(s) for s in input().split()]
sum, b = 0, 0
for i in a:
b = max(b, i)
sum += i
if b * n - sum >= b:
print(b)
else:
print((sum + n - 2) // (n - 1))
``` | output | 1 | 103,195 | 19 | 206,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,196 | 19 | 206,392 |
Tags: binary search, math, sortings
Correct Solution:
```
import sys
import math
#sys.stdin = open('in.txt')
n = int(input())
a = list(map(int, input().split()))
ans= max(math.ceil(sum(a)/(n-1)),max(a))
print(ans)
``` | output | 1 | 103,196 | 19 | 206,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,197 | 19 | 206,394 |
Tags: binary search, math, sortings
Correct Solution:
```
import math
n=int(input())
inp=[int(x) for x in input().split()]
print(max(max(inp), math.ceil(sum(inp)/(n-1))))
``` | output | 1 | 103,197 | 19 | 206,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game). | instruction | 0 | 103,198 | 19 | 206,396 |
Tags: binary search, math, sortings
Correct Solution:
```
import sys
import math
#from queue import *
#import random
#sys.setrecursionlimit(int(1e6))
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input()))
def inara():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
n=inp()
ara=inara()
ara.sort()
lo=ara[-1]
hi=100000000000000000000000000000
ans=hi
while hi>=lo:
mid=(hi+lo)//2
tot=0
for num in ara:
tot+=mid-num
if tot>=mid:
break
if tot>=mid:
ans=mid
hi=mid-1
else:
lo=mid+1
print(ans)
``` | output | 1 | 103,198 | 19 | 206,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
m = max(l)
n = n - 1
if s%n == 0:
e = s // n
else:
e = s // n + 1
if e < m:
e = m
print(e)
``` | instruction | 0 | 103,199 | 19 | 206,398 |
Yes | output | 1 | 103,199 | 19 | 206,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
import math
n=int(input())
arr=list(map(int,input().split()))
summ=sum(arr)
ans=math.ceil(summ/(n-1))
print(max(max(arr),ans))
``` | instruction | 0 | 103,200 | 19 | 206,400 |
Yes | output | 1 | 103,200 | 19 | 206,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
def ceil(x):
if int(x) != x:
return int(x+1)
else:
return int(x)
def mafia():
n = int(input())
rounds = list(map(int, str(input()).split()))
count = 0
rounds.sort(reverse=True)
for i in range(n-1, 0, -1):
if rounds[i-1] == rounds[i]:
continue
else:
steps = n - i
count += steps*(rounds[i-1] - rounds[i])
rounds[0] -= count
if rounds[0] > 0:
k = ceil(rounds[0] / (n - 1))
count += n * k
rounds[0] += k * (1 - n)
if rounds[0] <= 0:
count += rounds[0]
print(count)
if __name__ == '__main__':
mafia()
``` | instruction | 0 | 103,201 | 19 | 206,402 |
Yes | output | 1 | 103,201 | 19 | 206,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=1000000007
def value():return tuple(map(float,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def possible(x):
have=0
for i in a:
have+=x-i
# print(have)
return have>=x
n=Int()
a=sorted(array())
# print(a)
low=max(a)
high=10**15
ans=0
while(low<=high):
mid=low+(high-low)//2
if(possible(mid)):
ans=mid
high=mid-1
else:
low=mid+1
print(ans)
``` | instruction | 0 | 103,202 | 19 | 206,404 |
Yes | output | 1 | 103,202 | 19 | 206,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
n = int(input())
a = [int(s) for s in input().split()]
sum, b = 0, 0
for i in a:
b = max(b, i)
sum += i
l, r = 1, 2 * b
while l < r:
m = (l + r) // 2
if n * m - sum >= m:
r = m
else:
l = m + 1
print(l)
``` | instruction | 0 | 103,203 | 19 | 206,406 |
No | output | 1 | 103,203 | 19 | 206,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
n = int(input())
sa = sum((int(x) for x in input().split()))
res = (sa+(n-2))//(n-1)
print(res)
``` | instruction | 0 | 103,204 | 19 | 206,408 |
No | output | 1 | 103,204 | 19 | 206,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
n = int(input())
ar = list(map(int,input().split()))
sm = sum(ar)
mnr = (sm - 1) // (n-1) + 1
print(mnr)
``` | instruction | 0 | 103,205 | 19 | 206,410 |
No | output | 1 | 103,205 | 19 | 206,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Submitted Solution:
```
v = int(input())
l = list(map(int, input().split()))
c = sum(l)
if c % (v - 1) == 0:
print(c // (v - 1))
else:
print(c // (v - 1) + 1)
``` | instruction | 0 | 103,206 | 19 | 206,412 |
No | output | 1 | 103,206 | 19 | 206,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
Input
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
Output
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
Examples
Input
5
1 2
1 3
2 4
2 5
Output
3 2
Input
6
1 2
1 3
3 4
1 5
5 6
Output
3 3
Note
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3. | instruction | 0 | 103,274 | 19 | 206,548 |
Tags: dfs and similar, dp, math, trees
Correct Solution:
```
import sys
fin = sys.stdin
n = int(fin.readline())
ut = [-1] * n
vc = [[] for i in range(0, n)]
cvc = [[] for i in range(0, n)]
nr = [0] * n
for i in range(0, n - 1):
a, b = [int(number) for number in fin.readline().split()]
a -= 1
b -= 1
vc[a].append(b)
cvc[b].append(a)
nr[a] += 1
size = [0] * n
mx = [0] * n
def nonzero(x):
if not x:
return 0
return 1
stk = []
size = [0] * n
for i in range(0, n):
if nr[i] == 0:
stk.append(i)
size[i] = 1
order = []
while nonzero(stk):
x = stk.pop(-1)
order.append(x)
for p in cvc[x]:
nr[p] -= 1
if nr[p] == 0:
stk.append(p)
h = [0] * n
for i in range(n - 1, -1, -1):
x = order[i]
for p in vc[x]:
h[p] = h[x] + 1
#parcurg
for x in order:
for p in vc[x]:
size[x] += size[p]
#maximum
def solv(tp, mx):
for x in order:
if h[x] % 2 == tp:
r = 999999999
for p in vc[x]:
r = min(r, size[p] - mx[p] + 1)
if r == 999999999:
r = 1
mx[x] = r
else:
r = 0
for p in vc[x]:
r += size[p] - mx[p]
mx[x] = r + 1
solv(0, mx)
print(size[0] - mx[0] + 1, end = ' ')
solv(1, mx)
print(size[0] - mx[0] + 1)
``` | output | 1 | 103,274 | 19 | 206,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
Input
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
Output
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
Examples
Input
5
1 2
1 3
2 4
2 5
Output
3 2
Input
6
1 2
1 3
3 4
1 5
5 6
Output
3 3
Note
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3. | instruction | 0 | 103,275 | 19 | 206,550 |
Tags: dfs and similar, dp, math, trees
Correct Solution:
```
import sys
input = sys.stdin.readline
def solve():
n = int(input())
g = [[] for i in range(n+1)]
for i in range(1, n):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
q = [1]
d = [None]*(n+1)
d[1] = 0
i = 0
while i < len(q):
x = q[i]
#print(x)
i += 1
for v in g[x]:
if d[v] is None:
q.append(v)
d[v] = d[x] + 1
g[v].remove(x)
m = [0]*(n+1)
M = [0]*(n+1)
cnt = 0
for i in range(len(q)-1,-1,-1):
x = q[i]
if len(g[x]) == 0:
m[x] = 1
M[x] = 1
cnt += 1
elif d[x] % 2 == 0:
c = 0
C = int(1e9)
for v in g[x]:
c += m[v]
for v in g[x]:
C = min(C, M[v])
m[x] = c
M[x] = C
else:
c = int(1e9)
C = 0
for v in g[x]:
c = min(c, m[v])
for v in g[x]:
C += M[v]
m[x] = c
M[x] = C
print(cnt + 1 - M[x], m[1])
solve()
``` | output | 1 | 103,275 | 19 | 206,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
Input
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
Output
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
Examples
Input
5
1 2
1 3
2 4
2 5
Output
3 2
Input
6
1 2
1 3
3 4
1 5
5 6
Output
3 3
Note
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def solve():
n = int(input())
g = [[] for i in range(n+1)]
for i in range(1, n):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
q = [1]
d = [None]*(n+1)
d[1] = 0
i = 0
while i < len(q):
x = q[i]
#print(x)
i += 1
for v in g[x]:
if d[v] is None:
q.append(v)
d[v] = d[x] + 1
g[v].remove(x)
m = [0]*(n+1)
M = [0]*(n+1)
cnt = 0
for i in range(len(q)-1,-1,-1):
x = q[i]
if len(g[x]) == 0:
m[x] = 1
cnt += 1
elif d[x] % 2 == 0:
c = 0
C = int(1e9)
for v in g[x]:
c += m[v]
for v in g[x]:
C = min(C, M[v])
m[x] = c
M[x] = C
else:
c = int(1e9)
C = 0
for v in g[x]:
c = min(c, m[v])
for v in g[x]:
C += M[v]
m[x] = c
M[x] = C
print(cnt + 1 - M[x], m[1])
solve()
``` | instruction | 0 | 103,276 | 19 | 206,552 |
No | output | 1 | 103,276 | 19 | 206,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
Input
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
Output
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
Examples
Input
5
1 2
1 3
2 4
2 5
Output
3 2
Input
6
1 2
1 3
3 4
1 5
5 6
Output
3 3
Note
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
Submitted Solution:
```
import sys
fin = sys.stdin
n = int(fin.readline())
ut = [-1] * n
vc = [[] for i in range(0, n)]
for i in range(0, n - 1):
a, b = [int(number) for number in fin.readline().split()]
a -= 1
b -= 1
vc[a].append(b)
size = [0] * n
mx = [0] * n
def parc(x):
global size
r = 0
for p in vc[x]:
parc(p)
r += size[p]
size[x] = r
if size[x] == 0:
size[x] = 1
parc(0)
#maximum
def solv1(x, h, tp):
global mx
if not vc[x]:
mx[x] = 1
return
if h % 2 == tp:
r = 999999999
for p in vc[x]:
solv1(p, h + 1, tp)
r = min(r, size[p] - mx[p] + 1)
mx[x] = r
else:
r = 0
for p in vc[x]:
solv1(p, h + 1, tp)
r += size[p] - mx[p]
mx[x] = r + 1
solv1(0, 0, 0)
print(mx)
print(size[0] - mx[0] + 1, end = ' ')
solv1(0, 0, 1)
print(size[0] - mx[0] + 1)
``` | instruction | 0 | 103,277 | 19 | 206,554 |
No | output | 1 | 103,277 | 19 | 206,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
Input
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
Output
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
Examples
Input
5
1 2
1 3
2 4
2 5
Output
3 2
Input
6
1 2
1 3
3 4
1 5
5 6
Output
3 3
Note
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def solve():
n = int(input())
g = [[] for i in range(n+1)]
for i in range(1, n):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
q = [1]
d = [None]*(n+1)
d[1] = 0
i = 0
while i < len(q):
x = q[i]
#print(x)
i += 1
for v in g[x]:
if d[v] is None:
q.append(v)
d[v] = d[x] + 1
g[v].remove(x)
m = [0]*(n+1)
M = [0]*(n+1)
cnt = 0
for i in range(len(q)-1,-1,-1):
x = q[i]
if len(g[x]) == 0:
m[x] = 1
cnt += 1
elif d[x] % 2 == 0:
c = 0
C = int(1e9)
for v in g[x]:
c += m[v]
for v in g[x]:
C = min(C, m[v])
m[x] = c
M[x] = C
else:
c = int(1e9)
C = 0
for v in g[x]:
c = min(c, m[v])
for v in g[x]:
C += M[v]
m[x] = c
M[x] = C
print(cnt + 1 - M[x], m[1])
solve()
``` | instruction | 0 | 103,278 | 19 | 206,556 |
No | output | 1 | 103,278 | 19 | 206,557 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,427 | 19 | 206,854 |
"Correct Solution:
```
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)
ans = 0
def ind(a, b):
t = sorted((a, b))
return t[0] * (N+1) + t[1]
def inv(i):
b = i % (N+1)
a = i // (N + 1)
return a, b
dp = [-1] * (N+1)**2
dp[ind(A[0],A[1])] = 0
current_max = 0
dp1 = [-1] * (N+1)
dp1[A[0]] = 0
dp1[A[1]] = 0
ans = 0
for i in range(0, N):
a = A[2+3*i]
b = A[2+3*i+1]
c = A[2+3*i+2]
#print(a,b,c)
if a == b == c:
ans+=1
continue
dp_new = {}
for x, y, z in [(a, b, c), (b, c, a), (c, a, b)]:
# choose 1 (x), leave y, z
i = ind(y, z)
if i not in dp_new:
dp_new[i] = 0
dp_new[i] = max(dp_new[i], current_max)
if dp[ind(x, x)] >= 0:
dp_new[i] = max(dp_new[i], dp[ind(x, x)] + 1)
# choose 2 (x, y), leave t, z
for t in range(1, N+1):
i = ind(t,z)
# Not making three
if dp1[t] >= 0:
if i not in dp_new:
dp_new[i]=0
dp_new[i] = max(dp_new[i], dp1[t])
# Making three (x==y)
if x == y and dp[ind(t, x)] >= 0:
if i not in dp_new:
dp_new[i] = 0
dp_new[i] = max(dp_new[i], dp[ind(t, x)] + 1)
#print(a,b,c)
#print({inv(i):v for i, v in dp_new.items() })
for i, s in dp_new.items():
dp[i] = max(dp[i], s)
ta, tb = inv(i)
dp1[ta] = max(dp1[ta], s)
dp1[tb] = max(dp1[tb], s)
current_max = max(current_max, s)
#print(doubles)
print(ans + max(dp1))
``` | output | 1 | 103,427 | 19 | 206,855 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,428 | 19 | 206,856 |
"Correct Solution:
```
from collections import deque
def upd(a, b, v):
global N
dp[a][b] = max(dp[a][b], v)
dp[b][a] = max(dp[b][a], v)
dp[N][a] = max(dp[N][a], v)
dp[N][b] = max(dp[N][b], v)
dp[a][N] = max(dp[a][N], v)
dp[b][N] = max(dp[b][N], v)
dp[N][N] = max(dp[N][N], v)
N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))
INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0
q = deque()
for i in range(2, 3 * N - 1, 3):
x, y, z = A[i], A[i + 1], A[i + 2]
if x == y == z: # x, y, z 3文字すべて等しいとき
base += 1
else:
for _ in range(3):
# a, b, x, y, z のうち, b, y, z を消すとき
for k in range(N):
v = dp[k][N]
if y == z:
v = max(v, dp[k][y] + 1)
q.append((k, x, v))
# a, b, x, y, z のうち, a, b, z を消すとき
v = max(dp[z][z] + 1, dp[N][N])
q.append((x, y, v))
# x, y, z をローテート
x, y, z = z, x, y
while q:
a, b, v = q.popleft()
upd(a, b, v)
l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)
``` | output | 1 | 103,428 | 19 | 206,857 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,429 | 19 | 206,858 |
"Correct Solution:
```
from typing import List
class DP:
N: int
table: List[List[int]]
max_all: int
max_arr: List[int]
inc: int
def __init__(self, N: int, x: int, y: int):
self.N = N
self.table = [[-(1 << 28)] * (N + 1) for _ in range(N + 1)]
self.table[x][y] = self.table[y][x] = 0
self.max_all = 0
self.max_arr = [-(1 << 28)] * (N + 1)
self.max_arr[x] = self.max_arr[y] = 0
self.inc = 0
def update(self, p: int, q: int, r: int) -> None:
if p == q == r:
self.inc += 1
return
npqr = self._update_pqr(p, q, r)
nqrp = self._update_pqr(q, r, p)
nrpq = self._update_pqr(r, p, q)
npq = self._update_pq(p, q)
nqr = self._update_pq(q, r)
nrp = self._update_pq(r, p)
np = self._update_p(p)
nq = self._update_p(q)
nr = self._update_p(r)
pq, qr, rp = None, None, None
if p == q:
pq = self._update_pair(p, r)
if q == r:
qr = self._update_pair(q, p)
if r == p:
rp = self._update_pair(r, q)
self._update_maximum(q, r, npqr)
self._update_maximum(r, p, nqrp)
self._update_maximum(p, q, nrpq)
self._update_maximum(p, q, npq)
self._update_maximum(q, r, nqr)
self._update_maximum(r, p, nrp)
for k in range(self.N + 1):
self._update_maximum(k, p, np[k])
self._update_maximum(k, q, nq[k])
self._update_maximum(k, r, nr[k])
if pq:
for k in range(self.N + 1):
self._update_maximum(k, r, pq[k])
if qr:
for k in range(self.N + 1):
self._update_maximum(k, p, qr[k])
if rp:
for k in range(self.N + 1):
self._update_maximum(k, q, rp[k])
def _update_maximum(self, x: int, y: int, v: int) -> None:
self.table[x][y] = self.table[y][x] = max(self.table[x][y], v)
self.max_all = max(self.max_all, v)
self.max_arr[x] = max(self.max_arr[x], v)
self.max_arr[y] = max(self.max_arr[y], v)
def _update_pair(self, p: int, q: int) -> List[int]:
return [max(self.table[k][q], self.table[k][p] + 1, self.table[p][k] + 1) for k in range(self.N + 1)]
def _update_pqr(self, p: int, q: int, r: int) -> int:
return max(self.table[q][r], self.table[p][p] + 1)
def _update_pq(self, p: int, q: int) -> int:
return max(self.table[p][q], self.max_all)
def _update_p(self, p) -> List[int]:
return [max(self.table[p][k], self.table[k][p], self.max_arr[k]) for k in range(self.N + 1)]
def ans(self):
return self.max_all + self.inc
N = int(input())
A = list(map(int, input().split())) + [0, 0]
dp = DP(N, A[0], A[1])
for i in range(N):
Z = A[3 * i + 2: 3 * i + 5]
dp.update(Z[0], Z[1], Z[2])
# print("out", dp.table, dp.max_all, dp.max_arr)
print(dp.ans())
``` | output | 1 | 103,429 | 19 | 206,859 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,430 | 19 | 206,860 |
"Correct Solution:
```
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit
INF = 1 << 50
def run():
N, *A_ = map(int, read().split())
dp = [[-INF] * (N+1) for _ in range(N+1)]
dp_max = [-INF] * (N+1)
a, b = A_[:2]
dp[a][b] = dp[b][a] = dp_max[a] = dp_max[b] = 0
triplets = 0
chunks = []
for i in range(N-1):
x,y,z = A_[3*i+2 : 3*i+5]
if x == y == z:
triplets+=1
else:
chunks.append((x,y,z))
for x,y,z in chunks:
cache = []
# transition : a = x = y
if y == z:
x,y,z = y,z,x
elif z == x:
x,y,z = z,x,y
if x == y:
for i in range(1, N+1):
cache.append((z, i, dp[x][i] + 1))
# trainsition : a = b = x
cache.append((y, z, dp[x][x] + 1))
cache.append((z, x, dp[y][y] + 1))
cache.append((x, y, dp[z][z] + 1))
# trainsition : only fill next
MAX = max(dp_max)
cache.append((x, y, MAX))
cache.append((y, z, MAX))
cache.append((z, x, MAX))
for i in range(1, N+1):
cache.append((i, x, dp_max[i]))
cache.append((i, y, dp_max[i]))
cache.append((i, z, dp_max[i]))
# Update
for a, b, value in cache:
dp[a][b] = dp[b][a] = max(value, dp[a][b])
dp_max[a] = max(dp_max[a], dp[a][b])
dp_max[b] = max(dp_max[b], dp[a][b])
a = A_[-1]
dp[a][a] += 1
dp_max[a] = max(dp[a][a], dp_max[a])
print(max(dp_max) + triplets)
#print(chunks)
if __name__ == "__main__":
run()
``` | output | 1 | 103,430 | 19 | 206,861 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,431 | 19 | 206,862 |
"Correct Solution:
```
n = int(input())
a = [*map(int, input().split())]
for i in range(n * 3):
a[i] -= 1
INF = 1<<30
dp = [[-INF] * n for i in range(n)]
mx = [-INF] * n
mx_all = -INF
add_all = 0
dp[a[0]][a[1]] = dp[a[1]][a[0]] = 0
mx[a[0]] = mx[a[1]] = 0
mx_all = 0
for i in range(n - 1):
x, y, z = sorted(a[i * 3 + 2 : i * 3 + 5])
update = []
# パターン1
if x == y == z:
add_all += 1
continue
# パターン2
if x == y:
for j in range(n):
update.append((j, z, dp[j][x] + 1))
if y == z:
for j in range(n):
update.append((j, x, dp[j][y] + 1))
# パターン3
update.append((y, z, dp[x][x] + 1))
update.append((x, z, dp[y][y] + 1))
update.append((x, y, dp[z][z] + 1))
# パターン2-2
for j in range(n):
update.append((j, x, mx[j]))
update.append((j, y, mx[j]))
update.append((j, z, mx[j]))
# パターン3-2
update.append((y, z, mx_all))
update.append((x, z, mx_all))
update.append((x, y, mx_all))
# in-place にするために更新を遅延させる
for j, k, val in update:
if dp[j][k] < val:
dp[j][k] = dp[k][j] = val
if mx[j] < val:
mx[j] = val
if mx[k] < val:
mx[k] = val
if mx_all < val:
mx_all = val
# 最後の 1 回
if mx_all < dp[a[-1]][a[-1]] + 1:
mx_all = dp[a[-1]][a[-1]] + 1
print(mx_all + add_all)
``` | output | 1 | 103,431 | 19 | 206,863 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,432 | 19 | 206,864 |
"Correct Solution:
```
N = int(input())
A = list(map(int,input().split()))
INF = 2*N
DP = [[-INF]*(N+1) for _ in range(N+1)]
M = 0
rowM = [-INF]*(N+1)
def chmax(i,j,k):
DP[i][j] = max(k,DP[i][j])
DP[j][i] = max(k,DP[j][i])
rowM[i] = max(rowM[i],DP[i][j])
rowM[j] = max(rowM[j],DP[i][j])
global M
M = max(M,k)
ans = 0
DP[A[0]][A[1]] = 0
DP[A[1]][A[0]] = 0
rowM[A[0]] = 0
rowM[A[1]] = 0
for i in range(N-1):
new = A[3*i+2:3*i+5]
change = []
if len(set(new)) == 1:
ans += 1
continue
if len(set(new)) == 2:
for j in range(3):
if new.count(new[j]) == 2:
X = new[j]
else:
Y = new[j]
for j in range(1,N+1):
change.append((j,Y,DP[X][j]+1))
X,Y,Z = new
change.append((X,Y,DP[Z][Z]+1))
change.append((Y,Z,DP[X][X]+1))
change.append((X,Z,DP[Y][Y]+1))
change.append((X,Y,M))
change.append((X,Z,M))
change.append((Y,Z,M))
for j in range(1,N+1):
for k in range(3):
change.append((j,new[k],rowM[j]))
for j,k,l in change:
chmax(j,k,l)
M = max(M,DP[A[-1]][A[-1]]+1)
print(ans+M)
``` | output | 1 | 103,432 | 19 | 206,865 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,433 | 19 | 206,866 |
"Correct Solution:
```
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
B = A[i*3+2:i*3+5]
cnt = len(set(B))
if cnt == 1:
ans += 1
elif cnt == 2:
a1 = B[0]^B[1]^B[2]
a2 = (sum(B) - a1) // 2
#
# X[a1], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(ma, X[a2][a2], X[a1][a1] + 1)
# X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, MA[j]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
#
X[a2] = [max(X[a2][j], MA[j]) for j in range(N+1)]
MA = [max(MA[j], X[a1][j], X[a2][j]) for j in range(N+1)]
MA[a1] = max(MA[a1], max(X[a1]), X[a2][a1])
MA[a2] = max(MA[a2], X[a1][a2], X[a2][a2])
ma = max(ma, MA[a1], MA[a2])
else:
a, b, c = B
X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
X[a] = [max(X[a][j], MA[j]) for j in range(N+1)]
X[b] = [max(X[b][j], MA[j]) for j in range(N+1)]
X[c] = [max(X[c][j], MA[j]) for j in range(N+1)]
MA[a] = max(MA[a], max(X[a]))
MA[b] = max(MA[b], X[a][b], max(X[b]))
MA[c] = max(MA[c], X[a][c], X[b][c], max(X[c]))
MA = [max(MA[j], X[a][j], X[b][j], X[c][j]) for j in range(N+1)]
ma = max(ma, MA[a], MA[b], MA[c])
print(ans + max(ma, max(max(x) for x in X)))
``` | output | 1 | 103,433 | 19 | 206,867 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3 | instruction | 0 | 103,434 | 19 | 206,868 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
ANS=0
B=[A[0],A[1]]
C=[]
D=A[-1]
for i in range(N-1):
if A[i*3+2]==A[i*3+3]==A[i*3+4]:
ANS+=1
else:
C.append((A[i*3+2],A[i*3+3],A[i*3+4]))
DP=[[-1]*(N+1) for i in range(N+1)]
MAXDP=[-1]*(N+1)
SCMAX=0
DP[B[0]][B[1]]=DP[B[1]][B[0]]=0
MAXDP[B[0]]=0
MAXDP[B[1]]=0
for x,y,z in C:
#for d in DP:
# print(*d)
#print()
#print(x,y,z)
#print()
NLIST=[]
SCMAX2=0
NMAXDP=[i for i in MAXDP]
xy=max(0,DP[x][y],DP[z][z]+1)
yz=max(0,DP[y][z],DP[x][x]+1)
zx=max(0,DP[z][x],DP[y][y]+1)
if x==y:
for i in range(N+1):
if DP[i][x]!=-1:
NLIST.append((i,z,max(DP[i][z],DP[i][x]+1)))
#DP[i][z]=DP[z][i]=max(DP[i][z],DP[i][x]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][z],DP[z][i])
NMAXDP[z]=max(NMAXDP[z],DP[i][z],DP[z][i])
if y==z:
for i in range(N+1):
if DP[i][y]!=-1:
NLIST.append((i,x,max(DP[i][x],DP[i][y]+1)))
#DP[i][x]=DP[x][i]=max(DP[i][x],DP[i][y]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][x],DP[x][i])
NMAXDP[x]=max(NMAXDP[x],DP[i][x],DP[x][i])
if z==x:
for i in range(N+1):
if DP[i][x]!=-1:
NLIST.append((i,y,max(DP[i][y],DP[i][x]+1)))
#DP[i][y]=DP[y][i]=max(DP[i][y],DP[i][x]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][y],DP[y][i])
NMAXDP[y]=max(NMAXDP[y],DP[i][y],DP[y][i])
for i,j,sc in NLIST:
DP[i][j]=DP[j][i]=max(DP[i][j],sc)
SCMAX2=max(SCMAX,DP[i][j])
#for d in DP:
# print(*d)
#print()
for i in range(N+1):
DP[i][x]=DP[x][i]=max(DP[i][x],MAXDP[i])
DP[i][y]=DP[y][i]=max(DP[i][y],MAXDP[i])
DP[i][z]=DP[z][i]=max(DP[i][z],MAXDP[i])
SCMAX2=max(SCMAX2,DP[i][x],DP[i][y],DP[i][z])
NMAXDP[i]=max(NMAXDP[i],DP[i][x],DP[i][y],DP[i][z])
NMAXDP[x]=max(NMAXDP[x],DP[i][x])
NMAXDP[y]=max(NMAXDP[y],DP[i][y])
NMAXDP[z]=max(NMAXDP[z],DP[i][z])
DP[x][y]=DP[y][x]=max(SCMAX,xy)
DP[y][z]=DP[z][y]=max(SCMAX,yz)
DP[z][x]=DP[x][z]=max(SCMAX,zx)
SCMAX2=max(SCMAX2,DP[x][y],DP[y][z],DP[z][x])
MAXDP=NMAXDP
MAXDP[x]=max(MAXDP[x],DP[x][y],DP[y][x],DP[z][x],DP[x][z])
MAXDP[y]=max(MAXDP[y],DP[x][y],DP[y][x],DP[z][y],DP[y][z])
MAXDP[z]=max(MAXDP[z],DP[x][z],DP[z][x],DP[z][y],DP[y][z])
SCMAX=SCMAX2
#for d in DP:
# print(*d)
DP[D][D]+=1
DPANS=0
for d in DP:
DPANS=max(max(d),DPANS)
print(ANS+DPANS)
``` | output | 1 | 103,434 | 19 | 206,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input().strip())+2
A = list(map(int,input().split()))+[N-1,N]
INF = N
dp = [[-INF for j in range(N)] for i in range(N)]
dpmax = -INF
dpmaxj = [-INF for i in range(N)]
score = 0
a,b,c,d,e = [i-1 for i in sorted(A[:5])]
if a == b == c:
dp[d][e] = dp[e][d] = dpmaxj[d] = dpmaxj[e] = dpmax = 1
elif b == c == d:
dp[a][e] = dp[e][a] = dpmaxj[a] = dpmaxj[e] = dpmax = 1
elif c == d == e:
dp[a][b] = dp[b][a] = dpmaxj[a] = dpmaxj[b] = dpmax = 1
else:
L = [a,b,c,d,e]
for i in range(5):
for j in range(i+1,5):
p,q = L[i],L[j]
dp[p][q] = dp[q][p] = dpmaxj[p] = dpmaxj[q] = 0
dpmax = 0
def update(i,j):
dpmaxj[i] = max(dpmaxj[i],dp[i][j])
dpmaxj[j] = max(dpmaxj[j],dp[i][j])
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxj = dpmaxj[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
prevdpka = [dp[i][a] for i in range(N)]
prevdpla = [dp[a][i] for i in range(N)]
prevdpkb = [dp[i][b] for i in range(N)]
prevdplb = [dp[b][i] for i in range(N)]
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][c],max(prevdpka[k],prevdpla[k]) + 1)
dp[c][k] = max(dp[c][k],max(prevdpka[k],prevdpla[k]) + 1)
dpmax = max(dpmax,dp[k][c],dp[c][k])
update(k,c)
update(c,k)
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][b],max(prevdpka[k],prevdpla[k]) + 1)
dp[b][k] = max(dp[b][k],max(prevdpka[k],prevdpla[k]) + 1)
dpmax = max(dpmax,dp[k][b],dp[b][k])
update(k,b)
update(b,k)
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][a],max(prevdpkb[k],prevdplb[k]) + 1)
dp[a][k] = max(dp[a][k],max(prevdpkb[k],prevdplb[k]) + 1)
dpmax = max(dpmax,dp[k][a],dp[a][k])
update(k,a)
update(a,k)
dp[b][c] = max(dp[b][c],dpd[a] + 1)
dp[c][b] = max(dp[c][b],dpd[a] + 1)
dp[a][c] = max(dp[a][c],dpd[b] + 1)
dp[c][a] = max(dp[c][a],dpd[b] + 1)
dp[a][b] = max(dp[a][b],dpd[c] + 1)
dp[b][a] = max(dp[b][a],dpd[c] + 1)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b],dp[c][b],dp[c][a],dp[b][a])
dp[a][b] = max(0,dp[a][b],prevdpmax)
dp[b][a] = max(0,dp[b][a],prevdpmax)
dp[b][c] = max(0,dp[b][c],prevdpmax)
dp[c][b] = max(0,dp[c][b],prevdpmax)
dp[a][c] = max(0,dp[a][c],prevdpmax)
dp[c][a] = max(0,dp[c][a],prevdpmax)
update(a,b)
update(a,c)
update(b,a)
update(b,c)
update(c,a)
update(c,b)
for j in range(N):
dp[j][a] = max(dp[j][a],prevdpmaxj[j])
dp[j][b] = max(dp[j][b],prevdpmaxj[j])
dp[j][c] = max(dp[j][c],prevdpmaxj[j])
dp[a][j] = max(dp[a][j],prevdpmaxj[j])
dp[b][j] = max(dp[b][j],prevdpmaxj[j])
dp[c][j] = max(dp[c][j],prevdpmaxj[j])
update(j,a)
update(j,b)
update(j,c)
update(a,j)
update(b,j)
update(c,j)
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
``` | instruction | 0 | 103,435 | 19 | 206,870 |
Yes | output | 1 | 103,435 | 19 | 206,871 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
B = A[i*3+2:i*3+5]
cnt = len(set(B))
if cnt == 1:
ans += 1
elif cnt == 2:
a1 = B[0]^B[1]^B[2]
a2 = (sum(B) - a1) // 2
X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, MA[j]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
X[a2] = [max(X[a2][j], MA[j]) for j in range(N+1)]
MA = [max(m, x1, x2) for m, x1, x2 in zip(MA, X[a1], X[a2])]
MA[a1] = max(MA[a1], max(X[a1]), X[a2][a1])
MA[a2] = max(MA[a2], X[a1][a2], X[a2][a2])
ma = max(ma, MA[a1], MA[a2])
else:
a, b, c = B
# X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
X[a][b], X[a][c], X[b][c] = max(ma, X[c][c] + 1), max(ma, X[b][b] + 1), max(ma, X[a][a] + 1)
X[a] = [max(x, m) for x, m in zip(X[a], MA)]
X[b] = [max(x, m) for x, m in zip(X[b], MA)]
X[c] = [max(x, m) for x, m in zip(X[c], MA)]
MA[a] = max(MA[a], max(X[a]))
MA[b] = max(MA[b], X[a][b], max(X[b]))
MA[c] = max(MA[c], X[a][c], X[b][c], max(X[c]))
MA = [max(m, xa, xb, xc) for m, xa, xb, xc in zip(MA, X[a], X[b], X[c])]
ma = max(ma, MA[a], MA[b], MA[c])
print(ans + max(ma, max(max(x) for x in X)))
``` | instruction | 0 | 103,436 | 19 | 206,872 |
Yes | output | 1 | 103,436 | 19 | 206,873 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
def solve(n, aaa):
dp = [[-1] * n for _ in range(n)]
dp_max = [-1] * n
ans = 0
a0, a1 = sorted(aaa[:2])
dp[a0][a1] = 0
dp_max[a0] = 0
dp_max[a1] = 0
for k in range(n - 1):
a0, a1, a2 = sorted(aaa[2 + 3 * k:5 + 3 * k])
if a0 == a1 and a1 == a2:
ans += 1
continue
dp_max_max = max(dp_max)
update_tasks = []
if a0 == a1 or a1 == a2:
rev = a1 == a2
if rev:
a0, a2 = a2, a0
for b in range(n):
c, d = min(a0, b), max(a0, b)
e, f = min(a2, b), max(a2, b)
update_tasks.append((e, f, dp_max[b]))
if dp[c][d] != -1:
update_tasks.append((e, f, dp[c][d] + 1))
update_tasks.append((a0, a0, dp_max_max))
if dp[a2][a2] != -1:
update_tasks.append((a0, a0, dp[a2][a2] + 1))
if rev:
update_tasks.append((a2, a0, dp_max_max))
else:
update_tasks.append((a0, a2, dp_max_max))
else:
update_tasks.append((a0, a1, dp_max_max))
update_tasks.append((a0, a2, dp_max_max))
update_tasks.append((a1, a2, dp_max_max))
if dp[a0][a0] != -1:
update_tasks.append((a1, a2, dp[a0][a0] + 1))
if dp[a1][a1] != -1:
update_tasks.append((a0, a2, dp[a1][a1] + 1))
if dp[a2][a2] != -1:
update_tasks.append((a0, a1, dp[a2][a2] + 1))
for b in range(n):
b_max = dp_max[b]
for a in (a0, a1, a2):
c, d = min(a, b), max(a, b)
update_tasks.append((c, d, b_max))
for a, b, c in update_tasks:
dp[a][b] = max(dp[a][b], c)
dp_max[a] = max(dp_max[a], c)
dp_max[b] = max(dp_max[b], c)
a = aaa[-1]
dp[a][a] += 1
return max(map(max, dp)) + ans
n = int(input())
aaa = [a - 1 for a in map(int, input().split())]
ans = solve(n, aaa)
print(ans)
``` | instruction | 0 | 103,437 | 19 | 206,874 |
Yes | output | 1 | 103,437 | 19 | 206,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input().strip())+2
A = list(map(int,input().split()))+[N-1,N]
INF = N
dp = [[-INF for j in range(N)] for i in range(N)]
dpmax = -INF
dpmaxj = [-INF for i in range(N)]
score = 0
a,b,c,d,e = [i-1 for i in sorted(A[:5])]
if a == b == c:
dp[d][e] = dp[e][d] = dpmaxj[d] = dpmaxj[e] = dpmax = 1
elif b == c == d:
dp[a][e] = dp[e][a] = dpmaxj[a] = dpmaxj[e] = dpmax = 1
elif c == d == e:
dp[a][b] = dp[b][a] = dpmaxj[a] = dpmaxj[b] = dpmax = 1
else:
L = [a,b,c,d,e]
for i in range(5):
for j in range(i+1,5):
p,q = L[i],L[j]
dp[p][q] = dp[q][p] = dpmaxj[p] = dpmaxj[q] = 0
dpmax = 0
def update(i,j):
dpmaxj[i] = max(dpmaxj[i],dp[i][j])
dpmaxj[j] = max(dpmaxj[j],dp[i][j])
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxj = dpmaxj[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
prevdpka = [dp[i][a] for i in range(N)]
prevdpla = [dp[a][i] for i in range(N)]
prevdpkb = [dp[i][b] for i in range(N)]
prevdplb = [dp[b][i] for i in range(N)]
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][c],max(prevdpka[k],prevdpla[k]) + 1); update(k,c)
dp[c][k] = max(dp[c][k],max(prevdpka[k],prevdpla[k]) + 1); update(c,k)
dpmax = max(dpmax,dp[k][c],dp[c][k])
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][b],max(prevdpka[k],prevdpla[k]) + 1); update(k,b)
dp[b][k] = max(dp[b][k],max(prevdpka[k],prevdpla[k]) + 1); update(b,k)
dpmax = max(dpmax,dp[k][b],dp[b][k])
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][a],max(prevdpkb[k],prevdplb[k]) + 1); update(k,a)
dp[a][k] = max(dp[a][k],max(prevdpkb[k],prevdplb[k]) + 1); update(a,k)
dpmax = max(dpmax,dp[k][a],dp[a][k])
dp[b][c] = max(dp[b][c],dpd[a] + 1); update(b,c)
dp[c][b] = max(dp[c][b],dpd[a] + 1); update(c,b)
dp[a][c] = max(dp[a][c],dpd[b] + 1); update(a,c)
dp[c][a] = max(dp[c][a],dpd[b] + 1); update(c,a)
dp[a][b] = max(dp[a][b],dpd[c] + 1); update(a,b)
dp[b][a] = max(dp[b][a],dpd[c] + 1); update(b,a)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b],dp[c][b],dp[c][a],dp[b][a])
dp[a][b] = max(0,dp[a][b],prevdpmax); update(a,b)
dp[b][a] = max(0,dp[b][a],prevdpmax); update(b,a)
dp[b][c] = max(0,dp[b][c],prevdpmax); update(b,c)
dp[c][b] = max(0,dp[c][b],prevdpmax); update(c,b)
dp[a][c] = max(0,dp[a][c],prevdpmax); update(a,c)
dp[c][a] = max(0,dp[c][a],prevdpmax); update(c,a)
for j in range(N):
dp[j][a] = max(dp[j][a],prevdpmaxj[j]); update(j,a)
dp[j][b] = max(dp[j][b],prevdpmaxj[j]); update(j,b)
dp[j][c] = max(dp[j][c],prevdpmaxj[j]); update(j,c)
dp[a][j] = max(dp[a][j],prevdpmaxj[j]); update(a,j)
dp[b][j] = max(dp[b][j],prevdpmaxj[j]); update(b,j)
dp[c][j] = max(dp[c][j],prevdpmaxj[j]); update(c,j)
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
``` | instruction | 0 | 103,438 | 19 | 206,876 |
Yes | output | 1 | 103,438 | 19 | 206,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))
two = [0]*n
one = [0]*n
two[A[0]] += 1
two[A[1]] += 1
ans = 0
for i in range(n-1):
nxt = A[3*i+2:3*i+5]
nxt.sort()
if nxt[0] == nxt[1] == nxt[2]:
ans += 1
continue
if nxt[1] == nxt[2]:
nxt.reverse()
if nxt[0] == nxt[1]:
if one[nxt[0]] >= 1:
ans += 1
one = [0]*n
two[nxt[2]] += 1
continue
if two[nxt[0]] >= 1:
ans += 1
two[nxt[0]] -= 1
one = [0]*n
for i in range(n):
one[i] += two[i]
two = [0]*n
two[nxt[2]] += 1
continue
if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
else:
two[nxt[2]] += 1
two[nxt[0]] += 2
continue
if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[1]] += 1
two[nxt[2]] += 1
continue
if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[0]] += 1
two[nxt[2]] += 1
continue
if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[1]] += 1
two[nxt[0]] += 1
continue
for i in range(3):
two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
ans += 1
print(ans)
``` | instruction | 0 | 103,439 | 19 | 206,878 |
No | output | 1 | 103,439 | 19 | 206,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import numpy as np
n = int(input())
A = [int(i) for i in input().split()]
ans = 0
stack = 0
count = np.zeros(n)
for i in range(3*n):
stack += 1
count[A[i]-1] += 1
if count[A[i]-1] == 3 and stack < 6:
ans += 1
count[A[i]-1] = 0
stack -= 3
elif count[A[i]-1] == 3 and stack > 5:
count = np.zeros(n)
ans += 1
stack -= 3
print(ans)
``` | instruction | 0 | 103,440 | 19 | 206,880 |
No | output | 1 | 103,440 | 19 | 206,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
from collections import defaultdict
n = int(input())
A = list(map(int, input().split()))
remain = defaultdict(int)
must = defaultdict(int)
if n == 1:
if A[0] == A[1] and A[1] == A[2]:
print(1)
exit()
else:
print(0)
exit()
ans = 0
remain[A[0]] += 1
remain[A[1]] += 1
for i in range(1, n):
nex = defaultdict(int)
nex1 = A[3 * i - 1]
nex2 = A[3 * i]
nex3 = A[3 * i + 1]
if nex1 == nex2 and nex2 == nex3:
ans += 1
continue
elif nex1 == nex2:
if remain[nex1] >= 1:
remain[nex1] -= 1
ans += 1
remain[nex3] += 1
elif remain[nex3] >= 2:
remain[nex3] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 2
else:
remain[nex1] += 2
remain[nex3] += 1
elif nex2 == nex3:
if remain[nex2] >= 1:
remain[nex2] -= 1
ans += 1
remain[nex1] += 1
elif remain[nex1] >= 2:
remain[nex1] -= 2
ans += 1
remain = defaultdict(int)
remain[nex2] += 2
else:
remain[nex2] += 2
remain[nex1] += 1
elif nex3 == nex1:
if remain[nex1] >= 1:
remain[nex1] -= 1
ans += 1
remain[nex2] += 1
elif remain[nex2] >= 2:
remain[nex2] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 2
else:
remain[nex1] += 2
remain[nex2] += 1
else:
if remain[nex1] >= 2:
remain[nex1] -= 2
ans += 1
remain = defaultdict(int)
remain[nex2] += 1
remain[nex3] += 1
elif remain[nex2] >= 2:
remain[nex2] -= 2
ans += 1
remain = defaultdict(int)
remain[nex3] += 1
remain[nex1] += 1
elif remain[nex3] >= 2:
remain[nex3] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 1
remain[nex2] += 1
else:
remain[nex1] += 1
remain[nex2] += 1
remain[nex3] += 1
if remain[A[-1]] >= 2:
ans += 1
print(ans)
``` | instruction | 0 | 103,441 | 19 | 206,882 |
No | output | 1 | 103,441 | 19 | 206,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
# -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"
#############
# Functions #
#############
######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]
#####Shorten#####
def DD(arg): return defaultdict(arg)
#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if(len(kaijo_memo) > n): return kaijo_memo[n]
if(len(kaijo_memo) == 0): kaijo_memo.append(1)
while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
return gyaku_kaijo_memo[n]
def nCr(n,r):
if n == r: return 1
if n < r or r < 0: return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n-r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1: arr.append([temp, 1])
if arr==[]: arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
#####MakePrimes######
def make_primes(N):
max = int(math.sqrt(N))
seachList = [i for i in range(2,N+1)]
primeNum = []
while seachList[0] <= max:
primeNum.append(seachList[0])
tmp = seachList[0]
seachList = [i for i in seachList if i % tmp != 0]
primeNum.extend(seachList)
return primeNum
#####GCD#####
def gcd(a, b):
while b: a, b = b, a % b
return a
#####LCM#####
def lcm(a, b):
return a * b // gcd (a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n-1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X//n: return base_10_to_n(X//n, n)+[X%n]
return [X%n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))
def base_10_to_n_without_0(X, n):
X -= 1
if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
return [X%n]
#####IntLog#####
def int_log(n, a):
count = 0
while n>=a:
n //= a
count += 1
return count
#############
# Main Code #
#############
N = I()+2
if N == 3:
print(1)
exit()
A = IL()+[N-1,N]
dp = [[0 for j in range(N)] for i in range(N)]
dpmax = 0
dpmaxk = [0 for i in range(N)]
dpmaxl = [0 for i in range(N)]
score = 0
a,b,c,d,e = sorted(A[:5])
a -= 1
b -= 1
c -= 1
d -= 1
e -= 1
if a == b == c:
dp[d][e] = 1
dp[e][d] = 1
elif b == c == d:
dp[a][e] = 1
dp[e][a] = 1
elif c == d == e:
dp[a][b] = 1
dp[b][a] = 1
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxk = dpmaxk[:]
prevdpmaxl = dpmaxl[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][a],dp[a][k]) + 1
dp[c][k] = max(dp[k][a],dp[a][k]) + 1
dpmax = max(dpmax,dp[k][c])
dpmaxk[k] = max(dpmaxk[k],dp[k][c])
dpmaxl[c] = max(dpmaxl[c],dp[k][c])
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][a],dp[a][k]) + 1
dp[b][k] = max(dp[k][a],dp[a][k]) + 1
dpmax = max(dpmax,dp[k][b])
dpmaxk[k] = max(dpmaxk[k],dp[k][b])
dpmaxl[b] = max(dpmaxl[b],dp[k][b])
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][b],dp[b][k]) + 1
dp[a][k] = max(dp[k][b],dp[b][k]) + 1
dpmax = max(dpmax,dp[k][a])
dpmaxk[k] = max(dpmaxk[k],dp[k][a])
dpmaxl[a] = max(dpmaxl[a],dp[k][a])
dp[b][c] = max(dp[b][c],dpd[a] + 1)
dp[c][b] = max(dp[c][b],dpd[a] + 1)
dp[a][c] = max(dp[a][c],dpd[b] + 1)
dp[c][a] = max(dp[c][a],dpd[b] + 1)
dp[a][b] = max(dp[a][b],dpd[c] + 1)
dp[b][a] = max(dp[b][a],dpd[c] + 1)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b])
dpmaxk[b] = max(dpmaxk[b],dp[b][c],dp[b][a])
dpmaxl[c] = max(dpmaxl[c],dp[b][c],dp[a][c])
dpmaxk[a] = max(dpmaxk[a],dp[a][c],dp[a][b])
dpmaxl[c] = max(dpmaxl[c],dp[a][c],dp[b][c])
dpmaxk[a] = max(dpmaxk[a],dp[a][b],dp[a][c])
dpmaxl[b] = max(dpmaxl[b],dp[a][b],dp[c][b])
dp[a][b] = prevdpmax
dp[b][a] = prevdpmax
dp[b][c] = prevdpmax
dp[c][b] = prevdpmax
dp[a][c] = prevdpmax
dp[c][a] = prevdpmax
for j in range(N):
dp[j][a] = prevdpmaxk[j]
dp[j][b] = prevdpmaxk[j]
dp[j][c] = prevdpmaxk[j]
dp[a][j] = prevdpmaxl[j]
dp[b][j] = prevdpmaxl[j]
dp[c][j] = prevdpmaxl[j]
z = A[-1]-1
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
``` | instruction | 0 | 103,442 | 19 | 206,884 |
No | output | 1 | 103,442 | 19 | 206,885 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,511 | 19 | 207,022 |
"Correct Solution:
```
N, M = map(int, input().split())
if N > M:
N, M = M, N
if N == 1 and M == 1:
print(1)
elif N == 1:
print(M - 2)
else:
print(N * M - 2 * N - 2 * M + 4)
``` | output | 1 | 103,511 | 19 | 207,023 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,512 | 19 | 207,024 |
"Correct Solution:
```
n,m=map(int,input().split())
print(abs(m-2)*abs(n-2))
``` | output | 1 | 103,512 | 19 | 207,025 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,513 | 19 | 207,026 |
"Correct Solution:
```
n, m = [int(i) for i in input().split()]
print(abs((n - 2) * (m - 2)))
``` | output | 1 | 103,513 | 19 | 207,027 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,514 | 19 | 207,028 |
"Correct Solution:
```
N, M = map(int, input().split())
if N == M == 1:
print(1)
exit()
if N == 1 or M == 1:
print(max(0, N * M - 2))
else:
print(N * M - 2 * (N + M - 2))
``` | output | 1 | 103,514 | 19 | 207,029 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,515 | 19 | 207,030 |
"Correct Solution:
```
n,m = map(int,input().split())
if n==1 or m==1:
ans = max(max(n,m)-2,1)
else:
ans = (n-2)*(m-2)
print(ans)
``` | output | 1 | 103,515 | 19 | 207,031 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,516 | 19 | 207,032 |
"Correct Solution:
```
N,M = map(int,input().split())
if N == 1:
if M == 1:
print(1)
exit()
else:
print(M-2)
exit()
if M == 1:
print(N-2)
exit()
print((N-2)*(M-2))
``` | output | 1 | 103,516 | 19 | 207,033 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,517 | 19 | 207,034 |
"Correct Solution:
```
n,m=map(int,input().split())
if n==1 and m==1:
print(1)
elif n==1 and m>1:
print(m-2)
elif n>1 and m==1:
print(n-2)
else:
print((n-2)*(m-2))
``` | output | 1 | 103,517 | 19 | 207,035 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080 | instruction | 0 | 103,518 | 19 | 207,036 |
"Correct Solution:
```
n,m = map(int,input().split())
if n==1 and m==1:
ans = 1
elif min(n,m)==1:
ans = (n*m)-2
else:
ans = n*m-(n+m)*2+4
print(ans)
``` | output | 1 | 103,518 | 19 | 207,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.
We will perform the following operation once for each square contains a card:
* For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.
Constraints
* 1 \leq N,M \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of cards that face down after all the operations.
Examples
Input
2 2
Output
0
Input
1 7
Output
5
Input
314 1592
Output
496080
Submitted Solution:
```
N,M=map(int,input().split())
if min(N,M)>1:
print(M*N-2*(M+N-2))
elif N==1 and M==1:
print(1)
else:
print(max(M,N)-2)
``` | instruction | 0 | 103,519 | 19 | 207,038 |
Yes | output | 1 | 103,519 | 19 | 207,039 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.