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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
import math
import bisect
import heapq
from collections import defaultdict
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def mulinv(b, n):
g, x, _ = egcd(b, n)
if g == 1:
return x % n
primes = []
def isprime(n):
for d in range(2, int(math.sqrt(n))+1):
if n%d==0:
return False
return True
def argsort(ls):
return sorted(range(len(ls)), key=ls.__getitem__)
def f(p=0):
if p==1:
return map(int, input().split())
elif p==2:
return list(map(int, input().split()))
else:
return int(input())
n = f()
a = []
b = []
c = []
sm = 0
for i in range(n):
x, y = f(1)
a.append(x)
b.append(y)
c.append(x-y)
ind = argsort(c)
for i in range(n):
sm+=(n-i-1)*c[ind[i]]+b[i]*(n-1)
print(sm)
``` | instruction | 0 | 64,040 | 14 | 128,080 |
Yes | output | 1 | 64,040 | 14 | 128,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
n = int(input())
arr = [[0] + [int(i) for i in input().split()] for i in range(n)]
for i in range(n):
arr[i][0] = arr[i][1] - arr[i][2]
arr = sorted(arr)[::-1]
ans = 0
for i in range(n):
ans += i * arr[i][1] + (n - i - 1) * arr[i][2]
print(ans)
``` | instruction | 0 | 64,041 | 14 | 128,082 |
Yes | output | 1 | 64,041 | 14 | 128,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
n = int(input())
a = [list(map(int,input().split())) for i in range(n)]
a.sort(key = lambda x:-x[0]+x[1])
s = 0
for i in range(n):
s += a[i][0]*(i)+a[i][1]*(n-i-1)
print(s)
``` | instruction | 0 | 64,042 | 14 | 128,084 |
Yes | output | 1 | 64,042 | 14 | 128,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 3 13:32:13 2019
@author: plosi
"""
def compute(s,n):
s_new=sorted(s,reverse=True)
ret=0
for i in range(1,n+1):
ret=ret+i*s_new[i-1]
return ret
def main():
n=int(input())
s=[]
b_sum=0
a_sum=0
for i in range(n):
a,b=input().split(" ")
a=int(a)
b=int(b)
a_sum=a+a_sum
b_sum=b+b_sum
s.append(a-b)
ret=compute(s,n)
print(ret+b_sum*n-a_sum)
main()
``` | instruction | 0 | 64,043 | 14 | 128,086 |
Yes | output | 1 | 64,043 | 14 | 128,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
n = int(input())
L = []
for i in range(n):
a,b = map(int,input().split())
L.append([a,b])
L.sort(key = lambda x : -x[0]-x[1])
ans = 0
for i in range(n):
ans += L[i][0]*i + L[i][1]*(n-i-1)
print(ans)
``` | instruction | 0 | 64,044 | 14 | 128,088 |
No | output | 1 | 64,044 | 14 | 128,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
n = int(input())
q = []
for i in range(n):
q.append([int(j) for j in input().split(' ')])
# print(q)
q.sort(key = lambda x: (-x[0], x[1]))
print(q)
res = 0
for i in range(n):
a, b = q[i]
res += a * i + b * (n-i-1)
print(res)
``` | instruction | 0 | 64,045 | 14 | 128,090 |
No | output | 1 | 64,045 | 14 | 128,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
n = int(input())
m = [list(map(int, input().split())) for i in range(n)]
# print(m)
m.sort(key=lambda x:x[1]-x[0])
print(sum(m[j][0]*(j)+m[j][1]*(n-j-1)) for j in range(n))
``` | instruction | 0 | 64,046 | 14 | 128,092 |
No | output | 1 | 64,046 | 14 | 128,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Submitted Solution:
```
from queue import PriorityQueue
l= PriorityQueue()
n=int(input())
for i in range(n):
a,b=map(int,input().split())
l.put([a-b,a,b])
x=0
j=n
for i,a,b in l.queue:
x+=j*(i)-a+b*n
j-=1
print(x)
``` | instruction | 0 | 64,047 | 14 | 128,094 |
No | output | 1 | 64,047 | 14 | 128,095 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,149 | 14 | 128,298 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
for _ in range(int(input())):
if _:input()
n,m=map(int,input().split())
a=[]
b=[]
for i in range(m):
v1,v2=map(int,input().split())
a.append(v1)
b.append((v2,v1))
a.sort(reverse=True)
b.sort(reverse=True)
s,l,v,c=[0]*4
for i in range(m):
t=n-l
while c<m and t and a[c]>b[i][0]:
s+=a[c]
l+=1
c+=1
t-=1
if t:v=max(v,s+min(b[i])+b[i][0]*(t-1))
else:v=max(v,s)
if n==l:break
print(v)
``` | output | 1 | 64,149 | 14 | 128,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,150 | 14 | 128,300 |
Tags: binary search, brute force, data structures, dfs and similar, 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 output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
t = int(input())
ans_a = [''] * t
for ti in range(t):
if ti:
input()
n, m = map(int, input().split())
a = [10**9 + 100] * (m + 1)
acc = [0] * (m + 1)
b = [0] * (m + 1)
for i, (ai, bi) in enumerate(sorted([list(map(int, input().split())) for _ in range(m)], reverse=True), start=1):
acc[i] = a[i] = ai
b[i] = bi
for i in range(1, m):
acc[i + 1] += acc[i]
ans = acc[min(n, m)] + max(0, n - m) * max(b)
l = m
for i in sorted(range(1, m + 1), key=lambda i: b[i]):
while a[l] <= b[i]:
l -= 1
if l >= n:
continue
if a[l] > a[i]:
ans = max(ans, acc[l] + a[i] + b[i] * (n - l - 1))
else:
ans = max(ans, acc[l] + b[i] * (n - l))
ans_a[ti] = str(ans)
output(*ans_a)
if __name__ == '__main__':
main()
``` | output | 1 | 64,150 | 14 | 128,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,151 | 14 | 128,302 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
from bisect import bisect_left
t = int(input())
for case in range(t):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
aa = [e for e, _ in ab]
aa.sort()
acc = [0] * (m + 1)
for i in range(m, 0, -1):
acc[i-1] = acc[i] + aa[i-1]
if n > m:
ans = 0
else:
ans = sum(aa[-n:])
for a, b in ab:
i = bisect_left(aa, b)
cnt = min(m - i, n)
sm = acc[m - cnt]
sm += b * (n - cnt)
if a < b:
sm -= b
sm += a
ans = max(ans, sm)
print(ans)
if case != t - 1:
input()
``` | output | 1 | 64,151 | 14 | 128,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,152 | 14 | 128,304 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
import bisect
t = int(input())
for _ in range(t):
n, m = list(map(int, input().split()))
li = []
for i in range(m):
a,b = list(map(int, input().split()))
li.append([a,b])
li.sort(key=lambda x: x[0])
li[-1].append(li[-1][0])
for i in range(m-2, -1, -1):
li[i].append(li[i+1][2]+li[i][0])
#print(li)
ans = 0
for ele in li:
c = m-bisect.bisect_right(li, [ele[1], 10**10, 10**10])
#print(bisect.bisect_right(li, [ele[1], 10**10, 10**10]))
temp = ele[0]
rem = n-1
#print(rem, temp, c)
if(rem>0 and c>rem and c>0):
temp += li[-1*rem][2]
if(ele[0]>=li[-1*rem][0]):
#print("here", li[(-1*rem)][0])
temp += li[(-1*rem)-1][0]
temp -= ele[0]
rem = 0
if(rem>0 and c==rem and c>0):
temp += li[-1*rem][2]
if(ele[0]>=li[-1*rem][0]):
temp -= ele[0]
rem = 1
else:
rem = 0
if(rem>0 and c<rem and c>0):
rem -= c
temp += li[-1*c][2]
if(ele[0]>=li[-1*c][0]):
temp -= ele[0]
rem += 1
#print(rem, temp)
if(rem>0):
temp += ele[1]*rem
#print(rem, temp)
if(temp>ans):
ans = temp
#print(rem, temp)
#print()
print(ans)
if(_<t-1):
input()
``` | output | 1 | 64,152 | 14 | 128,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,153 | 14 | 128,306 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
for _ in range(int(input())):
if _: input()
n,m = map(int,input().split())
a1 = sorted( (tuple(map(int,input().split())) for i in range(m)),reverse=True)
a2 = sorted(a1,key=lambda x: x[1],reverse=True)
a1.append((0,0)) ; r=0;s=0;ans=0
for x, y in a2 :
while r < n-1 and a1[r][0] > y :
s+= a1[r][0] ; r+= 1
if x> y and a1[r][0] < x :
cs = s + (n-r)*y
else :
cs = s+x+(n-r-1)*y
ans = max(cs,ans)
print(ans,flush=False)
``` | output | 1 | 64,153 | 14 | 128,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,154 | 14 | 128,308 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
import sys, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s); sys.stdout.write('\n')
def wi(n): sys.stdout.write(str(n)); sys.stdout.write('\n')
def wia(a, sep=' '): sys.stdout.write(sep.join([str(x) for x in a])); sys.stdout.write('\n')
def solve(n, m, a):
a = sorted(a, reverse=True)
pre = [0] * (m + 1)
for i in range(m):
pre[i + 1] = pre[i] + a[i][0]
nm = min(n, m)
best = pre[nm]
for i in range(m):
x = a[i][1]
lo = -1
hi = m
while hi > lo + 1:
mid = (lo + hi) // 2
if a[mid][0] >= x:
lo = mid
else:
hi = mid
if hi >= n:
continue
s = pre[hi]
rem = n - hi
if i >= hi:
rem -= 1
s += a[i][0]
best = max(best, s + a[i][1] * rem)
return best
def main():
for _ in range(ri()):
n, m = ria()
a = []
for i in range(m):
a.append(ria())
wi(solve(n, m, a))
rs()
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024*8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
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, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
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()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
``` | output | 1 | 64,154 | 14 | 128,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,155 | 14 | 128,310 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
def binary_search(list, item):
low = 0
high = len(list) - 1
while low <= high:
mid = (low + high) // 2
guess = list[mid][0]
if guess == item:
return mid
if guess < item:
high = mid - 1
else:
low = mid + 1
else:
return low
for i in range(int(input())):
if i:
input()
n, m = list(map(int, input().split()))
d = [0]*m
for j in range(m):
d[j] = tuple(map(int, input().split()))
d.sort(reverse=True)
pre_d = [0]*m
pre_d[0] = d[0][0]
for j in range(1, m):
pre_d[j] = pre_d[j-1] + d[j][0]
s = 0
b_max = 0
for j in range(m):
if d[j][1] > b_max:
fin = binary_search(d, d[j][1])
if fin:
if fin > j:
if fin <= n:
s_temp = pre_d[fin-1] + d[j][1]*(n-fin)
else:
s_temp = pre_d[n-1]
else:
if fin <= n-1:
s_temp = pre_d[fin-1] + d[j][0] + d[j][1]*(n-1-fin)
else:
s_temp = pre_d[n-1]
else:
s_temp = d[j][0] + d[j][1]*(n-1)
if s_temp > s:
s = s_temp
b_max = d[j][1]
print(s)
``` | output | 1 | 64,155 | 14 | 128,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.
Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) β
b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).
Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.
Input
The first line contains the only integer t (1 β€ t β€ 10 000), the number of test cases. It is followed by t descriptions of the test cases.
Each test case description starts with two integers n and m (1 β€ n β€ 10^9, 1 β€ m β€ 100 000), the number of flowers Vladimir needs to choose and the number of types of available flowers.
The following m lines describe the types of flowers: each line contains integers a_i and b_i (0 β€ a_i, b_i β€ 10^9) for i-th available type of flowers.
The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100 000.
Output
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n flowers optimally.
Example
Input
2
4 3
5 0
1 4
2 2
5 3
5 2
4 2
3 1
Output
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5 + (1 + 2 β
4) = 14.
In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5 + 1 β
2) + (4 + 1 β
2) + 3 = 16. | instruction | 0 | 64,156 | 14 | 128,312 |
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers
Correct Solution:
```
from sys import stdin
TT = int(stdin.readline())
for loop in range(TT):
n,m = map(int,stdin.readline().split())
ba = []
alis = []
for i in range(m):
a,b = map(int,stdin.readline().split())
ba.append((b,a))
alis.append(a)
alis.sort()
alis.reverse()
ss = [0]
for i in range(m):
ss.append(ss[-1] + alis[i])
ans = 0
for i in range(m):
b,a = ba[i]
l = -1
r = m
while r-l != 1:
mid = (l+r)//2
if alis[mid] >= b:
l = mid
else:
r = mid
if l == -1:
ans = max(ans , a + b * (n-1))
continue
if alis[l] > a:
if l+1+1 <= n:
ans = max( ans , a + ss[l+1] + b * (n-(l+2)) )
else:
ans = max( ans , a + ss[n-1])
else:
if l+1 <= n:
ans = max( ans , ss[l+1] + b * (n-(l+1)) )
else:
ans = max( ans , ss[n] )
#print (a,b,l,ans)
print (ans)
if loop != TT-1:
stdin.readline()
``` | output | 1 | 64,156 | 14 | 128,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,243 | 14 | 128,486 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
n = int(input())
p = []
for i in range(n):
la = list(map(float,input().split()))
p.append(la)
full_bit = (1<<n) - 1
dp = [0]*(full_bit) + [1]
for i in range(full_bit,0,-1):
cunt = bin(i)[2:].count('1')
# print(cunt)
if cunt == 1 or dp[i] == 0:
continue
mul = 1/((cunt*(cunt-1))>>1)
for x in range(n):
if (i & (1<<x)) == 0:
continue
for y in range(x+1,n):
if (i & (1<<y)) == 0:
continue
dp[i-(1<<y)]+=dp[i]*p[x][y]*mul
dp[i-(1<<x)]+=dp[i]*p[y][x]*mul
ans = []
for i in range(n):
ans.append(dp[1<<i])
print(*ans)
``` | output | 1 | 64,243 | 14 | 128,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,244 | 14 | 128,488 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
from sys import stdin
input=stdin.readline
def count(n):
value=0
while(n):
n &= (n-1)
value+=1
return value
def nc2(n):
return (n*(n - 1))//2
def answer():
dp=[0]*(1 << n) #fishes
#let 1 be alive fishes
#let 0 be dead fishes
dp[(1 << n) - 1]=1 #initially let all be alive
for mask in range((1 << n) - 1,0,-1):
m=count(mask) # count alive fishes
if(m==1):continue
#probability of selecting 2 alive fishes
p=1/(nc2(m))
#pairing 2 fishes
for i in range(n):
for j in range(n):
if(i==j):continue
#check if i , j fishes are alive or not
if((mask >> i & 1) and (mask >> j & 1)):
#let fish i eat j
next_mask=mask ^ (1 << j)
dp[next_mask]+=(dp[mask]*p*a[i][j])
for i in range(n):
#fish i is alive
print(dp[1 << i],end=' ')
n=int(input())
a=[list(map(float,input().split())) for i in range(n)]
answer()
print()
``` | output | 1 | 64,244 | 14 | 128,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,245 | 14 | 128,490 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
prob = [tuple(map(float, input().split())) for _ in range(n)]
full_bit = (1 << n) - 1
dp = [0.0] * full_bit + [1.0]
for bit in range(full_bit, 0, -1):
popcount = len([1 for i in range(n) if (1 << i) & bit])
if popcount == 1 or dp[bit] == 0.0:
continue
div = 1 / ((popcount * (popcount - 1)) >> 1)
for i in range(n):
if ((1 << i) & bit) == 0:
continue
for j in range(i + 1, n):
if ((1 << j) & bit) == 0:
continue
dp[bit - (1 << j)] += dp[bit] * prob[i][j] * div
dp[bit - (1 << i)] += dp[bit] * prob[j][i] * div
print(*(dp[1 << i] for i in range(n)))
``` | output | 1 | 64,245 | 14 | 128,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,246 | 14 | 128,492 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
import sys
input=sys.stdin.readline
def count_bits(x):
cnt=0
for i in range(n):
if((1<<i)&x):
cnt+=1
return(cnt)
n=int(input())
a=[list(map(float,input().split())) for i in range(n)]
dp=[0 for i in range(1<<n)] #Probabilty a particular permutation of (alive) fish are acheived.
dp[-1]=1 #We start with all fish, so the probability they all together is 1(base case)
#We will calculate the probability of acheiving a particular permutation of k alive fish from all possible permutations of k+1 alive fish for all values of k.
for mask in range((1<<n)-1,-1,-1):
val=count_bits(mask)
total=val*(val-1)//2 #Used to calculate the probability of choosing two fish among the alive fish. We will take the case the first fish eats the second fish(the opposite case is dealt again in another loop, won't increase efficiency much), and add to the new permutation the probability of obtaiining it from the current permutation.
for i in range(n):
if(mask&(1<<i)==0): #We can't choose a dead/eaten fish
continue
for j in range(n): #Second fish of the pair for the above choosen fish among all other alive fish
if(mask&(1<<j)==0 or i==j):
continue
dp[mask^(1<<j)]+=dp[mask]*a[i][j]/total #considering ith fish eats jth fish
for i in range(n):
print(dp[1<<i])
``` | output | 1 | 64,246 | 14 | 128,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,247 | 14 | 128,494 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
def count(x):
ans = 0
while x:
x &= x-1
ans += 1
return ans
def main():
n = int(input())
a = [list(map(float,input().split())) for _ in range(n)]
y = 1<<n
dp = [0]*(y-1)+[1]
powe = [1<<i for i in range(n)]
for i in range(y-1,0,-1):
bit = count(i)
prob = bit*(bit-1)//2
for j in range(n):
if not i&powe[j]:
continue
for x in range(n):
if not i&powe[x]:
continue
dp[i-powe[x]] += dp[i]*a[j][x]*prob
dp[i-powe[j]] += dp[i]*a[x][j]*prob
z = sum(dp[1<<i] for i in range(n))
for i in range(n):
print(dp[1<<i]/z,end=' ')
print()
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self,file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
self.newlines = b.count(b"\n")+(not b)
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd,self.buffer.getvalue())
self.buffer.truncate(0),self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self,file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s:self.buffer.write(s.encode("ascii"))
self.read = lambda:self.buffer.read().decode("ascii")
self.readline = lambda:self.buffer.readline().decode("ascii")
sys.stdin,sys.stdout = IOWrapper(sys.stdin),IOWrapper(sys.stdout)
input = lambda:sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | output | 1 | 64,247 | 14 | 128,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,248 | 14 | 128,496 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# sys.setrecursionlimit(10**4)
def main():
n=int(input())
prob=[]
for _ in range(n):
prob.append(list(map(float,input().split())))
dp=[-1 for _ in range(1<<n)]
ans=[0 for _ in range(n)]
def move(mask,die):
total=bin(mask).count('1')
z=0
for i in range(n):
if mask & (1<<i):
z+=prob[i][die]
return z/((total*(total-1))>>1)
def solve(mask):
if mask==(1<<n)-1:
return 1
if dp[mask]!=-1:
return dp[mask]
ans=0
for i in range(n):
if not (mask & (1<<i)):
prev=solve(mask ^ (1<<i))
ans+=prev*move(mask ^ (1<<i),i)
dp[mask]=ans
return ans
for i in range(n):
ans[i]='%.6f'%solve(1<<i)
print(*ans)
#----------------------------------------------------------------------------------------
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# endregion
if __name__ == '__main__':
main()
``` | output | 1 | 64,248 | 14 | 128,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,249 | 14 | 128,498 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
n=int(input())
b=[]
for i in range(n):
b.append(list(map(float,input().split())))
ma=1<<n
dp=[0 for j in range(ma)]
dp[0]=1
for mask in range(1,ma):
l=n-bin(mask).count("1")+1
res=l*(l-1)//2
for i in range(n):
if mask&(1<<i):
for j in range(n):
if not mask&(1<<j):
dp[mask]+=((dp[mask^(1<<i)]*b[j][i])/res)
ans=[]
for i in range(n):
ans.append(dp[ma-1-(1<<i)])
print(*ans)
``` | output | 1 | 64,249 | 14 | 128,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 | instruction | 0 | 64,250 | 14 | 128,500 |
Tags: bitmasks, dp, probabilities
Correct Solution:
```
n = int(input())
probs = list()
for i in range(n): probs.append(list(map(float, input().split())))
dp = [list([0 for i in range(1<<n)]) for i in range(n)]
dp[0][(1<<n)-1] = 1
ak = [list() for i in range(n+1)]
for i in range(1<<n):
ak[bin(i).count("1")].append(i)
for k in range(1, n):
for ele in ak[n-k+1]:
for j in range(n):
if (ele&(1<<j)):
for w in range(n):
if (ele&(1<<w)) and j != w:
dp[k][ele-(1<<j)] += (dp[k-1][ele]*probs[w][j])/(((n-k+1)*(n-k))/2)
for i in range(n):
print(dp[n-1][(1<<i)], end = " ")
print()
``` | output | 1 | 64,250 | 14 | 128,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000
Submitted Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
a=[list(map(float,input().split())) for i in range(n)]
for i in range(n):
ans=1
tot=0
for j in range(n):
if(i==j or a[i][j]==0):
continue
ans*=a[i][j]
tot+=1/a[i][j]
ans*=tot/(n-1)
print(ans,end=" ")
``` | instruction | 0 | 64,251 | 14 | 128,502 |
No | output | 1 | 64,251 | 14 | 128,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000
Submitted Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
a=[list(map(float,input().split())) for i in range(n)]
for i in range(n):
ans=1
for j in range(n):
if(i==j):
continue
ans*=a[i][j]
print(ans,end=" ")
``` | instruction | 0 | 64,252 | 14 | 128,504 |
No | output | 1 | 64,252 | 14 | 128,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000
Submitted Solution:
```
n = int(input())
ans1 = []
for q in range(n):
row = [float(i) for i in input().split()]
if q != 0:
ans = row[0]
else:
ans = row[1]
if q == 0:
l = 2
else:
l = 1
for i in range(l, n):
if i != q:
ans *= row[i]
ans1.append(ans)
print(*ans1)
``` | instruction | 0 | 64,253 | 14 | 128,506 |
No | output | 1 | 64,253 | 14 | 128,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
Input
The first line contains integer n (1 β€ n β€ 18) β the amount of fish in the lake. Then there follow n lines with n real numbers each β matrix a. aij (0 β€ aij β€ 1) β the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
Examples
Input
2
0 0.5
0.5 0
Output
0.500000 0.500000
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000
Submitted Solution:
```
n = int(input())
ans = [1]*(n)
l = []
for i in range(n):
la = list(map(float,input().split()))
l.append(la)
k = 2**n - 1
# yo =
# print(bin(k)[2:])
for i in range(3,k+1):
z = '0'*(n - len(bin(i)[2:])) + bin(i)[2:]
ha = []
for j in range(n):
if z[j] == '1':
ha.append(j)
for x in ha:
for y in ha:
if x!=y:
ans[x]*=l[x][y]
print(*ans)
``` | instruction | 0 | 64,254 | 14 | 128,508 |
No | output | 1 | 64,254 | 14 | 128,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food.
Iahub asks Iahubina: can you build a rooted tree, such that
* each internal node (a node with at least one son) has at least two sons;
* node i has ci nodes in its subtree?
Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes.
Input
The first line of the input contains integer n (1 β€ n β€ 24). Next line contains n positive integers: the i-th number represents ci (1 β€ ci β€ n).
Output
Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes).
Examples
Input
4
1 1 1 4
Output
YES
Input
5
1 1 5 2 1
Output
NO | instruction | 0 | 64,355 | 14 | 128,710 |
Tags: bitmasks, constructive algorithms, dp, greedy, trees
Correct Solution:
```
def DFS(x):
for i in range(x):
if(Seen[i][x]):
continue
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Parent[x]=i
Children[i].append(x)
return True
for i in range(x):
if(Seen[i][x]):
continue
Y=[]
for j in range(len(Children[i])):
child=Children[i][j]
if(Seen[i][child]):
continue
Parent[child]=-1
Rem[i]+=C[child]
Seen[i][child]=True
Seen[child][i]=True
if(DFS(child)):
Seen[i][child]=False
Seen[child][i]=False
continue
Seen[i][child]=False
Seen[child][i]=False
Parent[child]=i
Rem[i]-=C[child]
Y.append(child)
Children[i]=list(Y)
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Children[i].append(x)
Parent[x]=i
return True
return False
n=int(input())
C=list(map(int,input().split()))
Rem=[-1]*n
Parent=[-1]*n
Children=[]
Seen=[]
for i in range(n):
Seen.append([False]*n)
C.sort(reverse=True)
if(C[0]!=n or C.count(2)>0):
print("NO")
else:
for i in range(n):
Rem[i]=C[i]-1
Children.append([])
Parent[0]=0
Ans="YES"
for i in range(1,n):
if(DFS(i)==False):
Ans="NO"
break
for i in range(n):
if(Rem[i]!=0 and C[i]!=1):
Ans="NO"
break
print(Ans)
# Made By Mostafa_Khaled
``` | output | 1 | 64,355 | 14 | 128,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food.
Iahub asks Iahubina: can you build a rooted tree, such that
* each internal node (a node with at least one son) has at least two sons;
* node i has ci nodes in its subtree?
Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes.
Input
The first line of the input contains integer n (1 β€ n β€ 24). Next line contains n positive integers: the i-th number represents ci (1 β€ ci β€ n).
Output
Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes).
Examples
Input
4
1 1 1 4
Output
YES
Input
5
1 1 5 2 1
Output
NO | instruction | 0 | 64,356 | 14 | 128,712 |
Tags: bitmasks, constructive algorithms, dp, greedy, trees
Correct Solution:
```
def DFS(x):
for i in range(x):
if(Seen[i][x]):
continue
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Parent[x]=i
Children[i].append(x)
return True
for i in range(x):
if(Seen[i][x]):
continue
Y=[]
for j in range(len(Children[i])):
child=Children[i][j]
Parent[child]=-1
Rem[i]+=C[child]
Seen[i][child]=True
Seen[child][i]=True
if(DFS(child)):
Seen[i][child]=False
Seen[child][i]=False
continue
Seen[i][child]=False
Seen[child][i]=False
Parent[child]=i
Rem[i]-=C[child]
Y.append(child)
Children[i]=list(Y)
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Children[i].append(x)
Parent[x]=i
return True
return False
n=int(input())
C=list(map(int,input().split()))
Rem=[-1]*n
Parent=[-1]*n
Children=[]
Seen=[]
for i in range(n):
Seen.append([False]*n)
C.sort(reverse=True)
if(C[0]!=n or C.count(2)>0):
print("NO")
else:
for i in range(n):
Rem[i]=C[i]-1
Children.append([])
Parent[0]=0
Ans="YES"
for i in range(1,n):
if(DFS(i)==False):
Ans="NO"
break
for i in range(n):
if(Rem[i]!=0 and C[i]!=1):
Ans="NO"
break
print(Ans)
``` | output | 1 | 64,356 | 14 | 128,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food.
Iahub asks Iahubina: can you build a rooted tree, such that
* each internal node (a node with at least one son) has at least two sons;
* node i has ci nodes in its subtree?
Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes.
Input
The first line of the input contains integer n (1 β€ n β€ 24). Next line contains n positive integers: the i-th number represents ci (1 β€ ci β€ n).
Output
Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes).
Examples
Input
4
1 1 1 4
Output
YES
Input
5
1 1 5 2 1
Output
NO | instruction | 0 | 64,357 | 14 | 128,714 |
Tags: bitmasks, constructive algorithms, dp, greedy, trees
Correct Solution:
```
def DFS(x):
for i in range(x):
if(Seen[i][x]):
continue
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Parent[x]=i
Children[i].append(x)
return True
for i in range(x):
if(Seen[i][x]):
continue
Y=[]
for j in range(len(Children[i])):
child=Children[i][j]
if(Seen[i][child]):
continue
Parent[child]=-1
Rem[i]+=C[child]
Seen[i][child]=True
Seen[child][i]=True
if(DFS(child)):
Seen[i][child]=False
Seen[child][i]=False
continue
Seen[i][child]=False
Seen[child][i]=False
Parent[child]=i
Rem[i]-=C[child]
Y.append(child)
Children[i]=list(Y)
if(Rem[i]>=C[x]):
if(Rem[i]==C[x] and len(Children[i])==0):
continue
Rem[i]-=C[x]
Children[i].append(x)
Parent[x]=i
return True
return False
n=int(input())
C=list(map(int,input().split()))
Rem=[-1]*n
Parent=[-1]*n
Children=[]
Seen=[]
for i in range(n):
Seen.append([False]*n)
C.sort(reverse=True)
if(C[0]!=n or C.count(2)>0):
print("NO")
else:
for i in range(n):
Rem[i]=C[i]-1
Children.append([])
Parent[0]=0
Ans="YES"
for i in range(1,n):
if(DFS(i)==False):
Ans="NO"
break
for i in range(n):
if(Rem[i]!=0 and C[i]!=1):
Ans="NO"
break
print(Ans)
``` | output | 1 | 64,357 | 14 | 128,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is the exact same as for the easy version. But this time, the marmots subtract the village's population P from their random number before responding to Heidi's request.
Also, there are now villages with as few as a single inhabitant, meaning that <image>.
Can you help Heidi find out whether a village follows a Poisson or a uniform distribution?
Input
Same as for the easy and medium versions. But remember that now 1 β€ P β€ 1000 and that the marmots may provide positive as well as negative integers.
Output
Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answers came from a uniform distribution. | instruction | 0 | 64,500 | 14 | 129,000 |
Tags: math, probabilities
Correct Solution:
```
for i in range(int(input())):
a=list(map(int,input().split()))
mx=max(list(map(abs,a)))
std=(sum(list(map(lambda x:x*x,a)))/len(a))**0.5
print('poisson'if mx/std>2 else'uniform')
``` | output | 1 | 64,500 | 14 | 129,001 |
Provide a correct Python 3 solution for this coding contest problem.
Gift Exchange Party
A gift exchange party will be held at a school in TKB City.
For every pair of students who are close friends, one gift must be given from one to the other at this party, but not the other way around. It is decided in advance the gift directions, that is, which student of each pair receives a gift. No other gift exchanges are made.
If each pair randomly decided the gift direction, some might receive countless gifts, while some might receive only few or even none.
You'd like to decide the gift directions for all the friend pairs that minimize the difference between the smallest and the largest numbers of gifts received by a student. Find the smallest and the largest numbers of gifts received when the difference between them is minimized. When there is more than one way to realize that, find the way that maximizes the smallest number of received gifts.
Input
The input consists of at most 10 datasets, each in the following format.
n m
u1 v1
...
um vm
n is the number of students, and m is the number of friendship relations (2 β€ n β€ 100, 1 β€ m β€ n (n-1)/2). Students are denoted by integers between 1 and n, inclusive. The following m lines describe the friendship relations: for each i, student ui and vi are close friends (ui < vi). The same friendship relations do not appear more than once.
The end of the input is indicated by a line containing two zeros.
Output
For each dataset, output a single line containing two integers l and h separated by a single space. Here, l and h are the smallest and the largest numbers, respectively, of gifts received by a student.
Sample Input
3 3
1 2
2 3
1 3
4 3
1 2
1 3
1 4
4 6
1 2
1 3
1 4
2 3
3 4
2 4
0 0
Output for the Sample Input
1 1
0 1
1 2
Example
Input
3 3
1 2
2 3
1 3
4 3
1 2
1 3
1 4
4 6
1 2
1 3
1 4
2 3
3 4
2 4
0 0
Output
1 1
0 1
1 2 | instruction | 0 | 64,783 | 14 | 129,566 |
"Correct Solution:
```
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
N, M = map(int, readline().split())
if N == M == 0:
return False
G = [[] for i in range(N)]
cs = [0]*N
for i in range(M):
u, v = map(int, readline().split())
forward = [v-1, 1, None]
backward = forward[2] = [u-1, 0, forward]
G[u-1].append(forward)
G[v-1].append(backward)
cs[v-1] += 1
prv = [0]*N
while 1:
que = deque()
used = [0]*N
mi = min(cs); ma = max(cs)
for i in range(N):
if mi < cs[i]:
continue
que.append(i)
used[i] = 1
prv[i] = None
j = -1
while que:
v = que.popleft()
if mi+2 <= cs[v]:
j = v
break
for w, d, rev in G[v]:
if d and not used[w]:
que.append(w)
prv[w] = rev
used[w] = 1
if j == -1:
break
v = j
while prv[v] is not None:
e = prv[v]
e[1] = 1; e[2][1] = 0
v = prv[v][0]
cs[v] += 1; cs[j] -= 1
while 1:
que = deque()
used = [0]*N
mi = min(cs); ma = max(cs)
for i in range(N):
if cs[i] < ma:
continue
que.append(i)
used[i] = 1
prv[i] = None
j = -1
while que:
v = que.popleft()
if cs[v] <= ma-2:
j = v
break
for w, d, rev in G[v]:
if not d and not used[w]:
que.append(w)
prv[w] = rev
used[w] = 1
if j == -1:
break
v = j
while prv[v] is not None:
e = prv[v]
e[1] = 0; e[2][1] = 1
v = prv[v][0]
cs[v] -= 1; cs[j] += 1
write("%d %d\n" % (min(cs), max(cs)))
return True
while solve():
...
``` | output | 1 | 64,783 | 14 | 129,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,959 | 14 | 129,918 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from sys import stdin, stdout
import math as np
dp = list()
item = list()
def top_down(left, right):
global dp
global item
level = int(np.ceil(np.log2(right - left + 1)))
if dp[level][left] != 0:
return dp[level][left]
neu = int( pow(2, level - 1) * np.floor(right / pow(2, level - 1)))
res_left = top_down(left, neu-1)
res_right = top_down(neu,right)
res = min(res_left, res_right)
if left % pow(2, level) == 0 and (right + 1) % pow(2, level) == 0:
dp[level][left] = res
return res
def testing(n):
global dp
global item
dp = [[0] * n for i in range(int(np.ceil(np.log2(n)) + 1))]
dp[0] = list(item)
def main():
global dp
global item
T = int(stdin.readline())
for zzz in range(T):
arr = list(map(int, stdin.readline().split()))
n,m,k = arr
item = list(map(int, stdin.readline().split()))
for i in range(m):
item[i] = max(item[i], item[n-m+i])
testing(m)
if m <= k:
k = m - 1
luck = m - k - 1
maxer = 0
for i in range(k+1):
left = i
right = n - k + i - 1
temp_min = top_down(left, left + luck)
"""
for j in range(luck+1):
left2 = j
right2 = luck - j
temp_max = max(item[left + left2], item[right - right2])
temp_min = min(temp_min, temp_max)
"""
if maxer == 0:
maxer = temp_min
else:
maxer = max(maxer, temp_min)
stdout.write(str(maxer)+"\n")
main()
``` | output | 1 | 64,959 | 14 | 129,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,960 | 14 | 129,920 |
Tags: brute force, data structures, implementation
Correct Solution:
```
V, IDX = [0]*3501, [0]*3501
def solve(A, n, m, k):
d = n-m
B = [0]*m
for i in range(m): B[i] = max(A[i], A[i+d])
w = max(1, m-k)
# print("* ", B, w)
res = 0
l = r = 0
for i in range(m):
while r > l and V[r-1] >= B[i]:
r -= 1
V[r], IDX[r] = B[i], i
r += 1
if IDX[l] == i-w: l += 1
if i >= w-1:
res = max(res, V[l])
# print("+ ", i, " ", V[l], " ", res)
return res
n = int(input())
for i in range(n):
n, m, k, = map(int, input().split())
A = list(map(int, input().strip().split()))
print(solve(A, n, m, k))
``` | output | 1 | 64,960 | 14 | 129,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,961 | 14 | 129,922 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import sys
t = input()
def solve(n, m, k, nums):
ans = 0
new_k = min(m-1, k)
for kk in range(new_k+1):
left = kk
right = new_k-kk
temp = nums[left:len(nums)-right]
c = min([max(temp[i], temp[-(m-new_k-i)]) for i in range(m-new_k)])
ans = max(ans, c)
return ans
for _ in range(int(t)):
temp = [int(s) for s in sys.stdin.readline().split(" ")]
n = temp[0]
m = temp[1]
k = temp[2]
nums = [int(s) for s in sys.stdin.readline().split(" ")]
print(solve(n, m, k, nums))
``` | output | 1 | 64,961 | 14 | 129,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,962 | 14 | 129,924 |
Tags: brute force, data structures, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
# sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
N, M, K = MAP()
A = LIST()
K = min(K, M-1)
L = M - K - 1
ans = 0
for i in range(K+1):
j = N - K + i
mn = INF
for k in range(L+1):
l = L - k
mx = max(A[i+k], A[j-l-1])
mn = min(mn, mx)
ans = max(ans, mn)
print(ans)
``` | output | 1 | 64,962 | 14 | 129,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,963 | 14 | 129,926 |
Tags: brute force, data structures, implementation
Correct Solution:
```
t = int(input())
for loop in range(t):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if k < m-1:
for w in range(k+1):
x = k-w
nmin = float("inf")
for y in range(m-k):
z = m-k-1-y
nmin = min(nmin , max (a[w+y] , a[n-1-x-z]))
#print (a[w+y:n-x-z])
ans = max(nmin,ans)
#print ("n" , nmin)
print (ans)
else:
ans = 0
for i in range(m):
ans = max(ans , a[i] , a[n-1-i])
print (ans)
``` | output | 1 | 64,963 | 14 | 129,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,964 | 14 | 129,928 |
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 2 16:03:04 2020
@author: dennis
"""
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for _ in range(int(input())):
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
controlled = min(m-1, k)
score = 0
for i in range(controlled+1):
uncontrolled = max(0, m-k-1)
low = 10**9+1
for j in range(uncontrolled+1):
best = max(a[i+j], a[len(a)-(controlled-i)-(uncontrolled-j)-1])
low = min(low, best)
score = max(score, low)
print(score)
``` | output | 1 | 64,964 | 14 | 129,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,965 | 14 | 129,930 |
Tags: brute force, data structures, implementation
Correct Solution:
```
t = int(input())
for y in range(t):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
if(k >= m-1):
ans = max(max(a[:m]),max(a[n-m:]))
else:
i = k
j = 0
ans = 0
while(i >= 0):
ind1 = i+(m-k)-1
ind2 = n-j-1
#print(i,j,ind1,ind2)
res2 = 1e9
for kk in range(m-k):
res = max(a[ind1-kk],a[ind2-kk])
res2 = min(res,res2)
ans = max(ans,res2)
i -= 1
j += 1
print(ans)
``` | output | 1 | 64,965 | 14 | 129,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers n, m and k (1 β€ m β€ n β€ 3500, 0 β€ k β€ n - 1) β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3500.
Output
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
Example
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4. | instruction | 0 | 64,966 | 14 | 129,932 |
Tags: brute force, data structures, implementation
Correct Solution:
```
t=int(input())
for i in range(t):
n,m,k=list(map(int,input().strip().split()))
a=list(map(int,input().strip().split()))
if k>=m:
k=m-1
z=m-1-k
sol=[]
for j in range(k+1):
big=max(a[j],a[j-k-z-1])
for o in range(z+1):
if max(a[j+o],a[o+j-k-z-1])<big:
big=max(a[j+o],a[o+j-k-z-1])
sol.append(big)
print(max(sol))
``` | output | 1 | 64,966 | 14 | 129,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,653 | 14 | 131,306 |
Tags: implementation
Correct Solution:
```
n,m = map(int,input().split())
l = list(map(int,input().split()))
h = []
for i in range(m):
c = list(l)
for j in range(i,n,m):
c[j] = 0
h.append(abs(sum(c)))
print(max(h))
``` | output | 1 | 65,653 | 14 | 131,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,654 | 14 | 131,308 |
Tags: implementation
Correct Solution:
```
n = input().split(" ")
k = int(n[1])
n = int(n[0])
mas = input().split(" ")
mas = [int(i) for i in mas]
mas_n = set(i for i in range(n))
max_def = 0
for b in range(k):
drop_n = set(b+i for i in range(0, n, k))
mas_n_ = mas_n - drop_n
mas_ = [0 if index in drop_n else i for index, i in enumerate(mas)]
if abs(sum(mas_)) > max_def: max_def = abs(sum(mas_))
print(max_def)
``` | output | 1 | 65,654 | 14 | 131,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,655 | 14 | 131,310 |
Tags: implementation
Correct Solution:
```
n, k = map(int , input().strip().split())
l = [int(x) for x in input().strip().split()]
e1 = 0
s1 = 0
for i in l:
if(i == 1):
e1+=1
else:
s1+=1
m = 0
for i in range(0, k):
e = e1
s = s1
for j in range(i, n, k):
if(l[j] == 1):
e=e-1
else:
s=s-1
m = max(max(e-s, s-e), m)
print(m)
``` | output | 1 | 65,655 | 14 | 131,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,656 | 14 | 131,312 |
Tags: implementation
Correct Solution:
```
s = input().split()
n = int(s[0])
k = int(s[1])
l = list(map(int, input().split()))
max_val = -1000
for b in range(n):
e = 0
s = 0
for j in range(n):
if j%k != b%k:
e += (1 if l[j] == 1 else 0)
s += (1 if l[j] == -1 else 0)
if abs(e-s) > max_val:
max_val= abs(e-s)
print(max_val)
``` | output | 1 | 65,656 | 14 | 131,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,657 | 14 | 131,314 |
Tags: implementation
Correct Solution:
```
def calMode(i):
ne, ns = 0, 0
temp = i
while temp<=(len(arr)-1):
if arr[temp]==1:
ne = ne + 1
else:
ns = ns + 1
temp = temp + k
return abs(e - ne - s + ns)
n, k = map(int, input().split(" "))
arr = list(map(int, input().split(" ")))
e, s = 0, 0
for i in arr:
if i==1:
e = e + 1
else:
s = s + 1
ans = []
for i in range(k):
ans.append(calMode(i))
print(max(ans))
``` | output | 1 | 65,657 | 14 | 131,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,658 | 14 | 131,316 |
Tags: implementation
Correct Solution:
```
n, k = map(int,input().split())
m = [int(i) for i in input().split()]
s = 0
e = 0
for i in range(n):
if m[i] == 1:
e+=1
else:
s-=1
r = s + e
res = 0
for i in range(k):
r = s + e
for j in range(n):
if i + k * j < n:
r -= m[i+k*j]
if (abs(r) > res):
res = abs(r)
print(res)
``` | output | 1 | 65,658 | 14 | 131,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,659 | 14 | 131,318 |
Tags: implementation
Correct Solution:
```
import math
import bisect
import itertools
import sys
I=lambda : sys.stdin.readline()
mod=10**9 +7
'''fact=[1]*100001
ifact=[1]*100001
for i in range(1,100001):
fact[i]=((fact[i-1])*i)%mod
ifact[i]=((ifact[i-1])*pow(i,mod-2,mod))%mod
def ncr(n,r):
return (((fact[n]*ifact[n-r])%mod)*ifact[r])%mod
def npr(n,r):
return (((fact[n]*ifact[n-r])%mod))
'''
def mindiff(a):
b=a[:]
b.sort()
m=10000000000
for i in range(len(b)-1):
if b[i+1]-b[i]<m:
m=b[i+1]-b[i]
return m
def lcm(a,b):
return a*b//math.gcd(a,b)
def merge(a,b):
i=0;j=0
c=0
ans=[]
while i<len(a) and j<len(b):
if a[i]<b[j]:
ans.append(a[i])
i+=1
else:
ans.append(b[j])
c+=len(a)-i
j+=1
ans+=a[i:]
ans+=b[j:]
return ans,c
def mergesort(a):
if len(a)==1:
return a,0
mid=len(a)//2
left,left_inversion=mergesort(a[:mid])
right,right_inversion=mergesort(a[mid:])
m,c=merge(left,right)
c+=(left_inversion+right_inversion)
return m,c
def is_prime(num):
if num == 1: return False
if num == 2: return True
if num == 3: return True
if num%2 == 0: return False
if num%3 == 0: return False
t = 5
a = 2
while t <= int(math.sqrt(num)):
if num%t == 0: return False
t += a
a = 6 - a
return True
def ceil(a,b):
if a%b==0:
return a//b
else:
return (a//b + 1)
def binsearch(arr,b,low,high):
if low==high:
return low
if arr[math.ceil((low+high)/2)]<b:
return binsearch(arr,b,low,math.ceil((low+high)/2) -1 )
else:
return binsearch(arr,b,math.ceil((low+high)/2),high)
def ncr1(n,r):
s=1
for i in range(min(n-r,r)):
s*=(n-i)
s%=mod
s*=pow(i+1,mod-2,mod)
s%=mod
return s
def calc(n,m,r):
s=0
for i in range(0,r+1,2):
s+=ncr1(n,i)*ncr1(m,i)
s%=mod
return s
#/////////////////////////////////////////////////////////////////////////////////////////////////
n,k=map(int,input().split())
a=list(map(int,input().split()))
m=-1
for i in range(1,n+1):
d={}
for j in range(-100,100):
d[i + j*k]=1
c=0;d1=0
for j in range(n):
if j+1 not in d:
c+=a[j]
m=max(abs(c),m)
print(m)
``` | output | 1 | 65,659 | 14 | 131,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them. | instruction | 0 | 65,660 | 14 | 131,320 |
Tags: implementation
Correct Solution:
```
a,b = map(int,input().split())
arr = list(map(int, input().split()))
maxi = 0
for n in range(b):
i = [arr[x] for x in range(a) if x not in range(n,a,b)]
x = abs(i.count(1) - i.count(-1))
if x>maxi:
maxi = x
print(maxi)
``` | output | 1 | 65,660 | 14 | 131,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.
He decided to accomplish this by closing every k-th (2 β€ k β€ n - 1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c = b + i β
k that satisfy the following condition: 1 β€ c β€ n and i is an integer (it may be positive, negative or zero).
For example, if k = 3, n = 14 and Roman chooses b = 8, then he will close tabs with numbers 2, 5, 8, 11 and 14.
After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e - s| so that it would be easy to decide what to do next.
Input
The first line contains two integers n and k (2 β€ k < n β€ 100) β the amount of tabs opened currently and the distance between the tabs closed.
The second line consists of n integers, each of them equal either to 1 or to -1. The i-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to -1, it's a social network tab.
Output
Output a single integer β the maximum absolute difference between the amounts of remaining tabs of different types |e - s|.
Examples
Input
4 2
1 1 -1 1
Output
2
Input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
Output
9
Note
In the first example we can choose b = 1 or b = 3. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, e = 2 and s = 0 and |e - s| = 2.
In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 3 17:23:55 2018
@author: GAME
"""
d = list(map(int, input().split()))
n = d[0]
k = d[1]
maxcount = -1
a = list(map(int, input().split()))
for i in range(k):
a1 = a.copy()
for j in range(i,n,k):
a1[j] = 0
temp = abs(a1.count(-1) - a1.count(1))
if temp > maxcount:
maxcount = temp
a1.clear()
print(maxcount)
``` | instruction | 0 | 65,661 | 14 | 131,322 |
Yes | output | 1 | 65,661 | 14 | 131,323 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.