message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,205 | 14 | 12,410 |
Tags: implementation
Correct Solution:
```
n = int(input())
alco = ["ABSINTH", "BEER", 'BRANDY', 'CHAMPAGNE', 'GIN', 'RUM', 'SAKE', 'TEQUILA', 'VODKA', 'WHISKEY', 'WINE']
count = 0
while n>0:
n-=1
s = input()
if s.isdecimal():
if int(s)<18:
count+=1
else:
if s in alco:
count+=1
print(count)
``` | output | 1 | 6,205 | 14 | 12,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,206 | 14 | 12,412 |
Tags: implementation
Correct Solution:
```
data=['ABSINTH',
'BEER',
'BRANDY',
'CHAMPAGNE',
'GIN',
'RUM',
'SAKE',
'TEQUILA',
'VODKA',
'WHISKEY',
'WINE',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',]
q = 0
p=int(input())
for _ in range(p):
n=input()
if n in data:
q+=1
print(q)
``` | output | 1 | 6,206 | 14 | 12,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,208 | 14 | 12,416 |
Tags: implementation
Correct Solution:
```
alc=["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"];p=0
for _ in range(int(input())):
x=input()
if len(x)<=2:
if ord(x[0])>=65: continue
else:
x=int(x)
if x<18: p+=1
else:
if x in alc: p+=1
print(p)
``` | output | 1 | 6,208 | 14 | 12,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,209 | 14 | 12,418 |
Tags: implementation
Correct Solution:
```
#!/usr/bin/env python3
num = int(input())
lis = ['ABSINTH', 'BEER', 'BRANDY', 'CHAMPAGNE', 'GIN', 'RUM', 'SAKE', 'TEQUILA', 'VODKA', 'WHISKEY', 'WINE']
cnt = 0
for i in range(num):
s = input()
if s in lis:
cnt += 1
#
continue
if s.isdigit():
s = int(s)
if(s<18):
cnt += 1
print(cnt)
``` | output | 1 | 6,209 | 14 | 12,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,210 | 14 | 12,420 |
Tags: implementation
Correct Solution:
```
def solve(arr):
count = 0
for i in arr:
if i.isnumeric():
if int(i) < 18:
count += 1
elif i in ["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]:
count += 1
return count
def main():
# vars = list(map(int, input().split(" ")))
n = int(input())
arr = []
for _ in range(n):
l = input()
arr.append(l)
# t = input()
# s = input()
# a = list(map(int, input().split(" ")))
# b = list(map(int, input().split(" ")))
# c = list(map(int, input().split(" ")))
# res = []
# for _ in range(n):
# arr = list(map(int, input().split(" ")))
# res.append(arr)
print(solve(arr))
# i = 0
# inputpath = 'input.txt'
# outPath = 'output.txt'
# with open(inputpath) as fp:
# line = fp.readline()
# cnt = 1
# while line:
# if cnt == 1:
# i = int(line)
# else:
# arr = list(map(int, line.split(" ")))
# res.append(arr)
# cnt += 1
# line = fp.readline()
# s = solve(res,i)
# with open(outPath, 'a') as out:
# out.write(str(s))
main()
``` | output | 1 | 6,210 | 14 | 12,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,211 | 14 | 12,422 |
Tags: implementation
Correct Solution:
```
a=["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]
ans=0
for i in range(int(input())):
x=input()
if x in a:
ans+=1
else:
try:
y=int(x)
if y<18:
ans+=1
except:
continue
print(ans)
``` | output | 1 | 6,211 | 14 | 12,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked. | instruction | 0 | 6,212 | 14 | 12,424 |
Tags: implementation
Correct Solution:
```
t = int(input())
alco = "ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE".replace(",","").split()
check = 0
for _ in range(t):
n = input()
check += int(n.isdigit() and int(n) < 18 or n in alco)
print(check)
``` | output | 1 | 6,212 | 14 | 12,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?
The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
Input
The first line contains an integer n (1 β€ n β€ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators.
Only the drinks from the list given above should be considered alcohol.
Output
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
Examples
Input
5
18
VODKA
COKE
19
17
Output
2
Note
In the sample test the second and fifth clients should be checked.
Submitted Solution:
```
n=int(input())
a="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
s="0123456789"
w={"ABSINTH","BEER", "BRANDY","CHAMPAGNE","GIN","RUM","SAKE","TEQUILA","VODKA","WHISKEY","WINE"}
c=0
for i in range(n):
i=input()
if i[0] in s and int(i)<18:
c=c+1
elif i[0] in a and i in w:
c=c+1
print(c)
``` | instruction | 0 | 6,215 | 14 | 12,430 |
Yes | output | 1 | 6,215 | 14 | 12,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody). | instruction | 0 | 6,274 | 14 | 12,548 |
Tags: flows, graphs
Correct Solution:
```
import sys
def solve():
n = int(sys.stdin.readline())
a = [0] + [int(i) for i in sys.stdin.readline().split()]
dp = [[0]*(n + 1) for i in range(n + 1)]
ans = 0
maxnum = [0] * (10**5 + 2)
maxmod = [0] * 7
for y in range(n + 1):
maxmod = [0] * 7
for ai in a:
maxnum[ai] = 0
for i in range(y):
maxmod[a[i] % 7] = max(maxmod[a[i] % 7], dp[i][y])
maxnum[a[i]] = max(maxnum[a[i]], dp[i][y])
for x in range(y + 1, n + 1):
dp[x][y] = max(maxmod[a[x] % 7], maxnum[a[x] + 1], maxnum[a[x] - 1], dp[0][y]) + 1
dp[y][x] = dp[x][y]
maxmod[a[x] % 7] = max(maxmod[a[x] % 7], dp[x][y])
maxnum[a[x]] = max(maxnum[a[x]], dp[x][y])
ans = max(ans, dp[x][y])
print(ans)
if __name__ == '__main__':
solve()
``` | output | 1 | 6,274 | 14 | 12,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody). | instruction | 0 | 6,275 | 14 | 12,550 |
Tags: flows, graphs
Correct Solution:
```
import sys
n = int(sys.stdin.readline())
a = [0] + [int(i) for i in sys.stdin.readline().split()]
dp = [[0]*(n + 1) for i in range(n + 1)]
ans = 0
maxnum = [0] * (10**5 + 2)
maxmod = [0] * 7
for y in range(n + 1):
maxmod = [0] * 7
for ai in a:
maxnum[ai] = 0
for i in range(y):
maxmod[a[i] % 7] = max(maxmod[a[i] % 7], dp[i][y])
maxnum[a[i]] = max(maxnum[a[i]], dp[i][y])
for x in range(y + 1, n + 1):
dp[x][y] = max(maxmod[a[x] % 7], maxnum[a[x] + 1], maxnum[a[x] - 1], dp[0][y]) + 1
dp[y][x] = dp[x][y]
maxmod[a[x] % 7] = max(maxmod[a[x] % 7], dp[x][y])
maxnum[a[x]] = max(maxnum[a[x]], dp[x][y])
ans = max(ans, dp[x][y])
print(ans)
``` | output | 1 | 6,275 | 14 | 12,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,333 | 14 | 12,666 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
n, k = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
t = [int(x) for x in input().split(' ')]
g = sum([a[x] for x in range(n) if t[x] == 1])
max_app_g = 0
app_g = 0
for i in range(n):
if t[i] == 0:
app_g += a[i]
if i - k >= 0 and t[i - k] == 0:
app_g -= a[i - k]
if app_g > max_app_g:
max_app_g = app_g
print(max_app_g + g)
``` | output | 1 | 6,333 | 14 | 12,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,334 | 14 | 12,668 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
def main():
n, k = map(int, input().split())
a = [int(c) for c in input().split()]
t = [int(c) for c in input().split()]
diff = 0
i = 0
s = 0
ii = jj = 0
while i < n:
ii += a[i]
if i - k >= 0:
ii -= a[i - k]
if t[i] == 1:
s += a[i]
jj += a[i]
if i - k >= 0 and t[i - k] == 1:
jj -= a[i - k]
diff = max(diff, ii - jj)
i += 1
print(s + diff)
if __name__ == '__main__':
main()
``` | output | 1 | 6,334 | 14 | 12,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,335 | 14 | 12,670 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
n, k = [int(x) for x in input().split()]
a = list(map(int, input().split()))
t = list(map(int, input().split()))
m = 0
s = 0
temp = 0
for i in range(n):
m = max(m, temp)
if i >= k:
ch = i - k
if t[ch] == 0:
temp -= a[ch]
if t[i] == 0:
temp += a[i]
if t[i] == 1:
s += a[i]
m = max(m, temp)
print(s + m)
``` | output | 1 | 6,335 | 14 | 12,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,336 | 14 | 12,672 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
a = input()
b = input()
c = input()
a = a.split(' ')
b = b.split(' ')
c = c.split(' ')
for i in range(len(a)):
a[i] = int(a[i])
for i in range(len(b)):
b[i] = int(b[i])
for i in range(len(c)):
c[i] = int(c[i])
def blower():
d = []
s = 0
x = 0
for i in range(a[1]):
if c[i] == 0:
s += b[i]
d += [s]
for i in range(a[1],a[0]):
if c[i-a[1]] == 0:
s -= b[i-a[1]]
if c[i] == 0:
s += b[i]
d += [s]
for i in range(a[0]):
if c[i] == 1:
x += b[i]
print(x + max(d))
blower()
``` | output | 1 | 6,336 | 14 | 12,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,337 | 14 | 12,674 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
(n, k) = map(int, input().split())
lst = []
for x in input().split():
lst.append(int(x))
array = []
for x in input().split():
array.append(int(x))
mass1 = [0]
for x in range(n):
mass1.append(mass1[-1] + lst[x])
mass2 = [0]
for x in range(n):
mass2.append(mass2[-1] + lst[x] * array[x])
ans = 0
for x in range(1, n - k + 2):
ans = max(ans, mass2[x - 1] + (mass1[x + k - 1] - mass1[x - 1]) + (mass2[-1] - mass2[x + k - 1]))
#print(ans, x)
print(ans)
``` | output | 1 | 6,337 | 14 | 12,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,338 | 14 | 12,676 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
n,k = map(int, input().split())
theorems = [int(i) for i in input().split()]
wake = [int(i) for i in input().split()]
acu = 0
for i in range(n):
if wake[i] == 1:
acu += theorems[i]
elif i < k:
acu += theorems[i]
ans = acu
j = k
ans = acu
for i in range(n-k):
if wake[i] == 0:
acu -= theorems[i]
if wake[j] == 0:
acu += theorems[j]
ans = max(ans,acu)
j += 1
print(ans)
``` | output | 1 | 6,338 | 14 | 12,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,339 | 14 | 12,678 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
#pyrival orz
import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def main():
try:
n, k = invr()
a = inlt()
b = inlt()
extra = 0
for i in range(k):
if not b[i]:
extra += a[i]
ans = 0
temp = 0
for i in range(n):
if b[i]:
ans += a[i]
if not b[i]:
temp += a[i]
if i > k - 1 and not b[i - k]:
temp -= a[i - k]
extra = max(extra, temp)
print(ans + extra)
except Exception as e:
print(e)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 6,339 | 14 | 12,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β ai during the i-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that <image> and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Input
The first line of the input contains two integer numbers n and k (1 β€ k β€ n β€ 105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains n integer numbers a1, a2, ... an (1 β€ ai β€ 104) β the number of theorems lecturer tells during the i-th minute.
The third line of the input contains n integer numbers t1, t2, ... tn (0 β€ ti β€ 1) β type of Mishka's behavior at the i-th minute of the lecture.
Output
Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.
Example
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
Note
In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | instruction | 0 | 6,340 | 14 | 12,680 |
Tags: data structures, dp, implementation, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
import math
import copy
import collections
from collections import deque
import heapq
import itertools
from collections import defaultdict
from collections import Counter
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = list(map(int,input().split()))
pre = [0]
for i in range(n):
if t[i]==0:
pre.append(pre[i]+a[i])
else:
pre.append(pre[i])
mx = 0
for i in range(n-k+1):
l = i
r = i+k
ans = pre[r]-pre[l]
mx = max(mx,ans)
ans = 0
for i in range(n):
if(t[i]==1):
ans+=a[i]
print(ans+mx)
``` | output | 1 | 6,340 | 14 | 12,681 |
Provide a correct Python 3 solution for this coding contest problem.
There is the word heuristics. It's a relatively simple approach that usually works, although there is no guarantee that it will work. The world is full of heuristics because it is simple and powerful.
Some examples of heuristics include: In an anime program, the popularity of a character is proportional to the total appearance time in the main part of the anime. Certainly this seems to hold true in many cases. However, it seems that I belong to the minority. Only characters with light shadows and mobs that glimpse in the background look cute.
It doesn't matter what other people think of your favorite character. Unfortunately, there are circumstances that cannot be said. Related products, figures and character song CDs, tend to be released with priority from popular characters. Although it is a commercial choice, fans of unpopular characters have a narrow shoulder.
Therefore, what is important is the popularity vote held by the production company of the anime program. Whatever the actual popularity, getting a lot of votes here opens the door to related products. You have to collect votes by any means.
For strict voting, you will be given one vote for each purchase of a related product. Whether or not this mechanism should be called strict is now being debated, but anyway I got the right to vote for L votes. There are a total of N characters to be voted on, and related products of K characters who have won more votes (in the case of the same vote, in dictionary order of names) will be planned. I have M characters in all, and I want to put as many characters as possible in the top K.
I first predicted how many votes each character would get (it's not difficult, I just assumed that a character's popularity is proportional to the sum of its appearance times). Given that this prediction is correct, who and how much should I cast this L-vote to ensure that more related products of my favorite characters are released?
Input
The input consists of multiple cases. Each case is given in the following format.
N M K L
name0 x0
..
..
..
nameN-1 xN-1
fav0
..
..
..
favM-1
The meanings of N, M, K, and L are as described in the problem statement.
namei is the name of the character and xi is the total number of votes for the i-th character.
favi represents the name of the character you like. These names are always different and are always included in the character's name input.
The end of the input is given by a line consisting of N = 0, M = 0, K = 0, and L = 0.
In addition, each value satisfies the following conditions
1 β€ N β€ 100,000
1 β€ M β€ N
1 β€ K β€ N
1 β€ L β€ 100,000,000
0 β€ xi β€ 100,000,000
namei contains only the alphabet and is 10 characters or less in length.
The number of test cases does not exceed 150.
It is also guaranteed that the number of cases where 20,000 β€ N does not exceed 20.
Judge data is large, so it is recommended to use fast input.
Output
Output in one line how many of the M characters can be included in the top K.
Example
Input
4 1 3 4
yskwcnt 16
akzakr 7
tsnukk 12
fnmyi 13
akzakr
4 1 3 5
akzakr 7
tsnukk 12
fnmyi 13
yskwcnt 16
akzakr
4 2 2 1
akzakr 4
tsnukk 6
yskwcnt 6
fnmyi 12
akzakr
fnmyi
5 2 3 28
knmmdk 6
skrkyk 14
tmemm 14
akmhmr 15
mksyk 25
knmmdk
skrkyk
5 2 3 11
tmemm 12
skrkyk 18
knmmdk 21
mksyk 23
akmhmr 42
skrkyk
tmemm
14 5 10 38
iormns 19
hbkgnh 23
yyitktk 31
rtkakzk 36
mmftm 43
ykhhgwr 65
hrkamm 66
ktrotns 67
mktkkc 68
mkhsi 69
azsmur 73
tknsj 73
amftm 81
chyksrg 88
mkhsi
hbkgnh
mktkkc
yyitktk
tknsj
14 5 10 38
mktkkc 24
rtkakzk 25
ykhhgwr 25
hrkamm 27
amftm 37
iormns 38
tknsj 38
yyitktk 39
hbkgnh 53
mmftm 53
chyksrg 63
ktrotns 63
azsmur 65
mkhsi 76
mkhsi
hbkgnh
mktkkc
yyitktk
tknsj
0 0 0 0
Output
0
1
1
2
1
4
5 | instruction | 0 | 6,529 | 14 | 13,058 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
while True:
n, m, k, l = map(int, input().split())
if n == 0:
break
ranking = []
for _ in range(n):
name, x = input().split()
x = int(x)
ranking.append([x, name])
ranking.sort(key=lambda x:(-x[0], x[1]))
favs = {input().strip() for _ in range(m)}
favs_x = {}
not_favs_x = {}
for x, name in ranking:
if name in favs:
favs_x[name] = x
else:
not_favs_x[name] = x
favs_ranking = sorted(favs_x.items(), key=lambda x:(-x[1], x[0]))
not_favs_ranking = sorted(not_favs_x.items(), key=lambda x:(-x[1], x[0]))
not_favs_length = len(not_favs_ranking)
favs_length = len(favs_ranking)
def check(num):
not_favs_num = k - num
if num > favs_length:
return False
if not_favs_num >= not_favs_length:
return True
target_name, target_x = not_favs_ranking[not_favs_num]
need = 0
for name, x in favs_ranking[:num]:
if target_name > name:
if target_x <= x:
continue
else:
need += target_x - x
else:
if target_x < x:
continue
else:
need += target_x - x + 1
return (need <= l)
left = 0
right = min(k, favs_length) + 1
while True:
if right <= left + 1:
break
center = (left + right) // 2
if check(center):
left = center
else:
right = center
print(left)
``` | output | 1 | 6,529 | 14 | 13,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,878 | 14 | 13,756 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
from sys import stdin, stdout
def find(node):
x = []
while dsu[node] > 0:
x.append(node)
node = dsu[node]
for i in x:
dsu[i] = node
return node
def union(node1, node2):
if node1 != node2:
if dsu[node1] > dsu[node2]:
node1, node2 = node2, node1
dsu[node1] += dsu[node2]
dsu[node2] = node1
n = int(stdin.readline().strip())
dsu = [-1]*(n+1)
m = int(stdin.readline().strip())
for __ in range(m):
a, b = map(int, stdin.readline().strip().split())
union(find(a), find(b))
k = int(stdin.readline().strip())
for __ in range(k):
a, b = map(int, stdin.readline().strip().split())
p_a = find(a)
p_b = find(b)
if p_a == p_b:
dsu[p_a] = 0
maxm = 0
for i in range(1, n+1):
if dsu[i] < 0:
maxm = max(maxm, abs(dsu[i]))
stdout.write(f'{maxm}')
``` | output | 1 | 6,878 | 14 | 13,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,879 | 14 | 13,758 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)
def dfs(g, col, st):
global used
used[st] = col
for w in g[st]:
if used[w] is False:
dfs(g, col, w)
n = int(input())
k = int(input())
g = []
used = [False] * n
for i in range(n):
g.append([])
for i in range(k):
x, y = map(int, input().split())
g[x - 1].append(y - 1)
g[y - 1].append(x - 1)
cur = 0
for i in range(n):
if used[i] is False:
dfs(g, cur, i)
cur += 1
k = int(input())
lst = [0] * n
for i in range(k):
x, y = map(int, input().split())
x -= 1
y -= 1
if used[x] == used[y]:
lst[used[x]] = -1
for i in range(n):
if lst[used[i]] != -1:
lst[used[i]] += 1
print(max(0, max(lst)))
``` | output | 1 | 6,879 | 14 | 13,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,880 | 14 | 13,760 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
t, p, k = [0] * (int(input()) + 1), {0: []}, 1
for i in range(int(input())):
a, b = map(int, input().split())
if t[a] == t[b]:
if t[a] == 0:
t[a] = t[b] = k
p[k] = [a, b]
k += 1
else:
if t[a] == 0:
t[a] = t[b]
p[t[b]].append(a)
elif t[b] == 0:
t[b] = t[a]
p[t[a]].append(b)
else:
x, y = t[b], t[a]
for c in p[x]:
t[c] = y
p[y] += p[x]
p[x] = []
for i in range(int(input())):
a, b = map(int, input().split())
if t[a] == t[b]: p[t[a]] = []
ans = max(len(p[i]) for i in p)
print(ans if ans > 0 else int(0 in t[1:]))
``` | output | 1 | 6,880 | 14 | 13,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,881 | 14 | 13,762 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
class DSNode:
def __init__(self, val):
self.val = val
self.rank = 0
self.parent = self
self.correct = True
def __str__(self):
return str(self.find().val)
def find(self):
x = self
if x != x.parent:
x.parent = x.parent.find()
return x.parent
def union(x, y):
x = x.find()
y = y.find()
if x == y:
return
if x.rank > y.rank:
y.parent = x
else:
x.parent = y
if x.rank == y.rank:
y.rank += 1
# Amount of people
n = int(input())
# Pairs of friends
k = int(input())
P = [DSNode(x) for x in range(n+1)]
for i in range(k):
u, v = map(int, input().split(' '))
union(P[u], P[v])
# Pairs of people who dislike each other
m = int(input())
for i in range(m):
u, v = map(int, input().split(' '))
u1, v1 = P[u].find(), P[v].find()
if u1 == v1:
u1.correct = False
max_size = 0
A = [0 for _ in range(n + 1)]
for i in range(1, n+1):
p = P[i]
p = p.find()
if p.correct:
A[p.val] += 1
print(max(A))
``` | output | 1 | 6,881 | 14 | 13,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,882 | 14 | 13,764 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
n = int(input())
l = int(input())
likes_list = [[] for i in range(n + 1)]
for i in range(l):
a, b = map(int, input().split())
likes_list[a].append(b)
likes_list[b].append(a)
d = int(input())
dislikes_list = [[] for i in range(n + 1)]
for i in range(d):
a, b = map(int, input().split())
dislikes_list[a].append(b)
dislikes_list[b].append(a)
v = [False] * (n + 1)
groups = {}
f_id = [i for i in range(n + 1)]
for i in range(1, n + 1):
if not v[i]:
f = set()
s = [i]
while len(s) > 0:
x = s.pop()
f_id[x] = i
f.add(x)
if v[x]:
continue
v[x] = True
for y in likes_list[x]:
s.append(y)
groups[i] = f
for i in range(1, n + 1):
for ds in dislikes_list[i]:
groups[f_id[i]].difference_update({ds}.union(groups[f_id[ds]]))
ans = 0
for v in groups.values():
ans = max(ans, len(v))
print(ans)
``` | output | 1 | 6,882 | 14 | 13,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,883 | 14 | 13,766 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
# Problem: C1. Party
# Contest: Codeforces - ABBYY Cup 2.0 - Easy
# URL: https://codeforces.com/contest/177/problem/C1
# Memory Limit: 256 MB
# Time Limit: 2000 ms
#
# KAPOOR'S
from sys import stdin, stdout
def INI():
return int(stdin.readline())
def INL():
return [int(_) for _ in stdin.readline().split()]
def INS():
return stdin.readline()
def MOD():
return pow(10,9)+7
def OPS(ans):
stdout.write(str(ans)+"\n")
def OPL(ans):
[stdout.write(str(_)+" ") for _ in ans]
stdout.write("\n")
rank=[0 for _ in range(2000+1)]
par=[_ for _ in range(2000+1)]
Size=[1 for _ in range(2000+1)]
def findpar(x):
if x==par[x]:
return x
return findpar(par[x])
def union(pu,pv):
if rank[pu]<rank[pv]:
par[pu]=pv
Size[pv]+=Size[pu]
elif rank[pv]<rank[pu]:
par[pv]=pu
Size[pu]+=Size[pv]
else:
par[pv]=pu
rank[pu]+=1
Size[pu]+=Size[pv]
if __name__=="__main__":
# for _ in range(INI()):
t=INI()
n=INI()
for _ in range(n):
u,v=INL()
pu=findpar(u)
pv=findpar(v)
if pu!=pv:
union(pu,pv)
q=int(input())
for _ in range(q):
u,v=INL()
pu=findpar(u)
pv=findpar(v)
if pu==pv:
Size[pu]=0
ans=0
for _ in range(1,t+1):
ans=max(ans,Size[findpar(_)])
print(ans)
``` | output | 1 | 6,883 | 14 | 13,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,884 | 14 | 13,768 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
def find(a):
if parent[a]!=a:
parent[a]=find(parent[a])
return parent[a]
def union(a,b):
u,v=find(a),find(b)
if u==v:
return
if rank[u]>rank[v]:
parent[v]=u
else:
parent[u]=v
if rank[u]==rank[v]:
rank[v]+=1
n=int(input())
k=int(input())
parent=list(map(int,range(n+1)))
rank=[0]*(n+1)
ans=[0]*(n+1)
count=[0]*(n+1)
for i in range(k):
u,v=map(int,input().split())
union(u,v)
for i in range(len(ans)):
ans[find(i)]+=1
for i in range(len(parent)):
count[parent[i]]+=1
d={}
m=int(input())
for i in range(m):
u,v=map(int,input().split())
if parent[u]==parent[v]:
d[parent[u]]=False
sak=0
for i in range(len(count)):
if count[i]!=0 and i not in d and i!=0:
sak=max(sak,count[i])
print(sak)
``` | output | 1 | 6,884 | 14 | 13,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | instruction | 0 | 6,885 | 14 | 13,770 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
n = int(input())
num_likes = int(input())
like = [ [] for u in range(n + 1) ]
for i in range(num_likes):
u, v = map(int, input().split())
like[u].append(v)
like[v].append(u)
num_dislikes = int(input())
dislike = [ (n + 1) * [ False ] for u in range(n + 1) ]
for i in range(num_dislikes):
u, v = map(int, input().split())
dislike[u][v] = True
dislike[v][u] = True
result = 0
seen = (n + 1) * [ False ]
for u in range(1, n + 1):
if seen[u]:
continue
seen[u] = True
group = [ u ]
queue = [ u ]
tail = 0
while tail < len(queue):
u = queue[tail]
tail += 1
for v in like[u]:
if seen[v]:
continue
seen[v] = True
group.append(v)
queue.append(v)
okay = True
for i, u in enumerate(group):
for j in range(i + 1, len(group)):
v = group[j]
if dislike[u][v]:
okay = False
break
if not okay:
break
if okay:
result = max(result, len(group))
print(result)
``` | output | 1 | 6,885 | 14 | 13,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
def dfs (chain):
if chain:
for c in chain:
if c not in groups:
groups.add(c)
dfs(graph[c-1])
graph[c-1].clear()
graph= [[] for _ in range(int(input()))]; sev = len(graph)
groups,inv,ind = set(),{},1
for _ in range(int(input())):
u,v = map(int,input().split())
graph[u-1].append(v)
for i,g in enumerate(graph):
if g:
groups.add(i+1)
dfs(g); inv[ind] = [p for p in groups]
ind+=1; groups.clear()
graph = [set() for _ in range(sev)]
for f in inv:
for k in inv[f]: graph[k-1].add(f)
for g in range(sev):
if not graph[g]: inv[g+1] = [g+1]
for _ in range(int(input())):
cat = [x for x in map(int,input().split())]
gat = graph[cat[0]-1].intersection(graph[cat[1]-1])
if gat:
for s in cat:
for l in graph[s-1]:
try: inv.pop(l)
except KeyError: continue
if inv: print(len(max(inv.items(),key = lambda x: len(x[1]))[1]))
else: print(0)
``` | instruction | 0 | 6,886 | 14 | 13,772 |
Yes | output | 1 | 6,886 | 14 | 13,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
t, p, k = [0] * (int(input()) + 1), {0: []}, 1
for i in range(int(input())):
a, b = map(int, input().split())
if t[a] == t[b]:
if t[a] == 0:
t[a] = t[b] = k
p[k] = [a, b]
k += 1
else:
if t[a] == 0:
t[a] = t[b]
p[t[b]].append(a)
elif t[b] == 0:
t[b] = t[a]
p[t[a]].append(b)
else:
x, y = t[b], t[a]
for c in p[x]:
t[c] = y
p[y] += p[x]
p[x] = []
for i in range(int(input())):
a, b = map(int, input().split())
if t[a] == t[b]: p[t[a]] = []
ans = max(map(len, p.values()))
if ans == 0: ans = int(0 in t[1:])
print(ans)
``` | instruction | 0 | 6,887 | 14 | 13,774 |
Yes | output | 1 | 6,887 | 14 | 13,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
class DisjointSetStructure():
def __init__(self, n):
self.A = [[i,0,1] for i in range(n)]
self.size = n
def getpair(self, i):
p = i
while self.A[p][0] != p: p = self.A[p][0]
j = self.A[i][0]
while j != p:
self.A[i][0] = p
i, j = j, self.A[j][0]
return (p, self.A[p][2])
def __getitem__(self, i):
return self.getpair(i)[0]
def union(self, i, j):
u, v = self[i], self[j]
if u == v: return
self.size -= 1
if self.A[u][1] > self.A[v][1]:
self.A[v][0] = u
self.A[u][2] += self.A[v][2]
else:
self.A[u][0] = v
self.A[v][2] += self.A[u][2]
if self.A[u][1] == self.A[v][1]:
self.A[v][1] += 1
def __len__(self): return self.size
n = int(input())
k = int(input())
D = DisjointSetStructure(n)
for i in range(k):
u, v = [int(x) - 1 for x in input().split()]
D.union(u, v)
l = int(input())
friendly = [True for i in range(n)]
for i in range(l):
u, v = [int(x) - 1 for x in input().split()]
if D[u] == D[v]: friendly[D[u]] = False
print(max(D.getpair(i)[1] if friendly[D[i]] else 0 for i in range(n)))
``` | instruction | 0 | 6,888 | 14 | 13,776 |
Yes | output | 1 | 6,888 | 14 | 13,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
from collections import defaultdict
def Root(child):
while(Parent[child]!=child):
child = Parent[child]
return child
def Union(a,b):
root_a = Root(a)
root_b = Root(b)
if(root_a!=root_b):
if(Size[root_a]<Size[root_b]):
Parent[root_a] = root_b
Size[root_b]+=Size[root_a]
else:
Parent[root_b] = root_a
Size[root_a]+=Size[root_b]
return 1
return 0
n = int(input())
Parent = [i for i in range(n)]
Size = [1 for i in range(n)]
k = int(input())
for i in range(k):
u,v = map(int,input().split())
u-=1;v-=1
Union(u,v)
m = int(input())
for i in range(m):
u,v = map(int,input().split())
root_u = Root(u-1)
root_v = Root(v-1)
if(root_u==root_v):
Size[root_u] = 0
Max = -float('inf')
for i in range(n):
Max = max(Max,Size[Root(i)])
print(Max)
``` | instruction | 0 | 6,889 | 14 | 13,778 |
Yes | output | 1 | 6,889 | 14 | 13,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
def root(Arr,i):
while(Arr[i]!=i):
Arr[i]=Arr[Arr[i]]
i=Arr[i]
return i
def Union(Arr,A,B):
root_A= root(Arr,A)
root_B=root(Arr,B)
if size[root_B]<size[root_A]:
Arr[root_B]=root_A
size[root_A]+=size[root_B]
else:
Arr[root_A]=root_B
size[root_B]+=size[root_A]
def Find(A,B):
if root(Arr,A)==root(Arr,B):
return True
return False
n=int(input())
Arr=list(range(n))
size=[1]*(n)
f=int(input())
for i in range(f):
A,B=map(int,input().split())
if not Find(A-1,B-1):
Union(Arr,A-1,B-1)
e=int(input())
enemy=[[0,0]]*e
lst=sorted(size)[::-1]
for i in range(e):
A,B=map(int,input().split())
enemy[i][0]=A-1
enemy[i][1]=B-1
ans=0
test=0
while(True and ans<n):
for i in range(e):
a=enemy[i][0]
b=enemy[i][1]
Root=root(Arr,size.index(lst[ans]))
if root(Arr,a)==Root and root(Arr,b)==Root:
break
if i==e-1:
test=1
break
if test==1:
break
ans+=1
#print(size)
if ans>e:
ans=0
print(lst[ans])
``` | instruction | 0 | 6,890 | 14 | 13,780 |
No | output | 1 | 6,890 | 14 | 13,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
def root(Arr,i):
while(Arr[i]!=i):
Arr[i]=Arr[Arr[i]]
i=Arr[i]
return i
def Union(Arr,A,B):
root_A= root(Arr,A)
root_B=root(Arr,B)
if size[root_B]<size[root_A]:
Arr[root_B]=root_A
size[root_A]+=size[root_B]
else:
Arr[root_A]=root_B
size[root_B]+=size[root_A]
def Find(A,B):
if root(Arr,A)==root(Arr,B):
return True
return False
n=int(input())
Arr=list(range(n))
size=[1]*(n)
f=int(input())
for i in range(f):
A,B=map(int,input().split())
if not Find(A-1,B-1):
Union(Arr,A-1,B-1)
e=int(input())
enemy=[[0,0]]*e
lst=sorted(size)[::-1]
for i in range(e):
A,B=map(int,input().split())
enemy[i][0]=A-1
enemy[i][1]=B-1
ans=0
test=0
if e==0:
print(lst[0])
else:
while(True and ans<n):
for i in range(e):
a=enemy[i][0]
b=enemy[i][1]
Root=root(Arr,size.index(lst[ans]))
if root(Arr,a)==Root and root(Arr,b)==Root:
break
if i==e-1:
test=1
break
if test==1:
break
ans+=1
#print(size)
if ans>e:
print('0')
else:
print(lst[ans])
``` | instruction | 0 | 6,891 | 14 | 13,782 |
No | output | 1 | 6,891 | 14 | 13,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
def root(Arr,i):
while(Arr[i]!=i):
Arr[i]=Arr[Arr[i]]
i=Arr[i]
return i
def Union(Arr,A,B):
root_A= root(Arr,A)
root_B=root(Arr,B)
if size[root_B]<size[root_A]:
Arr[root_B]=Arr[root_A]
size[root_A]+=size[root_B]
else:
Arr[root_A]=Arr[root_B]
size[root_B]+=size[root_A]
def Find(A,B):
if root(Arr,A)==root(Arr,B):
return True
return False
n=int(input())
Arr=list(range(n))
size=[1]*(n)
lst=[]
f=int(input())
for i in range(f):
A,B=map(int,input().split())
if not Find(A-1,B-1):
Union(Arr,A-1,B-1)
e=int(input())
enemy=[]
#print(enemy)
for x in range(e):
a,b=map(int,input().split())
enemy.append([a-1,b-1])
#print(enemy)
for i in range(n):
if Arr[i]==i:
lst.append([size[i],i])
lst=sorted(lst)[::-1]
ans=0
test=0
if e==0:
print(lst[0][0])
else:
for i in range(len(lst)):
Root=lst[i][1]
test=0
for k in range(e):
if root(Arr,enemy[k][0])==Root and root(Arr,enemy[k][1])==Root:
test=1
break
if test==0:
ans=lst[i][0]
break
#print(enemy)
#print(lst)
print(ans)
``` | instruction | 0 | 6,892 | 14 | 13,784 |
No | output | 1 | 6,892 | 14 | 13,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
More formally, for each invited person the following conditions should be fulfilled:
* all his friends should also be invited to the party;
* the party shouldn't have any people he dislikes;
* all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends.
Help the Beaver find the maximum number of acquaintances he can invite.
Input
The first line of input contains an integer n β the number of the Beaver's acquaintances.
The second line contains an integer k <image> β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> β indices of people who form the i-th pair of friends.
The next line contains an integer m <image> β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
The input limitations for getting 30 points are:
* 2 β€ n β€ 14
The input limitations for getting 100 points are:
* 2 β€ n β€ 2000
Output
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
Examples
Input
9
8
1 2
1 3
2 3
4 5
6 7
7 8
8 9
9 6
2
1 6
7 9
Output
3
Note
Let's have a look at the example.
<image>
Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
Submitted Solution:
```
# Problem: C1. Party
# Contest: Codeforces - ABBYY Cup 2.0 - Easy
# URL: https://codeforces.com/contest/177/problem/C1
# Memory Limit: 256 MB
# Time Limit: 2000 ms
#
# KAPOOR'S
from sys import stdin, stdout
def INI():
return int(stdin.readline())
def INL():
return [int(_) for _ in stdin.readline().split()]
def INS():
return stdin.readline()
def MOD():
return pow(10,9)+7
def OPS(ans):
stdout.write(str(ans)+"\n")
def OPL(ans):
[stdout.write(str(_)+" ") for _ in ans]
stdout.write("\n")
rank=[0 for _ in range(2000+1)]
par=[_ for _ in range(2000+1)]
Size=[1 for _ in range(2000+1)]
def findpar(x):
if x==par[x]:
return x
return findpar(par[x])
def union(pu,pv):
if rank[pu]<rank[pv]:
par[pu]=pv
Size[pv]+=Size[pu]
elif rank[pv]<rank[pu]:
par[pv]=pu
Size[pu]+=Size[pv]
else:
par[pv]=pu
rank[pu]+=1
Size[pu]+=Size[pv]
if __name__=="__main__":
# for _ in range(INI()):
INI()
n=INI()
for _ in range(n):
u,v=INL()
pu=findpar(u)
pv=findpar(v)
if pu!=pv:
union(pu,pv)
q=int(input())
for _ in range(q):
u,v=INL()
pu=findpar(u)
pv=findpar(v)
if pu==pv:
par[pu]=0
ans=0
for _ in range(1,n+1):
ans=max(ans,Size[findpar(_)])
print(ans)
``` | instruction | 0 | 6,893 | 14 | 13,786 |
No | output | 1 | 6,893 | 14 | 13,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,934 | 14 | 13,868 |
Tags: data structures, greedy, implementation
Correct Solution:
```
t=int(input())
arr=list(map(int,input().split()))
i=t-1
while( i>0 and arr[i] > arr[i-1] ):
i-=1
print(i)
``` | output | 1 | 6,934 | 14 | 13,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,935 | 14 | 13,870 |
Tags: data structures, greedy, implementation
Correct Solution:
```
import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))[::-1]
ans = 0
acc = n
pre = 9999999999999
for i in range(n):
if l[i] < pre:
ans += 1
pre = l[i]
else:
break
print(n-ans)
``` | output | 1 | 6,935 | 14 | 13,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,936 | 14 | 13,872 |
Tags: data structures, greedy, implementation
Correct Solution:
```
# ANDRE CHEKER BURIHAN RA 194071
noth = int(input())
oldpos = [int(num) for num in input().split()]
newpos = [i+1 for i in range(noth)]
counter = 0
if oldpos != newpos:
for i in reversed(range(1, noth)):
if oldpos[i-1] > oldpos[i]:
counter = i
break
print(counter)
``` | output | 1 | 6,936 | 14 | 13,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,937 | 14 | 13,874 |
Tags: data structures, greedy, implementation
Correct Solution:
```
n = int(input())
a = (str(input())).split(' ')
ma=0
output = 0
for i in range(n-1):
if int(a[i])<int(a[i+1]):
ma+=1
else:
output+=1+ma
ma=0
print(output)
``` | output | 1 | 6,937 | 14 | 13,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,938 | 14 | 13,876 |
Tags: data structures, greedy, implementation
Correct Solution:
```
n=input()
arr=input().split(" ")
asc=1
x=int(arr[0])
for i in range(1,int(n)):
if int(arr[i])>x:
asc+=1
else:
asc=1
x=int(arr[i])
print(int(n)-asc)
``` | output | 1 | 6,938 | 14 | 13,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,939 | 14 | 13,878 |
Tags: data structures, greedy, implementation
Correct Solution:
```
if __name__ == '__main__':
n = int(input())
q = (list(map(int, input().split())))
index = 0
for i in range(n-1, 0, -1):
if(q[i-1] > q[i]):
index = i
break
print(index)
``` | output | 1 | 6,939 | 14 | 13,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,940 | 14 | 13,880 |
Tags: data structures, greedy, implementation
Correct Solution:
```
n, ar = int(input()), [int(x) for x in input().split()][::-1]
ans = 0
for i in range(1, n):
if ar[i] > ar[i - 1]:
ans = n - i
break
print(ans)
``` | output | 1 | 6,940 | 14 | 13,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | instruction | 0 | 6,941 | 14 | 13,882 |
Tags: data structures, greedy, implementation
Correct Solution:
```
num = int(input())
arr = list(map(int,input().split()))
i = num-1
while arr[i] > arr[i-1]:
i -= 1
print(i)
``` | output | 1 | 6,941 | 14 | 13,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
n = int(input())
aux = input().split()
first = int(aux[0])
flag = 1
for i in range(1,n):
if int(aux[i]) < first: flag=1
else: flag+=1
first = int(aux[i])
print(n-flag)
``` | instruction | 0 | 6,942 | 14 | 13,884 |
Yes | output | 1 | 6,942 | 14 | 13,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
n= int(input())
lista = input().split()
cont = 0
i = n-1
if n == 1:
print("0")
else:
while i > 0:
if int(lista[i]) > int(lista[i-1]):
cont += 1
i -= 1
if i == 0:
cont+= 1
else:
cont += 1
break
print(n-cont)
``` | instruction | 0 | 6,943 | 14 | 13,886 |
Yes | output | 1 | 6,943 | 14 | 13,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
# from debug import *
import sys; input = sys.stdin.readline
from collections import deque, defaultdict
from math import log10, ceil, factorial as F
I = lambda : int(input())
L = lambda : list(map(int, input().split()))
T = lambda : map(int, input().split())
n = I()
lis = L()
i = n-1
while i>0 and lis[i-1] < lis[i]: i-=1
print(i)
``` | instruction | 0 | 6,944 | 14 | 13,888 |
Yes | output | 1 | 6,944 | 14 | 13,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
n=int(input(""))
a=[int(x) for x in input("").split(' ')]
output = 0
cache = 0
for x in range(n-1):
if a[x] > a[x+1]:
output+=1+cache
cache=0
else:
cache+=1
print(output)
``` | instruction | 0 | 6,945 | 14 | 13,890 |
Yes | output | 1 | 6,945 | 14 | 13,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
n = int(input())
inStr = input()
pos = [int(x) for x in inStr.split()]
min = n
ans = 0
for i in range(n-1, -1, -1):
if pos[i]>min:
ans = ans + 1
elif pos[i]<min:
min = pos[i]
print(ans)
``` | instruction | 0 | 6,946 | 14 | 13,892 |
No | output | 1 | 6,946 | 14 | 13,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
int(input())
l = [int(x) for x in input().split()]
l.reverse()
cur = 1000000
ans = 0
for x in l:
if x > cur:
ans += 1
else:
cur = x
print(ans)
``` | instruction | 0 | 6,947 | 14 | 13,894 |
No | output | 1 | 6,947 | 14 | 13,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread x is not updated (it has no new messages);
2. the list order 1, 2, ..., n changes to a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer β the number of threads that surely contain a new message.
Examples
Input
5
5 2 1 3 4
Output
2
Input
3
1 2 3
Output
0
Input
4
4 3 2 1
Output
3
Note
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
Submitted Solution:
```
N = int(input())
X = list(map(int, input().split()))
Index = N-1
for i in range(N-1 , 1, -1):
if X[i] <X[i-1]:
print(i)
exit()
print(0)
# Hope the best for Ravens member
``` | instruction | 0 | 6,948 | 14 | 13,896 |
No | output | 1 | 6,948 | 14 | 13,897 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.