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. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,239
14
126,478
Tags: dfs and similar, dsu, math Correct Solution: ``` N = [] AA = [] t = int(input()) for i in range(t): N.append(int(input())) a = list(map(int,input().split())) for j in range(len(a)): a[j] -= 1 AA.append(a) for i in range(t): n = N[i] A = AA[i] ans = [0] * n for j in range(n): if ans[j] == 0: M = [] c = 1 b = A[j] M.append(b) while b != j: b = A[b] M.append(b) c += 1 for kk in M: ans[kk] = c print(" ".join([str(i) for i in ans])) ```
output
1
63,239
14
126,479
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,240
14
126,480
Tags: dfs and similar, dsu, math Correct Solution: ``` for _ in range(int(input())): kids = int(input()) order = list(map(int, input().split())) used = set() re = [None]*kids for j in range(1,kids+1): if re[j-1]: continue else: idx = order[j-1] days = 1 group = set() group.add(idx) while j != idx: temp = order[idx-1] idx = temp group.add(idx) days += 1 for i in group: re[i-1]=days print(*re) ```
output
1
63,240
14
126,481
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,241
14
126,482
Tags: dfs and similar, dsu, math Correct Solution: ``` q = int(input()) nums = [] pos =[] '''for _ in range(q): nums += [int(input())] p = input().split() pos += [[ int(x)-1 for x in p]]''' for i in range(q): n = int(input()) p = input().split() p = [ int(x)-1 for x in p] list1 = list(range(n)) list2 = [] dict = dict.fromkeys(list(range(n)),0) for i in list1: if(i=='a'): continue j = p[i] count = 1 while( j!=i): j = p[j] count += 1 j = p[i]; dict[i] = count while( j!=i): list1[j]='a' dict[j] = count j = p[j] for key in dict.keys(): print(dict[key], end=' ') '''for _ in range(q): n = int(input()) p = input().split() p = [ int(x)-1 for x in p] list1 =[] for i in range(n): j = p[i]; count = 1 while( j!=i): j = p[j] count +=1 list1= list1+[count] for x in list1: print(x, end = ' ')''' ```
output
1
63,241
14
126,483
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,242
14
126,484
Tags: dfs and similar, dsu, math Correct Solution: ``` #!/usr/bin/env python import sys def atoi(line): line = line.rstrip().split() return list(map(int, line)) ans = [] q = int(input()) for q_ in range(q): n = int(input()) p = atoi(sys.stdin.readline()) p.insert(0, 0) used = [False] * (n + 1) days = [1] * (n + 1) i = 1 while i < n + 1: if used[i]: i += 1 continue loop = [i] used[i] = True j = p[i] while j != loop[0]: j = p[j] loop.append(p[j]) used[j] = True i += 1 for k in loop: days[k] = len(loop) ans.append(days) for d in ans: d = d[1:] print(*d, sep=" ") ```
output
1
63,242
14
126,485
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,243
14
126,486
Tags: dfs and similar, dsu, math Correct Solution: ``` #_______________________________________________________________# def fact(x): if x == 0: return 1 else: return x * fact(x-1) def lower_bound(li, num): #return 0 if all are greater or equal to answer = -1 #no role though start = 0 end = len(li)-1 while(start <= end): middle = (end+start)//2 if li[middle] >= num: answer = middle end = middle - 1 else: start = middle + 1 return answer #index where x is not less than num def upper_bound(li, num): #return n-1 if all are small or equal answer = -1 start = 0 end = len(li)-1 while(start <= end): middle = (end+start)//2 if li[middle] <= num: answer = middle start = middle + 1 else: end = middle - 1 return answer #index where x is not greater than num def abs(x): return x if x >=0 else -x def binary_search(li, val, lb, ub): # ITS A BINARY ans = 0 while(lb <= ub): mid = (lb+ub)//2 #print(mid, li[mid]) if li[mid] > val: ub = mid-1 elif val > li[mid]: lb = mid + 1 else: ans = 1 break return ans def sieve_of_eratosthenes(n): ans = [] arr = [1]*(n+1) arr[0],arr[1], i = 0, 0, 2 while(i*i <= n): if arr[i] == 1: j = i+i while(j <= n): arr[j] = 0 j += i i += 1 for k in range(n): if arr[k] == 1: ans.append(k) return ans def nc2(x): if x == 1: return 0 else: return x*(x-1)//2 def kadane(x): #maximum subarray sum sum_so_far = 0 current_sum = 0 for i in x: current_sum += i if current_sum < 0: current_sum = 0 else: sum_so_far = max(sum_so_far,current_sum) return sum_so_far def mex(li): check = [0]*1001 for i in li: check[i] += 1 for i in range(len(check)): if check[i] == 0: return i def sumdigits(n): ans = 0 while(n!=0): ans += n%10 n //= 10 return ans def product(li): ans = 1 for i in li: ans *= i return ans def maxpower(n,k): cnt = 0 while(n>1): cnt += 1 n //= k return cnt #_______________________________________________________________# ''' โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„ โ–„โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–€โ–€โ–€โ–€โ–€โ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–„ โ–‘โ–โ–ˆโ–ˆโ–ˆโ–ˆโ–€โ–’โ–’Aestroixโ–’โ–’โ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–‘โ–ˆโ–ˆโ–ˆโ–€โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–€โ–ˆโ–ˆโ–ˆโ–ˆ โ–‘โ–โ–ˆโ–ˆโ–’โ–’โ–’โ–’โ–’KARMANYAโ–’โ–’โ–’โ–’โ–’โ–’โ–ˆโ–ˆโ–ˆโ–ˆโ–Œ ________________ โ–‘โ–โ–ˆโ–Œโ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–ˆโ–ˆโ–ˆโ–ˆโ–Œ ? ? |โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’| โ–‘โ–‘โ–ˆโ–’โ–’โ–„โ–€โ–€โ–€โ–€โ–€โ–„โ–’โ–’โ–„โ–€โ–€โ–€โ–€โ–€โ–„โ–’โ–’โ–โ–ˆโ–ˆโ–ˆโ–Œ ? |___CM ONE DAY___| โ–‘โ–‘โ–‘โ–โ–‘โ–‘โ–‘โ–„โ–„โ–‘โ–‘โ–Œโ–โ–‘โ–‘โ–‘โ–„โ–„โ–‘โ–‘โ–Œโ–’โ–โ–ˆโ–ˆโ–ˆโ–Œ ? ? |โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’| โ–‘โ–„โ–€โ–Œโ–‘โ–‘โ–‘โ–€โ–€โ–‘โ–‘โ–Œโ–โ–‘โ–‘โ–‘โ–€โ–€โ–‘โ–‘โ–Œโ–’โ–€โ–’โ–ˆโ–Œ ? ? โ–‘โ–Œโ–’โ–€โ–„โ–‘โ–‘โ–‘โ–‘โ–„โ–€โ–’โ–’โ–€โ–„โ–‘โ–‘โ–‘โ–„โ–€โ–’โ–’โ–„โ–€โ–’โ–Œ ? โ–‘โ–€โ–„โ–โ–’โ–€โ–€โ–€โ–€โ–’โ–’โ–’โ–’โ–’โ–’โ–€โ–€โ–€โ–’โ–’โ–’โ–’โ–’โ–’โ–ˆ ? ? โ–‘โ–‘โ–‘โ–€โ–Œโ–’โ–„โ–ˆโ–ˆโ–„โ–„โ–„โ–„โ–ˆโ–ˆโ–ˆโ–ˆโ–„โ–’โ–’โ–’โ–’โ–ˆโ–€ ? โ–‘โ–‘โ–‘โ–‘โ–„โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆ=========โ–ˆโ–’โ–’โ–โ–Œ โ–‘โ–‘โ–‘โ–€โ–ˆโ–ˆโ–ˆโ–€โ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–€โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–€โ–’โ–Œ โ–‘โ–‘โ–‘โ–‘โ–‘โ–Œโ–’โ–’โ–’โ–„โ–’โ–’โ–’โ–„โ–’โ–’โ–’โ–’โ–’โ–’โ– โ–‘โ–‘โ–‘โ–‘โ–‘โ–Œโ–’โ–’โ–’โ–’โ–€โ–€โ–€โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ– โ–‘โ–‘โ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ ''' from math import * from collections import deque max_steps = 100005 for _ in range(int(input())): #for _ in range(1): n = int(input()) #x,y,z = map(int,input().split()) #st = input() a = list(map(int,input().split())) #l,h = map(int,input().split()) #b = list(map(int,input().split())) ans = [0]*(n) for i in range(n): if ans[i] == 0: li = [] li.append(i+1) k = a[i] steps = 1 while(k != i+1): steps += 1 li.append(k) #appending the character encountered k = a[k-1] # aquire the next character for j in li: ans[j-1] = steps print(*ans) ```
output
1
63,243
14
126,487
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
instruction
0
63,244
14
126,488
Tags: dfs and similar, dsu, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=[(int(i)-1) for i in input().split()] c=[1]*n b=set() for i in range(n): if a[i] in b: continue if len(b)==n: break j=i a1=set() s=1 while a[j]!=i: a1.add(a[j]) b.add(a[j]) s+=1 j=a[j] for k in a1: c[k]=s print(*c) ```
output
1
63,244
14
126,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` #TO MAKE THE PROGRAM FAST ''' ---------------------------------------------------------------------------------------------------- ''' import sys sys.setrecursionlimit(200001) from collections import * ''' ---------------------------------------------------------------------------------------------------- ''' #FOR TAKING INPUTS ''' ---------------------------------------------------------------------------------------------------- ''' def li():return [int(____) for ____ in input().split(' ')] def val():return int(input()) ''' ---------------------------------------------------------------------------------------------------- ''' visited = {} curr = {} ans = [] def do(tot): global curr,ans for q in curr: ans[q] = tot def bfs(l,ind,tot,mainind): d = deque() d.append([ind,tot]) while d: ind,tot = d.pop() if l[ind] == mainind+1: do(tot) else: curr[ind] = 1 visited[l[ind]] = 1 d.append([l[ind]-1,tot+1]) # def dfs(l,ind,tot,mainind): # global curr,visited # if l[ind] == mainind+1: # if len(curr):do(tot) # else: # curr[ind] = 1 # visited[l[ind]] = 1 # dfs(l,l[ind]-1,tot+1,mainind) #MAIN PROGRAM ''' ---------------------------------------------------------------------------------------------------- ''' for _ in range(val()): n = val() l = li() visited = {} ans = [1 for k in range(n)] for i in range(len(l)): if l[i] not in visited: curr = {} bfs(l,i,1,i) print(' '.join(str(arun) for arun in ans)) ''' ---------------------------------------------------------------------------------------------------- ''' ```
instruction
0
63,245
14
126,490
Yes
output
1
63,245
14
126,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` from sys import stdin cin=stdin.readline for _ in range(int(cin())): n=int(cin()) a=list(map(int,cin().split())) result=[-1]*(n+1) for i in range(n): if result[i+1]!=-1: continue x,y,cnt=-1,i+1,0 visited = [] while x!=i+1: x=a[y-1] y=x cnt+=1 visited.append(x) for child in visited: result[child]=cnt for i in range(1,n+1): print(result[i],end=' ') print() ```
instruction
0
63,246
14
126,492
Yes
output
1
63,246
14
126,493
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` import re def read(t=int): return list(map(t,input().strip().split(' '))) q = read()[0] def check(lis): ... while q>0: q-=1 n = read()[0] p = [None]+read() ans = [-1 for i in p] for i in range(1,len(p)): if ans[i]!=-1: ... else: stack = [i] day = 0 while True: now = p[stack[-1]] day+=1 if now == i: break else: if ans[now]!=-1: day+=ans[now] break stack.append(now) for k in stack: ans[k] = day print('{} '.format(ans[i]),end='') print() ```
instruction
0
63,247
14
126,494
Yes
output
1
63,247
14
126,495
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` #from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)ใงๆœ€ๅคงใฎ2ๅ€‹ใจใ‚Šใ ใ›ใ‚‹ใŠ a[0][0] from fractions import gcd from itertools import combinations,permutations,accumulate, product # (string,3) 3ๅ›ž #from collections import deque from collections import deque,defaultdict,Counter import decimal import re import math import bisect # # # # pythonใง็„ก็†ใชใจใใฏใ€pypyใงใ‚„ใ‚‹ใจๆญฃ่งฃใ™ใ‚‹ใ‹ใ‚‚๏ผ๏ผ # # # my_round_int = lambda x:np.round((x*2 + 1)//2) # ๅ››ๆจไบ”ๅ…ฅg # # ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น็ณป # int min_y = max(0, i - 2), max_y = min(h - 1, i + 2); # int min_x = max(0, j - 2), max_x = min(w - 1, j + 2); # # import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 #mod = 9982443453 #mod = 998244353 from sys import stdin readline = stdin.readline def readInts(): return list(map(int,readline().split())) def readTuples(): return tuple(map(int,readline().split())) def I(): return int(readline()) for _ in range(I()): n = I() A = readInts() dic = defaultdict(int) for i in range(n): if dic[i+1]: continue lis = set() lis.add(i+1) pos = i+1 now = A[i] cost = 1 while now != pos: cost += 1 lis.add(now) now = A[now-1] for c in list(lis): dic[c] = cost ans = [] for i in range(n): ans.append(dic[i+1]) print(*ans) ```
instruction
0
63,248
14
126,496
Yes
output
1
63,248
14
126,497
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` import sys def find(x): if parent[x] == x: return x parent[x] = find(parent[x]) return parent[x] def merge(x, y): x = find(x) y = find(y) if x == y: return if level[x] > level[y]: temp = y y = x x = temp parent[x] = y if level[x] == level[y]: level[y] += 1 temp1 = cnt[x] temp2 = cnt[y] cnt[y] += temp1 cnt[x] += temp2 tc = int(sys.stdin.readline()) for _ in range(tc): n = int(sys.stdin.readline()) arr = [0] + list(map(int, sys.stdin.readline().split())) parent = [i for i in range(n + 1)] level = [1] * (n + 1) cnt = dict().fromkeys([i for i in range(n + 1)], 1) ans = [0] * (n + 1) for i in range(1, n + 1): merge(arr[i], parent[i]) for i in range(1, n + 1): ans[i] = cnt[parent[i]] print(' '.join(map(str, ans[1:]))) ```
instruction
0
63,249
14
126,498
No
output
1
63,249
14
126,499
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` import sys sys.setrecursionlimit(300000) t = int(input()) class DSU: def __init__(self,n): self.parent = [0]*n for i in range(n): self.parent[i]=i self.size = n self.final = [0]*n def find_set(self,x): if self.parent[x]==x: return x self.parent[x] = self.find_set(self.parent[x]) return self.parent[x] def union_set(self,a,b): a = self.find_set(a) b = self.find_set(b) if a!=b: self.parent[a]=b def find_parent_all(self): for i in range(self.size): self.parent[i] = self.find_set(i) self.final[self.parent[i]]+=1 for i in range(self.size): print(self.final[self.parent[i]] , end=' ') print('') while t: t-=1 n = int(input()) v = list(map(int,input().strip().split()))[:n] data = DSU(n) i=0 if n==200000 and v[0]==2: for i in range(n): print(i+1 ,end=' ') print('') continue for val in v: data.union_set(i,val-1) i+=1 data.find_parent_all() ```
instruction
0
63,250
14
126,500
No
output
1
63,250
14
126,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` n = int(input()) string = input().strip() count = 0 for i in string: if i == '(': count += 1 else: count -= 1 if count < -1: break a = string.count('(') == string.count(')') if count >= -1 and a: print('YES') else: print('NO') ```
instruction
0
63,251
14
126,502
No
output
1
63,251
14
126,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed. For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on. Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n. Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: * after the 1-st day it will belong to the 5-th kid, * after the 2-nd day it will belong to the 3-rd kid, * after the 3-rd day it will belong to the 2-nd kid, * after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer q independent queries. Input The first line of the input contains one integer q (1 โ‰ค q โ‰ค 1000) โ€” the number of queries. Then q queries follow. The first line of the query contains one integer n (1 โ‰ค n โ‰ค 2 โ‹… 10^5) โ€” the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 โ‰ค p_i โ‰ค n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid. It is guaranteed that โˆ‘ n โ‰ค 2 โ‹… 10^5 (sum of n over all queries does not exceed 2 โ‹… 10^5). Output For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query. Example Input 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 Output 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4 Submitted Solution: ``` q = int(input()) for i in range(q): n = int(input()) p_seq = list(map(int,input().split())) p_seq = [x-1 for x in p_seq] anslist = [0 for b in range(n)] ansboolean = [False for c in range(n)] for a in range(n): if not ansboolean[a]: ans = 1 person_to_pass = p_seq[a] ansboolean[a] = True while person_to_pass != a: ansboolean[person_to_pass] = True person_to_pass = p_seq[person_to_pass] ans += 1 person_to_pass = p_seq[a] anslist[a] = ans while person_to_pass != a: anslist[person_to_pass] = ans person_to_pass = p_seq[person_to_pass] print(anslist) print(' '.join([str(x) for x in anslist])) ```
instruction
0
63,252
14
126,504
No
output
1
63,252
14
126,505
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,314
14
126,628
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import sys import math,bisect from collections import defaultdict from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,OrderedDict #input = iter(sys.stdin.buffer.read().decode().splitlines())._next_ def neo(): return map(int,input().split()) def Neo(): return list(map(int,input().split())) n,d,m = neo() A = Neo() G,L = [],[] for i in A: if i > m: G.append(i) else: L.append(i) G.sort(reverse = True) L.sort() Gpf = list(accumulate(G)) Lpf = list(accumulate(L)) Gpf = [0]+Gpf Lpf = [0]+Lpf Ans = [] for i in range(len(Gpf)): days = (i-1)*(d+1)+1 if days <= n: t = Gpf[i] rd = n - days t += Lpf[-1] - Lpf[max(0,len(Lpf)-rd-1)] Ans.append(t) print(max(Ans)) ```
output
1
63,314
14
126,629
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,315
14
126,630
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import sys from array import array # noqa: F401 import typing as Tp # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def main(): n, d, m = map(int, input().split()) small, big = [], [] for x in map(int, input().split()): if x > m: big.append(x) else: small.append(x) small.sort() big.sort(reverse=True) if not big: print(sum(small)) exit() n -= 1 ans = big.pop(0) big = [big[i] for i in range(min(len(big), n // (d + 1)))] ans += sum(small.pop() for i in range(min(len(small), n - (len(big) * (d + 1))))) while small and big: x = sum(small.pop() for i in range(min(len(small), d + 1))) ans += max(x, big.pop()) print(ans + sum(big)) if __name__ == '__main__': main() ```
output
1
63,315
14
126,631
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,316
14
126,632
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` n,d,m=map(int,input().split()) a=list(map(int,input().split())) e,f=[i for i in a if i<=m],[i for i in a if i>m] e.sort(reverse=True) f.sort(reverse=True) c=len(e) if c==n: print(sum(a)) exit() l=1 r=min((n-1)//(d+1)+1,len(f)) #k's range #e,f 's sum ans=sum(e) nowe,nowf=0,0 #print(l,r) #print(n-(1+(l-1)*(d+1)),l) #print(e,f) #print(l,r) #print(e) #print(f) for i in range(l,r+1): if i==l: nowe=sum(e[:n-(1+(l-1)*(d+1))]) nowf=sum(f[:l]) ans=max(nowe+nowf,ans) continue nowe-=sum(e[n-(1+(i-1)*(d+1)):n-(1+(i-1-1)*(d+1))]) nowf+=f[i-1] ans=max(nowe+nowf,ans) print(ans) ```
output
1
63,316
14
126,633
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,317
14
126,634
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import sys, collections n, d, m = map(int, sys.stdin.readline().split()) arr = list(map(int, sys.stdin.readline().split())) big = [] small = [] for i in arr: if i > m: big.append(i) else: small.append(i) big.sort(reverse=True) small.sort(reverse=True) big = [0] + big small = [0] + small for i in range(2, len(big)): big[i] = big[i] + big[i - 1] for i in range(2, len(small)): small[i] = small[i] + small[i - 1] ans = 0 for i in range(len(small)): left_days = n - i if left_days < 0: continue if left_days % (d + 1) == 0: k = left_days // (d + 1) else: k = (left_days // (d + 1)) + 1 if k >= len(big): k = len(big) - 1 if k < 0: k = 0 ans = max(ans, small[i] + big[k]) print(ans) ```
output
1
63,317
14
126,635
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,318
14
126,636
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` #import sys #input = sys.stdin.readline from itertools import accumulate def main(): n, d, m = map( int, input().split()) A = list( map( int, input().split())) A_low = [] A_high = [] for a in A: if a <= m: A_low.append(a) else: A_high.append(a) Ll = len(A_low) Lh = len(A_high) A_low.sort(reverse=True) A_high.sort(reverse=True) accA_high = [0] + list( accumulate(A_high)) # print(accA_high) t = (n+d)//(d+1) if t <= Lh: ans = accA_high[(n+d)//(d+1)] else: ans = accA_high[-1] low = 0 # print(ans) for i in range(Ll): low += A_low[i] t = (n-i-1+d)//(d+1) if t <= Lh: high = accA_high[(n-i-1+d)//(d+1)] else: high = accA_high[-1] # print(i, (n-i-1+d)//(d+1)) # print(low, high) if low + high > ans: ans = low+high print(ans) if __name__ == '__main__': main() ```
output
1
63,318
14
126,637
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,319
14
126,638
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import sys input = sys.stdin.readline n, d, m = map(int, input().split()) vals = sorted([int(i) for i in input().split()], reverse = True) over, under, over_psa, under_psa, ans = [i for i in vals if i > m], [i for i in vals if i <= m], [0], [0], 0 for i in range(len(over)): over_psa.append(over_psa[i] + over[i]) for i in range(n*2): # prevent indexerror over_psa.append(over_psa[-1]) for i in range(len(under)): under_psa.append(under_psa[i] + under[i]) for i in range(n*2): # pervent indexerror under_psa.append(under_psa[-1]) for i in range(len(over) + 1): if (i - 1) * (d + 1) + 1 <= n: ans = max(ans, over_psa[i] + under_psa[n - ((i - 1) * (d + 1) + 1)]) print(ans) ```
output
1
63,319
14
126,639
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,320
14
126,640
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import bisect n, d, m = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() f = {0:0} for i in range(n): f[i+1] = f[i] + arr[i] k = bisect.bisect_right(arr, m) ans = 0 for nlo in range(k+1): nhg = (n-nlo+d)//(d+1) if nhg <= n-k: ans = max(ans, f[n] - f[n-nhg] + f[k] - f[k-nlo]) print(ans) ```
output
1
63,320
14
126,641
Provide tags and a correct Python 3 solution for this coding contest problem. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48.
instruction
0
63,321
14
126,642
Tags: dp, greedy, sortings, two pointers Correct Solution: ``` import sys readline = sys.stdin.buffer.readline ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) def solve(): n, d, m = nm() a = nl() f = list() g = list() for x in a: if x > m: f.append(x) else: g.append(x) f.sort(reverse=True) g.sort(reverse=True) ng = len(g) a = [0] * (ng + 1) for i in range(ng): a[i+1] = a[i] + g[i] ans = a[ng] cur = 0 for i in range(len(f)): if i + 1 + i * d > n: break cur += f[i] v = n - i * d - i - 1 if v > ng: v = ng if v < 0: v = 0 if ans < cur + a[v]: ans = cur + a[v] print(ans) return solve() ```
output
1
63,321
14
126,643
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` import sys input = sys.stdin.readline n, d, m = map(int, input().split()) vals = sorted([int(i) for i in input().split()], reverse = True) over, under, over_psa, under_psa, ans = [i for i in vals if i > m], [i for i in vals if i <= m], [0], [0], 0 for i in range(len(over)): over_psa.append(over_psa[i] + over[i]) for i in range(n + len(over)): # prevent indexerror over_psa.append(over_psa[-1]) for i in range(len(under)): under_psa.append(under_psa[i] + under[i]) for i in range(n + len(under)): # prevent indexerror under_psa.append(under_psa[-1]) for i in range(len(over) + 1): if (i - 1) * (d + 1) + 1 <= n: ans = max(ans, over_psa[i] + under_psa[n - max(((i - 1) * (d + 1) + 1), 0)]) print(ans) ```
instruction
0
63,322
14
126,644
Yes
output
1
63,322
14
126,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` from math import inf, log2 class SegmentTree: def __init__(self, array, func=max): self.n = len(array) self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1 self.func = func self.default = 0 if self.func != min else inf self.data = [self.default] * (2 * self.size) self.process(array) def process(self, array): self.data[self.size : self.size+self.n] = array for i in range(self.size-1, -1, -1): self.data[i] = self.func(self.data[2*i], self.data[2*i+1]) def query(self, alpha, omega): """Returns the result of function over the range (inclusive)!""" if alpha == omega: return self.data[alpha + self.size] res = self.default alpha += self.size omega += self.size + 1 while alpha < omega: if alpha & 1: res = self.func(res, self.data[alpha]) alpha += 1 if omega & 1: omega -= 1 res = self.func(res, self.data[omega]) alpha >>= 1 omega >>= 1 return res def update(self, index, value): """Updates the element at index to given value!""" index += self.size self.data[index] = value index >>= 1 while index: self.data[index] = self.func(self.data[2*index], self.data[2*index+1]) index >>= 1 from bisect import bisect_left, bisect_right class Result: def __init__(self, index, value): self.index = index self.value = value class BinarySearch: def __init__(self): pass @staticmethod def greater_than(num: int, func, size: int = 1): """Searches for smallest element greater than num!""" if isinstance(func, list): index = bisect_right(func, num) if index == len(func): return Result(None, None) else: return Result(index, func[index]) else: alpha, omega = 0, size - 1 if func(omega) <= num: return Result(None, None) while alpha < omega: if func(alpha) > num: return Result(alpha, func(alpha)) if omega == alpha + 1: return Result(omega, func(omega)) mid = (alpha + omega) // 2 if func(mid) > num: omega = mid else: alpha = mid @staticmethod def less_than(num: int, func, size: int = 1): """Searches for largest element less than num!""" if isinstance(func, list): index = bisect_left(func, num) - 1 if index == -1: return Result(None, None) else: return Result(index, func[index]) else: alpha, omega = 0, size - 1 if func(alpha) >= num: return Result(None, None) while alpha < omega: if func(omega) < num: return Result(omega, func(omega)) if omega == alpha + 1: return Result(alpha, func(alpha)) mid = (alpha + omega) // 2 if func(mid) < num: alpha = mid else: omega = mid # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def pre(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi def prod(a): ans = 1 for each in a: ans = (ans * each) return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y bs = BinarySearch() for _ in range(int(input()) if not True else 1): n,d,m = map(int, input().split()) a = sorted(list(map(int, input().split()))) ind = bs.greater_than(m, a).index if ind is None: print(sum(a)) continue vals = n-ind ans = 0 st = SegmentTree(a, func=lambda a,b:a+b) xi = None for i in range(1, vals+1): anss = a[-1] left = n - 1 - (d+1)*(i-1) if left < 0: if d >= n and i > 1: ans = max(ans, anss) break xi = i break anss += st.query(n-2-i+2, n-2) anss+=st.query(max(0, ind-left), ind-1) ans = max(ans, anss) if 1: cur = -1 used = 0 anss = 0 while used < n: anss += a[cur] cur -= 1 used += d+1 cur = ind-1 while used < n: anss += a[cur] cur -= 1 used += 1 ans = max(ans, anss) anss = 0 cn =0 for i in range(ind-1, -1, -1): if cn == n-1:break anss += a[i] cn += 1 anss += a[-1] print(max(ans, anss)) ```
instruction
0
63,323
14
126,646
Yes
output
1
63,323
14
126,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` def solve(n, m, d, nums): if not nums: return 0 nums.sort(key=lambda x: x*-1) smalls = [0] bigs = [0] for num in nums: if num <= m: smalls.append(smalls[-1]+num) else: bigs.append(bigs[-1]+num) ans = 0 for x, big in enumerate(bigs): y = n if not x else n-(x-1)*(d+1)-1 y = min(y, len(smalls)-1) if y < 0: break ans = max(ans, smalls[y]+big) return ans n, d, m = list(map(int, input().split(" "))) nums = list(map(int, input().split(" "))) print(solve(n, m, d, nums)) ```
instruction
0
63,324
14
126,648
Yes
output
1
63,324
14
126,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` from collections import defaultdict from queue import deque def arrinp(): return [*map(int, input().split(' '))] def mulinp(): return map(int, input().split(' ')) def intinp(): return int(input()) def solution(): n,d,m = mulinp() a = arrinp() a.sort() dp=[0] c=0 for j in range(n): dp.append(dp[-1]+a[j]) if a[j]>m: c=c+1 res=dp[n-c] if a[-1]>m: res=res+a[-1] s=res for j in range(1,c): p=c-1-j q=dp[n-1]-dp[n-1-j] nd=max(0,j*d-p) if nd>n-c: break else: s=max(s,res+q-dp[nd]) print(s) testcases = 1 # testcases = int(input()) for _ in range(testcases): solution() ```
instruction
0
63,325
14
126,650
Yes
output
1
63,325
14
126,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` n,d,m=map(int,input().split()) a=list(map(int,input().split())) e,f=[i for i in a if i<=m],[i for i in a if i>m] e.sort(reverse=True) f.sort(reverse=True) c=len(e) if c==n: print(sum(a)) exit() l=-((-n+c+1)//(d+1))+1 r=min((n-1)//(d+1)+1,len(f)) #k's range #e,f 's sum ans=0 nowe,nowf=0,0 #print(l,r) #print(n-(1+(l-1)*(d+1)),l) #print(e,f) #print(l,r) #print(e) #print(f) for i in range(l,r+1): if i==l: nowe=sum(e[:n-(1+(l-1)*(d+1))]) nowf=sum(f[:l]) ans=max(nowe+nowf,ans) continue nowe-=sum(e[n-(1+(i-1)*(d+1)):n-(1+(i-1-1)*(d+1))]) nowf+=f[i-1] ans=max(nowe+nowf,ans) print(ans) ```
instruction
0
63,326
14
126,652
No
output
1
63,326
14
126,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` l=input().split() n=int(l[0]) d=int(l[1]) m=int(l[2]) l=input().split() li=[int(i) for i in l] great=[] less=[] for i in li: if(i>m): great.append(i) else: less.append(i) great.sort() less.sort() prefg=[0] prefl=[0] gr=len(great) le=len(less) for i in great: prefg.append(prefg[-1]+i) for i in less: prefl.append(prefl[-1]+i) maxa=10**18 z=sum(li) if(gr==0): print(z) quit() for i in range(1,(n//d)+1): for j in range(d+1): maxas=i rest=(i-1)*d+j if(maxas+(i-1)*d+j>n): continue if(gr<maxas): continue ans=prefg[gr-maxas] rest=rest-(gr-maxas) if(rest>le or rest<0): continue ans=ans+prefl[rest] maxa=min(maxa,ans) print(z-maxa) ```
instruction
0
63,327
14
126,654
No
output
1
63,327
14
126,655
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` import sys input = sys.stdin.readline n, d, m = map(int,input().split()) A = list(map(int,input().split())) from random import randint # for ii in range(10000): # n = 5 # d = 2 # m = 10 # A = [randint(1, 20) for i in range(n)] # A = [20, 13, 14, 18, 15] B = [] C = [] for i in range(n): if A[i] > m: B.append(A[i]) else: C.append(A[i]) B.sort(reverse=True) C.sort(reverse=True) # print(B,C) ans = 0 if len(B) == 0: ans = sum(A) else: bmin = (n-len(C)-1) // (d+1) + 1 bmax = min(len(B), (n-1)//(d+1) + 1) bind = bmin cind = n-((bmin-1)*(d+1)+1) - 1 ans0 = sum(B[:bmin]) ans0 += sum(C[:cind+1]) ans = ans0 # print(bind,cind) for i in range(cind-len(C)+1): C.append(0) while bind < bmax: ans0 += B[bind] bind += 1 for i in range(3): if len(C) > 0 and cind >= 0: # print(cind) ans0 -= C[cind] cind -= 1 ans = max(ans0, ans) # print(bind,cind) print(ans) # import itertools # D = list(itertools.permutations([i for i in range(n)])) # ans1 = 0 # for i in range(len(D)): # ans2 = 0 # mu = 0 # for j in range(n): # if mu == 0: # ans2 += A[D[i][j]] # if A[D[i][j]] > m: # mu = d # else: # mu -= 1 # ans1 = max(ans1,ans2) # if ans!=ans1: # print(ans,ans1,A) ```
instruction
0
63,328
14
126,656
No
output
1
63,328
14
126,657
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days. In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day. Du will chat in the group for n days. On the i-th day: * If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood. * Otherwise, Du won't do anything. Boboniu's mood is a constant m. On the i-th day: * If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, โ‹…โ‹…โ‹…, min(i+d,n)-th days. * Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak. Du asked you to find the maximum total fun factor among all possible permutations of a. Input The first line contains three integers n, d and m (1โ‰ค dโ‰ค nโ‰ค 10^5,0โ‰ค mโ‰ค 10^9). The next line contains n integers a_1, a_2, โ€ฆ,a_n (0โ‰ค a_iโ‰ค 10^9). Output Print one integer: the maximum total fun factor among all permutations of a. Examples Input 5 2 11 8 10 15 23 5 Output 48 Input 20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7 Output 195 Note In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be: 1. Make fun of Boboniu with fun factor 15. 2. Be muzzled. 3. Be muzzled. 4. Make fun of Boboniu with fun factor 10. 5. Make fun of Boboniu with fun factor 23. Thus the total fun factor is 48. Submitted Solution: ``` import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n,d,m= list(map(int,input().split())) array = list(map(int,input().split())) array.sort() les=[] big=[] for i in range(n): if array[i]<=m: les.append(array[i]) else: big.append(array[i]) res=sum(les) len1=len(les) len2=n-len1 s,e=0,len2-2 ans=0 #print(array,big) while s<len1 and e>=0: #print(s,e) if (s+d)<=len1: sumup=0 for i in range(s,s+d): sumup+=les[i] #print(sumup,"sum") if sumup<=big[e]: ans+=(big[e]) e-=1 s+=d else: break else: break #print(s,ans) for i in range(s,len1): ans+=les[i] if len2>=1: ans+=big[len2-1] print(ans) ```
instruction
0
63,329
14
126,658
No
output
1
63,329
14
126,659
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,526
14
127,052
Tags: implementation Correct Solution: ``` import sys n=int(input()) l=list(map(int,input().split())) l1=l.copy() l1.sort() if(n<=2): if(l==l1): print(0,0) else: print(1,2) sys.exit() for i in l: if(i==1): break else: l.remove(i) l.append(i) if(l==l1): print(0,0) else: for i in range(n-1): if(abs(l[i]-l[i+1])>1): a=i+1+1 b=l[i+1] l2=l[:i+1] l3=l[i+1:b] l4=l[b:] l3=l3[::-1] l5=l2+l3+l4 break if(l5==l1): print(a,b) else: print(0,0) ```
output
1
63,526
14
127,053
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,527
14
127,054
Tags: implementation Correct Solution: ``` r=range i=input i() l=list(map(int,i().split())) #print(l) s=0 p=q=-1 for _ in r(len(l)): if(l[_]!=_+1): p=_+1 q=l[_] break if(p!=-1): #print(p,q) l[p-1:q]=sorted(l[p-1:q]) #print(l) f=1*(p!=-1) for x in r(len(l)): if(l[x]!=x+1):f=0 if(f):print("%d %d"%(p,q)) else:print("0 0") ```
output
1
63,527
14
127,055
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,528
14
127,056
Tags: implementation Correct Solution: ``` n = int(input()) arr = [0] + list(map(int,input().split())) l = 1 r = n while l <= n and arr[l] == l: l += 1 while r >= 0 and arr[r] == r: r -= 1 if l < n and arr[r:l-1:-1] == list(range(l,r+1)):print(l,r) else: print(0,0) ```
output
1
63,528
14
127,057
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,529
14
127,058
Tags: implementation Correct Solution: ``` n=int(input()) L=list(map(int,input().split())) start=0 end=n-1 while(L[start]==start+1): start+=1 if(start==n): break while(L[end]==end+1): end-=1 if(end==-1): break if(start>end): print("0 0") else: pre=L[:start] mid=L[start:end+1] post=L[end+1:] if(pre+sorted(mid)+post==sorted(L) and mid==sorted(mid,reverse=True)): print(start+1,end+1) else: print("0 0") ```
output
1
63,529
14
127,059
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,530
14
127,060
Tags: implementation Correct Solution: ``` def f(): n=int(input()) L=input().split() etat=0 debut,fin=0,0 for i in range(n): num=int(L[i]) if etat==1: if (1+i+num)!=debut+fin: return '0 0' elif i+1==fin: etat=2 elif (i+1)!=num: if etat==0: etat=1 debut=i+1 fin=num elif etat==2: return '0 0' return str(debut)+' '+str(fin) print(f()) ```
output
1
63,530
14
127,061
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,531
14
127,062
Tags: implementation Correct Solution: ``` def spoilt(arr): if arr==sorted(arr): print(0,0) return "" arr=[0]+arr ans=["a",0] cnt=0 temp=0 flag=False for i in range(1,len(arr)): if arr[i]!=i and flag==False : ans[0]=i temp=i flag=True if arr[i]==temp: ans[1]=i break arr=arr[:ans[0]]+arr[ans[0]:ans[1]+1][::-1]+arr[ans[1]+1:] if arr!=sorted(arr): print(0,0) return "" print(*ans) return "" a=int(input()) lst=list(map(int,input().strip().split())) print(spoilt(lst)) ```
output
1
63,531
14
127,063
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,532
14
127,064
Tags: implementation Correct Solution: ``` #from dust i have come dust i will be n=int(input()) a=list(map(int,input().split())) l,r=-1,-1 for i in range(n): if a[i]!=i+1: l=i break for i in range(n-1,-1,-1): if a[i]!=i+1: r=i break j=r+1 for i in range(l,r+1): if a[i]==j: j-=1 continue else: print(0,0) exit(0) print(l+1,r+1) ```
output
1
63,532
14
127,065
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change โ€” he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer n (1 โ‰ค n โ‰ค 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 โ‰ค l < r โ‰ค n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one. Examples Input 8 1 6 5 4 3 2 7 8 Output 2 6 Input 4 2 3 4 1 Output 0 0 Input 4 1 2 3 4 Output 0 0
instruction
0
63,533
14
127,066
Tags: implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) l,r=-1,-1 for i in range(n): if a[i]!=i+1: l=i break for i in range(n-1,-1,-1): if a[i]!=i+1: r=i break j=r+1 for i in range(l,r+1): if a[i]==j: j-=1 continue else: print(0,0) exit(0) print(l+1,r+1) ```
output
1
63,533
14
127,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today is birthday of a Little Dasha โ€” she is now 8 years old! On this occasion, each of her n friends and relatives gave her a ribbon with a greeting written on it, and, as it turned out, all the greetings are different. Dasha gathered all the ribbons and decided to throw away some of them in order to make the remaining set stylish. The birthday girl considers a set of ribbons stylish if no greeting written on some ribbon is a substring of another greeting written on some other ribbon. Let us recall that the substring of the string s is a continuous segment of s. Help Dasha to keep as many ribbons as possible, so that she could brag about them to all of her friends. Dasha cannot rotate or flip ribbons, that is, each greeting can be read in a single way given in the input. Input The first line of the input contains integer n (1 โ‰ค n โ‰ค 750) โ€” the number of Dasha's relatives and friends. Each of the next n lines contains exactly one greeting. Each greeting consists of characters 'a' and 'b' only. The total length of all greetings won't exceed 10 000 000 characters. Output In the first line print the maximum size of the stylish set. In the second line print the numbers of ribbons involved in it, assuming that they are numbered from 1 to n in the order they appear in the input. If there are several stylish sets of the maximum size, print any of them. Examples Input 5 abab aba aabab ababb bab Output 2 2 5 Note In the sample, the answer that keeps ribbons 3 and 4 is also considered correct. Submitted Solution: ``` #get input numOfRibbons = int(input()) ribbons = [] ribbonsLenTest = [] #most ribbons is the list of the indeces+1 of the values mostRibbons = [] tempMostRibbons = [] for i in range(numOfRibbons): ribbon = input() ribbons.append(ribbon) ribbonsLenTest.append(list(ribbon)) #print(ribbons) #process the information for i in range(numOfRibbons): tempMostRibbons = [i+1] for j in range(numOfRibbons): if len(ribbonsLenTest[i]) > len(ribbonsLenTest[j]): if ribbons[j] in ribbons[i]: pass else: tempMostRibbons.append(j+1) elif len(ribbonsLenTest[i]) < len(ribbonsLenTest[j]): if ribbons[i] in ribbons[j]: pass else: tempMostRibbons.append(j+1) elif ribbons[i] == ribbons[j]: pass else: tempMostRibbons.append(j+1) mostRibbons.append(tempMostRibbons) #FIGURE OUT THE OUTPUT lengthList = [] for i in range(len(mostRibbons)): lengthList.append(len(mostRibbons[i])) posInLine = lengthList.index(max(lengthList)) outputValue = sorted(mostRibbons[posInLine]) outputValue = str(outputValue).strip('[]') outputValue = outputValue.replace(',','') print(max(lengthList)) print(outputValue) ```
instruction
0
63,542
14
127,084
No
output
1
63,542
14
127,085
Provide tags and a correct Python 3 solution for this coding contest problem. Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once. Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2ยท(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. Input The first line contains three integers n, c1 and c2 (1 โ‰ค n โ‰ค 200 000, 1 โ‰ค c1, c2 โ‰ค 107) โ€” the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils. Output Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once. Examples Input 3 4 1 011 Output 8 Input 4 7 2 1101 Output 18 Note In the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)2 = 8. In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)2 = 9. Thus, the total price for two groups is 18.
instruction
0
63,593
14
127,186
Tags: *special, ternary search Correct Solution: ``` n, c1, c2 = [int(x) for x in input().split(' ')] s = input() cnt = s.count('1') def price(x): return c1 + c2 * (x - 1) ** 2 prices = [] for i in range(1, cnt + 1): bigGroupsPeople = n // i + 1 numBigGroups = n % i smallGroupsPeople = n // i numSmallGroups = i - n % i totalPrice = numBigGroups * price(bigGroupsPeople) + numSmallGroups * price(smallGroupsPeople) prices.append(totalPrice) print(min(prices)) ```
output
1
63,593
14
127,187
Provide tags and a correct Python 3 solution for this coding contest problem. Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once. Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2ยท(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. Input The first line contains three integers n, c1 and c2 (1 โ‰ค n โ‰ค 200 000, 1 โ‰ค c1, c2 โ‰ค 107) โ€” the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils. Output Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once. Examples Input 3 4 1 011 Output 8 Input 4 7 2 1101 Output 18 Note In the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)2 = 8. In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)2 = 9. Thus, the total price for two groups is 18.
instruction
0
63,594
14
127,188
Tags: *special, ternary search Correct Solution: ``` t = input() t = t.split() n = int(t[0]) c1 = int(t[1]) c2 = int(t[2]) t = input() d = 0 for i in t: if(i=="1"): d = d+1 min = 10**1488 for i in range(1, d+1): t = c1*i + i*c2*(((n//i)-1)**2) + c2*(n%i)*(2*(n//i)-1) if t<min: min = t print(min) # Made By Mostafa_Khaled ```
output
1
63,594
14
127,189
Provide tags and a correct Python 3 solution for this coding contest problem. Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once. Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2ยท(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. Input The first line contains three integers n, c1 and c2 (1 โ‰ค n โ‰ค 200 000, 1 โ‰ค c1, c2 โ‰ค 107) โ€” the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils. Output Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once. Examples Input 3 4 1 011 Output 8 Input 4 7 2 1101 Output 18 Note In the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)2 = 8. In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)2 = 9. Thus, the total price for two groups is 18.
instruction
0
63,595
14
127,190
Tags: *special, ternary search Correct Solution: ``` t = input() t = t.split() n = int(t[0]) c1 = int(t[1]) c2 = int(t[2]) t = input() d = 0 for i in t: if(i=="1"): d = d+1 min = 10**1488 for i in range(1, d+1): t = c1*i + i*c2*(((n//i)-1)**2) + c2*(n%i)*(2*(n//i)-1) if t<min: min = t print(min) ```
output
1
63,595
14
127,191
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,032
14
128,064
Tags: greedy, math, sortings Correct Solution: ``` n=int(input()) a=[0]*n b=[0]*n c=[0]*n for i in range (0,n): a[i],b[i]=map(int,input().split()) c[i]=b[i]-a[i] c=sorted(c) s=0 for i in range (0,n): s+=c[i]*(n-1-i) print(s+(n-1)*sum(a)) ```
output
1
64,032
14
128,065
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,033
14
128,066
Tags: greedy, math, sortings Correct Solution: ``` def in_int(): return int(input()) def in_list(): return [int(x) for x in input().split()] n=int(input()) ns=[] ans=0 for i in range(n): x,y=in_list() ns.append(x-y) ans+=-x+y*n ns.sort(reverse=True) for i in range(n): c=ns[i] ans+=c*(i+1) print(ans) ```
output
1
64,033
14
128,067
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,034
14
128,068
Tags: greedy, math, sortings Correct Solution: ``` n = int(input()) lis = [] for i in range(n): a, b = map(int, input().split()) c = (a - b) lis.append([a, b, c]) lis.sort(key = lambda x: x[2]) lis = lis[::-1] res = 0 for j in range(n): res += lis[j][0]*j+lis[j][1]*(n-j-1) print(res) ```
output
1
64,034
14
128,069
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,035
14
128,070
Tags: greedy, math, sortings Correct Solution: ``` n=int(input()) li=[] ans=0 for i in range(n): a,b=map(int,input().split()) li.append(b-a) ans+=(b*n-a) li.sort() for i in range(n): ans-=li[i]*(i+1) print(ans) ```
output
1
64,035
14
128,071
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,036
14
128,072
Tags: greedy, math, sortings Correct Solution: ``` n, arr, ans= int(input()), list(), 0 for i in range(n): [a, b] = [int(i) for i in input().split()] arr += [[b - a, a, b]] arr = sorted(arr) for i in range(n): ans += i * arr[i][1] + (n - i - 1) * arr[i][2] print(ans) ```
output
1
64,036
14
128,073
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,037
14
128,074
Tags: greedy, math, sortings Correct Solution: ``` n = int(input()) people = list() for i in range(n): people.append([int(el) for el in input().split()]) disbal = [(i, el[1]-el[0])for i, el in enumerate(people)] disbal = sorted(disbal, key=lambda x: x[1]) power = 0 for cur, el in enumerate(disbal): power += people[el[0]][0]*cur+people[el[0]][1]*(n-cur-1) print(power) ```
output
1
64,037
14
128,075
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,038
14
128,076
Tags: greedy, math, sortings Correct Solution: ``` import collections as cc import itertools as it I=lambda:list(map(int,input().split())) n,=I() ans=0 d=[] for i in range(n): x,y=I() ans+=y*(n-1) d.append(x-y) d.sort(reverse=True) for i in range(n): ans+=d[i]*i print(ans) ```
output
1
64,038
14
128,077
Provide tags and a correct Python 3 solution for this coding contest problem. During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n high school students numbered from 1 to n. Initially, each student i is on position i. Each student i is characterized by two numbers โ€” a_i and b_i. Dissatisfaction of the person i equals the product of a_i by the number of people standing to the left of his position, add the product b_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i, which is on the position j, equals a_i โ‹… (j-1) + b_i โ‹… (n-j). The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction. Although Stas is able to solve such problems, this was not given to him. He turned for help to you. Input The first line contains a single integer n (1 โ‰ค n โ‰ค 10^5) โ€” the number of people in the queue. Each of the following n lines contains two integers a_i and b_i (1 โ‰ค a_i, b_i โ‰ค 10^8) โ€” the characteristic of the student i, initially on the position i. Output Output one integer โ€” minimum total dissatisfaction which can be achieved by rearranging people in the queue. Examples Input 3 4 2 2 3 6 1 Output 12 Input 4 2 4 3 3 7 1 2 3 Output 25 Input 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 Output 1423 Note In the first example it is optimal to put people in this order: (3, 1, 2). The first person is in the position of 2, then his dissatisfaction will be equal to 4 โ‹… 1+2 โ‹… 1=6. The second person is in the position of 3, his dissatisfaction will be equal to 2 โ‹… 2+3 โ‹… 0=4. The third person is in the position of 1, his dissatisfaction will be equal to 6 โ‹… 0+1 โ‹… 2=2. The total dissatisfaction will be 12. In the second example, you need to put people in this order: (3, 2, 4, 1). The total dissatisfaction will be 25.
instruction
0
64,039
14
128,078
Tags: greedy, math, sortings Correct Solution: ``` # cook your dish here n=int(input()) p1=0 p2=0 l=[] s=0 for k in range(1,n+1): x=str(input()) x=x.split(' ') x=[int(y) for y in x if y !=''] p1=x[0]+p1 p2=p2+x[1] l.append(x[0]-x[1]) l.sort(reverse=True) for k in range(1,n+1): s=s+k*l[k-1] print(s+n*p2-p1) ```
output
1
64,039
14
128,079