message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,972 | 14 | 5,944 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a , b = map( int,input().split())
if a==b==0 or abs(a-b)>1:
print('NO')
else:
print('YES')
``` | output | 1 | 2,972 | 14 | 5,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,973 | 14 | 5,946 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
n,m=map(int, input().split())
if n==0 and m==0:
print('NO')
elif abs(n-m)<=1:
print('YES')
else:
print('NO')
``` | output | 1 | 2,973 | 14 | 5,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,974 | 14 | 5,948 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b=map(int, input().split())
if a-b==1 or b-a==1 or (a==b and a!=0):
print('YES')
else:
print('NO')
``` | output | 1 | 2,974 | 14 | 5,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,975 | 14 | 5,950 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b=map(int,input().split())
if a==0 and b==0:
print('NO')
else:
print('YES' if abs(a-b)<=1 else 'NO')
``` | output | 1 | 2,975 | 14 | 5,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,976 | 14 | 5,952 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b=map(int, input().split())
if abs(a-b)<=1 and not(a==b==0): print("YES")
else: print("NO")
``` | output | 1 | 2,976 | 14 | 5,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,977 | 14 | 5,954 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b = map(int,input().split())
if(a==b and a==0):
print("NO")
else:
print("YES" if (abs(a-b)<=1) else "NO")
``` | output | 1 | 2,977 | 14 | 5,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,978 | 14 | 5,956 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b=map(int,input().split(' '))
l=0
if a==b or a==b-1 or b==a-1:
if a+b!=0:
print ("YES")
l=l+1
if l==0:
print ("NO")
``` | output | 1 | 2,978 | 14 | 5,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. | instruction | 0 | 2,979 | 14 | 5,958 |
Tags: brute force, constructive algorithms, implementation, math
Correct Solution:
```
a,b=map(int,input().split())
if a+b>0 and abs(a-b)<=1:
print("YES")
else:
print("NO")
``` | output | 1 | 2,979 | 14 | 5,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On her way to programming school tiger Dasha faced her first test — a huge staircase!
<image>
The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.
You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.
Input
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
Output
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
Examples
Input
2 3
Output
YES
Input
3 1
Output
NO
Note
In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5.
Submitted Solution:
```
p, n = input().split()
p = int(p)
n = int(n)
if p==n or n-p==1:
print("YES")
else:
print("NO")
``` | instruction | 0 | 2,984 | 14 | 5,968 |
No | output | 1 | 2,984 | 14 | 5,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to n different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate s1, Vasya is at the point with the coordinate s2, and the clients are at the points x1, x2, ..., xn in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the i-th client is delivered, the one who delivers the package to the (i + 1)-st client is sent to the path (it can be the same person who went to the point xi, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules.
Input
The first line contains three integers n, s1, s2 (1 ≤ n ≤ 100 000, 0 ≤ s1, s2 ≤ 109) — number of points of delivery and starting positions of Petya and Vasya.
The second line contains n integers x1, x2, ..., xn — customers coordinates (0 ≤ xi ≤ 109), in the order to make a delivery.
It is guaranteed, that among the numbers s1, s2, x1, ..., xn there are no two equal.
Output
Output the only integer, minimum possible maximal distance between couriers during delivery.
Examples
Input
2 0 10
5 6
Output
10
Input
3 2 1
3 4 5
Output
1
Input
1 4 5
2
Output
2
Note
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5 - 2 = 3. If Vasya will deliver the package, the maximum distance is 4 - 2 = 2. The latter method is optimal.
Submitted Solution:
```
from math import fabs as mod
n,a,b=map(int,input().split(' '))
m=list(map(int,input().split(' ')))
x1=mod(b-a)
x2a=mod(m[0]-a)
x2b=mod(m[0]-b)
x2=min(mod(m[0]-a),mod(m[0]-b))
d=max(x1,x2)
for i in range(n-1):
if mod(m[i+1]-m[i])>d:
d=mod(m[i+1]-m[i])
for i in range(n):
if mod(m[i]-a)>x2a:
x2a=mod(m[i]-a)
if mod(m[i]-b)>x2b:
x2b=mod(m[i]-b)
print(int(max(x1,min(d,x2a,x2b))))
``` | instruction | 0 | 3,047 | 14 | 6,094 |
No | output | 1 | 3,047 | 14 | 6,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to n different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate s1, Vasya is at the point with the coordinate s2, and the clients are at the points x1, x2, ..., xn in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the i-th client is delivered, the one who delivers the package to the (i + 1)-st client is sent to the path (it can be the same person who went to the point xi, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules.
Input
The first line contains three integers n, s1, s2 (1 ≤ n ≤ 100 000, 0 ≤ s1, s2 ≤ 109) — number of points of delivery and starting positions of Petya and Vasya.
The second line contains n integers x1, x2, ..., xn — customers coordinates (0 ≤ xi ≤ 109), in the order to make a delivery.
It is guaranteed, that among the numbers s1, s2, x1, ..., xn there are no two equal.
Output
Output the only integer, minimum possible maximal distance between couriers during delivery.
Examples
Input
2 0 10
5 6
Output
10
Input
3 2 1
3 4 5
Output
1
Input
1 4 5
2
Output
2
Note
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5 - 2 = 3. If Vasya will deliver the package, the maximum distance is 4 - 2 = 2. The latter method is optimal.
Submitted Solution:
```
from math import fabs as mod
n,a,b=map(int,input().split(' '))
m=list(map(int,input().split(' ')))
x1=mod(b-a)
x2a=mod(m[0]-a)
x2b=mod(m[0]-b)
x2=min(mod(m[0]-a),mod(m[0]-b))
d=max(x1,x2)
for i in range(n-1):
if mod(m[i+1]-m[i])>d:
d=mod(m[i+1]-m[i])
for i in range(n):
if mod(m[i]-a)>x2a:
x2a=mod(m[i]-a)
if mod(m[i]-b)>x2b:
x2b=mod(m[i]-b)
print(int(min(d,x2a,x2b)))
``` | instruction | 0 | 3,048 | 14 | 6,096 |
No | output | 1 | 3,048 | 14 | 6,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to n different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate s1, Vasya is at the point with the coordinate s2, and the clients are at the points x1, x2, ..., xn in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the i-th client is delivered, the one who delivers the package to the (i + 1)-st client is sent to the path (it can be the same person who went to the point xi, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules.
Input
The first line contains three integers n, s1, s2 (1 ≤ n ≤ 100 000, 0 ≤ s1, s2 ≤ 109) — number of points of delivery and starting positions of Petya and Vasya.
The second line contains n integers x1, x2, ..., xn — customers coordinates (0 ≤ xi ≤ 109), in the order to make a delivery.
It is guaranteed, that among the numbers s1, s2, x1, ..., xn there are no two equal.
Output
Output the only integer, minimum possible maximal distance between couriers during delivery.
Examples
Input
2 0 10
5 6
Output
10
Input
3 2 1
3 4 5
Output
1
Input
1 4 5
2
Output
2
Note
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5 - 2 = 3. If Vasya will deliver the package, the maximum distance is 4 - 2 = 2. The latter method is optimal.
Submitted Solution:
```
from math import fabs as mod
n,a,b=map(int,input().split(' '))
m=list(map(int,input().split(' ')))
x1=mod(b-a)
x2=min(mod(m[0]-a),mod(m[0]-b))
d=0
for i in range(n-1):
if mod(m[i+1]-m[i])>d:
d=mod(m[i+1]-m[i])
print(int(max(x1,x2,d)))
``` | instruction | 0 | 3,049 | 14 | 6,098 |
No | output | 1 | 3,049 | 14 | 6,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to n different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate s1, Vasya is at the point with the coordinate s2, and the clients are at the points x1, x2, ..., xn in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the i-th client is delivered, the one who delivers the package to the (i + 1)-st client is sent to the path (it can be the same person who went to the point xi, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules.
Input
The first line contains three integers n, s1, s2 (1 ≤ n ≤ 100 000, 0 ≤ s1, s2 ≤ 109) — number of points of delivery and starting positions of Petya and Vasya.
The second line contains n integers x1, x2, ..., xn — customers coordinates (0 ≤ xi ≤ 109), in the order to make a delivery.
It is guaranteed, that among the numbers s1, s2, x1, ..., xn there are no two equal.
Output
Output the only integer, minimum possible maximal distance between couriers during delivery.
Examples
Input
2 0 10
5 6
Output
10
Input
3 2 1
3 4 5
Output
1
Input
1 4 5
2
Output
2
Note
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5 - 2 = 3. If Vasya will deliver the package, the maximum distance is 4 - 2 = 2. The latter method is optimal.
Submitted Solution:
```
n, p1, p2 = map(int, input().split())
points = list(map(int, input().split()))
p1, p2 = min(p1, p2), max(p1, p2)
dist = p2 - p1
for i in range(n):
point = points[i]
point_ = points[i + (i < n - 1)]
d1 = abs(point - p1)
d2 = abs(point - p2)
if d1 == d2:
d1 = abs(point_ - p1)
d2 = abs(point_ - p2)
if d1 > d2:
p1 = point
else:
p2 = point
p1, p2 = min(p1, p2), max(p1, p2)
dist = max(dist, p2 - p1)
print(dist)
``` | instruction | 0 | 3,050 | 14 | 6,100 |
No | output | 1 | 3,050 | 14 | 6,101 |
Provide a correct Python 3 solution for this coding contest problem.
A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0.
When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests.
To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party.
Input
The first line of the input contains two integers, n and m (1 \leq n \leq 100, 0 \leq m \leq 100). The rabbits are numbered from 1 to n.
Each of the following m lines has three integers, u, v and f. u and v (1 \leq u, v \leq n, u \neq v, 1 \leq f \leq 1,000,000) stands for the rabbits' number, and f stands for their friendliness.
You may assume that the friendliness of a pair of rabbits will be given at most once.
Output
Output the maximal possible satisfaction score of the party in a line.
Examples
Input
3 3
1 2 3
2 3 1
3 1 2
Output
6
Input
2 1
1 2 5
Output
10
Input
1 0
Output
0
Input
4 5
1 2 4
1 3 3
2 3 7
2 4 5
3 4 6
Output
16 | instruction | 0 | 3,284 | 14 | 6,568 |
"Correct Solution:
```
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
def solve():
n,m = LI()
e = []
f = [[0]*n for i in range(n)]
v = [[] for i in range(n)]
for i in range(m):
a,b,c = LI()
a -= 1
b -= 1
e.append([a,b,c])
f[a][b] = c
f[b][a] = c
v[a].append(b)
v[b].append(a)
ans = 0
S = []
for p in range(n):
K = [{p}]
for q in v[p]:
K += [s|{q} for s in K]
S += K
for s in S:
for x in s:
for y in s:
if x == y:
continue
if f[x][y] > 0:
continue
else:
break
else:
continue
break
else:
m = [float("inf")]*n
for a in s:
for b in s:
if a == b:
continue
if f[a][b] < m[a]:
m[a] = f[a][b]
if f[a][b] < m[b]:
m[b] = f[a][b]
k = 0
for i in m:
if i != float("inf"):
k += i
if ans < k:
ans = k
print(ans)
return
#Solve
if __name__ == "__main__":
solve()
``` | output | 1 | 3,284 | 14 | 6,569 |
Provide a correct Python 3 solution for this coding contest problem.
A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0.
When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests.
To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party.
Input
The first line of the input contains two integers, n and m (1 \leq n \leq 100, 0 \leq m \leq 100). The rabbits are numbered from 1 to n.
Each of the following m lines has three integers, u, v and f. u and v (1 \leq u, v \leq n, u \neq v, 1 \leq f \leq 1,000,000) stands for the rabbits' number, and f stands for their friendliness.
You may assume that the friendliness of a pair of rabbits will be given at most once.
Output
Output the maximal possible satisfaction score of the party in a line.
Examples
Input
3 3
1 2 3
2 3 1
3 1 2
Output
6
Input
2 1
1 2 5
Output
10
Input
1 0
Output
0
Input
4 5
1 2 4
1 3 3
2 3 7
2 4 5
3 4 6
Output
16 | instruction | 0 | 3,285 | 14 | 6,570 |
"Correct Solution:
```
from itertools import product
def main():
INF = 10**9
N, M = map(int, input().split())
E = [[INF]*N for i in range(N)]
G = [set() for i in range(N)]
D = [0]*N
for i in range(M):
u, v, f = map(int, input().split()); u -= 1; v -= 1
E[u][v] = E[v][u] = f
G[u].add(v)
G[v].add(u)
D[u] += 1; D[v] += 1
def calc(vs):
if len(vs) == 1:
return 0
res = 0
for ps in product([0, 1], repeat=len(vs)):
R = [v for p, v in zip(ps, vs) if p]
if len(R) == 1:
continue
rs = 0
for v in R:
r = INF
for w in R:
if v == w:
continue
r = min(r, E[v][w])
rs += r
res = max(res, rs)
return res
def dfs(V, P, X):
if not P and not X:
return calc(V)
u = next(iter(X or P))
r = 0
for v in P - G[u]:
r = max(r, dfs(V | {v}, P & G[v], X & G[v]))
P.remove(v)
X.add(v)
return r
*I, = range(N)
I.sort(key = D.__getitem__, reverse=1)
ans = 0
P = set(range(N))
X = set()
for v in I:
ans = max(ans, dfs({v}, P & G[v], X & G[v]))
P.remove(v)
X.add(v)
print(ans)
main()
``` | output | 1 | 3,285 | 14 | 6,571 |
Provide a correct Python 3 solution for this coding contest problem.
A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0.
When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests.
To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party.
Input
The first line of the input contains two integers, n and m (1 \leq n \leq 100, 0 \leq m \leq 100). The rabbits are numbered from 1 to n.
Each of the following m lines has three integers, u, v and f. u and v (1 \leq u, v \leq n, u \neq v, 1 \leq f \leq 1,000,000) stands for the rabbits' number, and f stands for their friendliness.
You may assume that the friendliness of a pair of rabbits will be given at most once.
Output
Output the maximal possible satisfaction score of the party in a line.
Examples
Input
3 3
1 2 3
2 3 1
3 1 2
Output
6
Input
2 1
1 2 5
Output
10
Input
1 0
Output
0
Input
4 5
1 2 4
1 3 3
2 3 7
2 4 5
3 4 6
Output
16 | instruction | 0 | 3,286 | 14 | 6,572 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
def f(n,m):
e = [[0]*n for _ in range(n)]
cs = []
for _ in range(m):
a,b,c = LI_()
c += 1
e[a][b] = c
e[b][a] = c
cs.append([a,b])
r = 0
while cs:
ns = []
for a in cs:
t = 0
for i in a:
m = inf
for j in a:
if i == j:
continue
if m > e[i][j]:
m = e[i][j]
t += m
if r < t:
r = t
for i in range(a[-1]+1,n):
f = True
for j in a:
if e[i][j] < 1:
f = False
break
if f:
ns.append(a + [i])
cs = ns
return r
while 1:
n,m = LI()
if n == 0:
break
rr.append(f(n,m))
# print('rr', rr[-1])
break
return '\n'.join(map(str,rr))
print(main())
``` | output | 1 | 3,286 | 14 | 6,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger. | instruction | 0 | 3,370 | 14 | 6,740 |
Tags: binary search, data structures, graphs, greedy, implementation, math, sortings
Correct Solution:
```
def main():
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
mod=sum(a)%2
counts=[0]*(n+1)
for guy in a:
counts[guy]+=1
cumcounts=[0]*(n+1)
cumcounts[0]=counts[0]
for i in range(n):
cumcounts[i+1]=cumcounts[i]+counts[i+1]
partialsums=[0]*(n+1)
curr=0
for i in range(n):
curr+=(i+1)*counts[i+1]
partialsums[i+1]=curr
partialsums.append(0)
cumcounts.append(0)
sumi=0
diffs=[0]*n
altdiffs=[0]*n
for i in range(n):
sumi+=a[i]
rhs=i*(i+1)
if a[i]>i:
rhs+=partialsums[i]+(i+1)*(n-i-1-cumcounts[i])
else:
rhs+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
diffs[i]=sumi-rhs
rhs2=(i+1)*(i+2)
if a[i]>i+1:
rhs2+=partialsums[i+1]+(i+2)*(n-i-1-cumcounts[i+1])
else:
rhs2+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
altdiffs[i]=sumi-rhs2
mini=max(diffs)
maxi=-max(altdiffs)
mini=max(mini,0)
maxi=min(maxi,n)
out=""
if mini%2!=mod:
mini+=1
if maxi%2==mod:
maxi+=1
for guy in range(mini,maxi,2):
out+=str(guy)+" "
if mini>maxi:
print(-1)
else:
print(out)
main()
``` | output | 1 | 3,370 | 14 | 6,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger. | instruction | 0 | 3,371 | 14 | 6,742 |
Tags: binary search, data structures, graphs, greedy, implementation, math, sortings
Correct Solution:
```
def main():
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
mod=sum(a)%2
counts=[0]*(n+1)
for guy in a:
counts[guy]+=1
cumcounts=[counts[0]]
for i in range(n):
cumcounts.append(cumcounts[-1]+counts[i+1])
partialsums=[0]
curr=0
for i in range(n):
curr+=(i+1)*counts[i+1]
partialsums.append(curr)
partialsums.append(0)
cumcounts.append(0)
sumi=0
diffs=[]
altdiffs=[]
for i in range(n):
sumi+=a[i]
rhs=i*(i+1)
if a[i]>i:
rhs+=partialsums[i]+(i+1)*(n-i-1-cumcounts[i])
else:
rhs+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
diffs.append(sumi-rhs)
rhs2=(i+1)*(i+2)
if a[i]>i+1:
rhs2+=partialsums[i+1]+(i+2)*(n-i-1-cumcounts[i+1])
else:
rhs2+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
altdiffs.append(sumi-rhs2)
mini=max(diffs)
maxi=-max(altdiffs)
mini=max(mini,0)
maxi=min(maxi,n)
out=""
if mini%2!=mod:
mini+=1
if maxi%2==mod:
maxi+=1
for guy in range(mini,maxi,2):
out+=str(guy)+" "
if mini>maxi:
print(-1)
else:
print(out)
main()
``` | output | 1 | 3,371 | 14 | 6,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.
Submitted Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input())
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]
n = val()
l = sorted(li(),reverse = 1)
while len(l) and l[-1] == 0:l.pop()
if len(l) == 0:
print(0)
exit()
lcopy = l[:]
visited = {}
maxtot = tot = 0
if max(l)>len(l):
print(-1)
exit()
l = l[::-1]
help = [0 for i in range(n)]
maxtot = mintot = 0
till = 0
for i in range(n):
till += help[i]
l[i] += till
if n - l[i] == i:
l[i] -= 1
mintot += 1
if l[i]>0:help[n - l[i]] -= 1
# print(mintot)
l = lcopy[::-1]
help = [0 for i in range(n)]
till = 0
for i in range(n):
till += help[i]
l[i] += till
if l[i]>0:
maxtot += 1
l[i] -= 1
else:break
if l[i]>0:help[n - l[i]] -= 1
for i in range(mintot,maxtot + 1,2):print(i)
``` | instruction | 0 | 3,372 | 14 | 6,744 |
No | output | 1 | 3,372 | 14 | 6,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.
Submitted Solution:
```
import copy
def seqWorks(dat, extra):
# sort dat + extra.
x = copy.deepcopy(dat)
x.append(extra)
x.sort(key=lambda q:-q)
N = len(x)
lhsSum = 0
rhsSum = 0
k = 0
lastPositionAtLeastK = 0
while lastPositionAtLeastK < N-1 and x[lastPositionAtLeastK + 1] >= k:
lastPositionAtLeastK += 1
for q in range(N):
k = q+1
lhsSum += x[q]
rhsSum -= min(x[q], k-1)
# add the number of items in [q+1, N) that're at least k.
while lastPositionAtLeastK >= 0 and x[ lastPositionAtLeastK ] < k:
lastPositionAtLeastK -= 1
if lastPositionAtLeastK >= q+1:
rhsSum += lastPositionAtLeastK - q
rhsFinal = k * (k-1) + rhsSum
if lhsSum <= rhsFinal:
# this inequality is satisfied.
continue
# if the sequence fails, return some code telling whether 'extra' was too big or too little.
if x[q] >= extra:
# extra is small.
return "too small"
else:
return "too big"
return 'OK'
def largestNotTooBig(dat):
total = sum(dat)
parity = total % 2
lo2 = 0 # might work?
hi2 = len(dat) // 2 + 1 # too high
if seqWorks(dat, lo2*2 + parity) == 'too big':
return parity - 2
while hi2 - lo2 > 1:
curr2 = (lo2 + hi2) // 2
curr = curr2 * 2 + parity
result = seqWorks(dat, curr)
if result == 'too big':
hi2 = curr2
else:
lo2 = curr2
return lo2*2 + parity
def smallestNotTooSmall(dat):
total = sum(dat)
parity = total % 2
lo2 = 0 # too small?
hi2 = len(dat) // 2 + 1 # not too small
if seqWorks(dat, lo2*2 + parity) != 'too small':
return lo2*2 + parity
while hi2 - lo2 > 1:
curr2 = (lo2 + hi2) // 2
curr = curr2 * 2 + parity
result = seqWorks(dat, curr)
# print("lo2 = {}, hi2 = {}, result = {}".format(lo2, hi2, result))
if result == 'too small':
lo2 = curr2
else:
hi2 = curr2
return hi2*2 + parity
def solve(dat):
lower = smallestNotTooSmall(dat)
upper = largestNotTooBig(dat)
if lower <= upper:
print(
' '.join(
[str(x) for x in range(lower, upper+1, 2)]
)
)
else:
print(-1)
n = int(input())
dat = list(map(int, input().rstrip().split()))
solve(dat)
``` | instruction | 0 | 3,373 | 14 | 6,746 |
No | output | 1 | 3,373 | 14 | 6,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.
Submitted Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input())
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]
n = val()
l = sorted(li(),reverse = 1)
while len(l) and l[-1] == 0:l.pop()
if len(l) == 0:
print(0)
exit()
lcopy = l[:]
visited = {}
maxtot = tot = 0
if max(l)>len(l):
print(-1)
exit()
l = l[::-1]
help = [0 for i in range(n)]
maxtot = mintot = 0
till = 0
for i in range(n):
till += help[i]
l[i] += till
if n - l[i] == i:
l[i] -= 1
mintot += 1
if l[i]>0:help[n - l[i]] -= 1
# print(mintot)
l = lcopy
help = [0 for i in range(n)]
till = 0
for i in range(n):
till += help[i]
l[i] += till
if l[i]>0:
maxtot += 1
l[i] -= 1
else:break
if l[i]>0:help[n - l[i]] -= 1
for i in range(mintot,maxtot + 1,2):print(i)
``` | instruction | 0 | 3,374 | 14 | 6,748 |
No | output | 1 | 3,374 | 14 | 6,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends.
This morning, somebody anonymously sent Bob the following link: [graph realization problem](https://en.wikipedia.org/wiki/Graph_realization_problem) and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.
Input
The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5), the number of people on the network excluding Bob.
The second line contains n numbers a_1,a_2, ..., a_n (0 ≤ a_i ≤ n), with a_i being the number of people that person i is a friend of.
Output
Print all possible values of a_{n+1} — the amount of people that Bob can be friend of, in increasing order.
If no solution exists, output -1.
Examples
Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21
Note
In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 3 friends.
In the second test case, there are three possible solutions (apart from symmetries):
* a is friend of b, c is friend of d, and Bob has no friends, or
* a is a friend of b and both c and d are friends with Bob, or
* Bob is friends of everyone.
The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
mod=sum(a)%2
counts=[0]*(n+1)
for guy in a:
counts[guy]+=1
cumcounts=[counts[0]]
for i in range(n):
cumcounts.append(cumcounts[-1]+counts[i+1])
partialsums=[0]
curr=0
for i in range(n):
curr+=(i+1)*counts[i+1]
partialsums.append(curr)
partialsums.append(0)
cumcounts.append(0)
sumi=0
diffs=[]
altdiffs=[]
for i in range(n):
sumi+=a[i]
rhs=i*(i+1)
if a[i]>i:
rhs+=partialsums[i]+(i+1)*(n-i-1-cumcounts[i])
else:
rhs+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
diffs.append(sumi-rhs)
rhs2=(i+1)*(i+2)
if a[i]>i+1:
rhs2+=partialsums[i+1]+(i+2)*(n-i-1-cumcounts[i+1])
else:
rhs2+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1])
altdiffs.append(sumi-rhs2)
mini=max(diffs)
maxi=-max(altdiffs)
out=""
if mini%2!=mod:
mini+=1
if maxi%2==mod:
maxi+=1
for guy in range(mini,maxi,2):
out+=str(guy)+" "
if mini>maxi:
print(-1)
else:
print(out)
``` | instruction | 0 | 3,375 | 14 | 6,750 |
No | output | 1 | 3,375 | 14 | 6,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to n from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat i (1 ≤ i ≤ n) will decide to go for boiled water at minute t_i.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly p minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the i-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to i - 1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than i are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of people and the amount of time one person uses the tank.
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ 10^9) — the moments when the corresponding passenger will go for the boiled water.
Output
Print n integers, where i-th of them is the time moment the passenger on i-th seat will receive his boiled water.
Example
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water. | instruction | 0 | 3,440 | 14 | 6,880 |
Tags: data structures, greedy, implementation
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from heapq import heappop, heappush, heapify
from collections import deque
class SWAG_Stack():
def __init__(self, F):
self.stack1 = deque()
self.stack2 = deque()
self.F = F
self.len = 0
def push(self, x):
if self.stack2:
self.stack2.append((x, self.F(self.stack2[-1][1], x)))
else:
self.stack2.append((x, x))
self.len += 1
def pop(self):
if not self.stack1:
while self.stack2:
x, _ = self.stack2.pop()
if self.stack1:
self.stack1.appendleft((x, self.F(x, self.stack1[0][1])))
else:
self.stack1.appendleft((x, x))
x, _ = self.stack1.popleft()
self.len -= 1
return x
def sum_all(self):
if self.stack1 and self.stack2:
return self.F(self.stack1[0][1], self.stack2[-1][1])
elif self.stack1:
return self.stack1[0][1]
elif self.stack2:
return self.stack2[-1][1]
else:
return float("inf")
n,p = map(int, input().split())
t = list((j, i) for i,j in enumerate(map(int, input().split())))
heapify(t)
stack = SWAG_Stack(min)
heap = []
cur = 0
ans = [-1]*n
hoge = 0
# 今追加できるやつで最も小さいものを追加
# ここでなにもなかったら?
# 時間を変更する
# 次の時間までに追加できるものを追加
# 清算
while hoge != n:
if heap and stack.sum_all() > heap[0]:
j = heappop(heap)
stack.push(j)
if stack.len==0 and t:
cur = max(cur, t[0][0])
while t and t[0][0] <= cur+p:
ti, i = heappop(t)
if ti == cur+p:
# 後回し
heappush(heap, i)
else:
# いま追加できるか確認
if stack.sum_all() > i:
stack.push(i)
else:
# 後回し
heappush(heap, i)
if stack.len:
j = stack.pop()
cur += p
ans[j] = cur
hoge += 1
print(*ans)
``` | output | 1 | 3,440 | 14 | 6,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to n from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat i (1 ≤ i ≤ n) will decide to go for boiled water at minute t_i.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly p minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the i-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to i - 1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than i are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of people and the amount of time one person uses the tank.
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ 10^9) — the moments when the corresponding passenger will go for the boiled water.
Output
Print n integers, where i-th of them is the time moment the passenger on i-th seat will receive his boiled water.
Example
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water.
Submitted Solution:
```
n, p = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
data = [[t[i], i] for i in range(n)]
data.sort(key = lambda x: x[0])
back = 0
ready = []
answer = [0 for i in range(n)]
j = 0
while j < len(data):#пока есть кто-то
while j < len(data) and data[j][0] <= back:
ready.append(data[j][1])
j += 1
if ready:
ind = min(ready)
ready.remove(ind)
back += p
answer[ind] = back
else:
if j < len(data):
ind = data[j][1]
answer[ind] = data[j][0] + p
back = answer[ind]
j += 1
if ready:
ready.sort()
for i in range(len(ready)):
answer[ready[i]] = back + (i + 1)*p
for i in range(n):
print(answer[i], end = ' ')
``` | instruction | 0 | 3,441 | 14 | 6,882 |
No | output | 1 | 3,441 | 14 | 6,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to n from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat i (1 ≤ i ≤ n) will decide to go for boiled water at minute t_i.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly p minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the i-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to i - 1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than i are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of people and the amount of time one person uses the tank.
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ 10^9) — the moments when the corresponding passenger will go for the boiled water.
Output
Print n integers, where i-th of them is the time moment the passenger on i-th seat will receive his boiled water.
Example
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water.
Submitted Solution:
```
mod=pow(10,9)+7
dp=[1,1]
for i in range(100005):
dp.append((dp[-1]+dp[-2])%mod)
n,m=map(int,input().split())
print(2*(dp[n]+dp[m]-1))
``` | instruction | 0 | 3,442 | 14 | 6,884 |
No | output | 1 | 3,442 | 14 | 6,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to n from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat i (1 ≤ i ≤ n) will decide to go for boiled water at minute t_i.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly p minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the i-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to i - 1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than i are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of people and the amount of time one person uses the tank.
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ 10^9) — the moments when the corresponding passenger will go for the boiled water.
Output
Print n integers, where i-th of them is the time moment the passenger on i-th seat will receive his boiled water.
Example
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water.
Submitted Solution:
```
n, p = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
v = [False] * n
p0 = 0
t0 = min(t)
t0i = t.index(min(t))
v[t0i] = True
times = [0] * n
while not all(v):
p0 += p
times[t0i] = p0
v[t0i] = True
for i in range(n):
if t[i] <= p0 and not v[i]:
t0i = i
break
s = ""
for time in times:
s += str(time) + " "
print(s.strip())
``` | instruction | 0 | 3,443 | 14 | 6,886 |
No | output | 1 | 3,443 | 14 | 6,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to n from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat i (1 ≤ i ≤ n) will decide to go for boiled water at minute t_i.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly p minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the i-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to i - 1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than i are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of people and the amount of time one person uses the tank.
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ 10^9) — the moments when the corresponding passenger will go for the boiled water.
Output
Print n integers, where i-th of them is the time moment the passenger on i-th seat will receive his boiled water.
Example
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water.
Submitted Solution:
```
import heapq
'''f = open('input.txt', 'r')
n, p = map(int, f.readline().split())
l = list(map(int, f.readline().split()))
f.close()'''
n, p = map(int,input().split())
l = list(map(int,input().split()))
out_time = 0
sol = []
for i in range(n):
sol.append(0)
for i in range(n):
l[i] = (l[i],i)
l.sort()
order = []
heapq.heapify(order)
while len(l):
if out_time < l[0][0] and not len(order):
out_time = l[0][0] + p
sol[l[0][1]] = out_time
l.pop(0)
elif out_time < l[0][0] and len(order):
idx = heapq.heappop(order)
out_time += p
sol[idx] = out_time
elif out_time >= l[0][0]:
while len(l) and l[0][0] <= out_time:
heapq.heappush(order, l[0][1])
l.pop(0)
idx = heapq.heappop(order)
out_time += p
sol[idx] = out_time
while(len(order)):
idx = heapq.heappop(order)
out_time += p
sol[idx] = out_time
print(*sol)
``` | instruction | 0 | 3,444 | 14 | 6,888 |
No | output | 1 | 3,444 | 14 | 6,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues A and B and the direct link between clues B and A is the same thing. No more than one direct link can exist between two clues.
Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues A and B are considered linked either if there is a direct link between them or if there is a direct link between A and some other clue C which is linked to B.
Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals T.
Please count the number of different ways to find exactly T direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way.
As the number of different ways can turn out rather big, print it modulo k.
Input
The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 109) — the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation.
Each of next m lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual.
Output
Print the single number — the answer to the problem modulo k.
Examples
Input
2 0 1000000000
Output
1
Input
3 0 100
Output
3
Input
4 1 1000000000
1 4
Output
8
Note
The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.
The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime — there are 3 ways to do it.
The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime. | instruction | 0 | 3,635 | 14 | 7,270 |
Tags: combinatorics, graphs
Correct Solution:
```
def dfs(node, my_cc):
vis[node] = True
acc[my_cc]+=1
for i in adj[node]:
if not vis[i]:
dfs(i, my_cc)
def bfs(node):
vis[node] = True
cola = [node]
cur = 0
while (cur < len(cola)):
x = cola[cur]
acc[cc] += 1
cur += 1;
for i in adj[x]:
if not vis[i]:
vis[i] = True
cola.append(i)
if __name__ == '__main__':
_input = input().split()
n = int(_input[0])
m = int(_input[1])
k = int(_input[2])
adj = []
vis = []
acc = []
cc = 0
for i in range(n):
vis.append(False)
adj.append([])
acc.append(0)
for i in range(m):
_in2 = input().split()
v = int(_in2[0]) - 1
w = int(_in2[1]) - 1
adj[v].append(w)
adj[w].append(v)
for i in range(n):
if not vis[i]:
# dfs(i, cc)
bfs(i)
cc+=1
if cc == 1:
print(1 % k)
exit()
ans = 1
for i in range(cc - 2):
ans = ans * n
ans = ans % k
for i in range(cc):
ans = ans * acc[i]
ans = ans % k
print(ans)
``` | output | 1 | 3,635 | 14 | 7,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues A and B and the direct link between clues B and A is the same thing. No more than one direct link can exist between two clues.
Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues A and B are considered linked either if there is a direct link between them or if there is a direct link between A and some other clue C which is linked to B.
Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals T.
Please count the number of different ways to find exactly T direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way.
As the number of different ways can turn out rather big, print it modulo k.
Input
The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 109) — the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation.
Each of next m lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual.
Output
Print the single number — the answer to the problem modulo k.
Examples
Input
2 0 1000000000
Output
1
Input
3 0 100
Output
3
Input
4 1 1000000000
1 4
Output
8
Note
The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.
The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime — there are 3 ways to do it.
The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime. | instruction | 0 | 3,636 | 14 | 7,272 |
Tags: combinatorics, graphs
Correct Solution:
```
def dfs(node, my_cc):
vis[node] = True
acc[my_cc]+=1
for i in adj[node]:
if not vis[i]:
dfs(i, my_cc)
def ittDfs(node):
queue = [node]
curr = 0
while(queue):
node = queue.pop()
if vis[node]:
continue
vis[node] = True
acc[cc] += 1
for i in adj[node]:
if not vis[i]:
queue.append(i)
def bfs(node):
vis[node] = True
cola = [node]
cur = 0
while (cur < len(cola)):
x = cola[cur]
acc[cc] += 1
cur += 1;
for i in adj[x]:
if not vis[i]:
vis[i] = True
cola.append(i)
if __name__ == '__main__':
_input = input().split()
n = int(_input[0])
m = int(_input[1])
k = int(_input[2])
adj = []
vis = []
acc = []
cc = 0
for i in range(n):
vis.append(False)
adj.append([])
acc.append(0)
for i in range(m):
_in2 = input().split()
v = int(_in2[0]) - 1
w = int(_in2[1]) - 1
adj[v].append(w)
adj[w].append(v)
for i in range(n):
if not vis[i]:
# dfs(i, cc)
ittDfs(i)
cc+=1
if cc == 1:
#print(1)
print(1 % k)
exit()
ans = 1
for i in range(cc - 2):
ans = (ans * n) % k
#ans = ans % k
for i in range(cc):
ans = (ans * acc[i]) % k
#ans = ans % k
print(ans)
``` | output | 1 | 3,636 | 14 | 7,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues A and B and the direct link between clues B and A is the same thing. No more than one direct link can exist between two clues.
Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues A and B are considered linked either if there is a direct link between them or if there is a direct link between A and some other clue C which is linked to B.
Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals T.
Please count the number of different ways to find exactly T direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way.
As the number of different ways can turn out rather big, print it modulo k.
Input
The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 109) — the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation.
Each of next m lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual.
Output
Print the single number — the answer to the problem modulo k.
Examples
Input
2 0 1000000000
Output
1
Input
3 0 100
Output
3
Input
4 1 1000000000
1 4
Output
8
Note
The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.
The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime — there are 3 ways to do it.
The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime. | instruction | 0 | 3,637 | 14 | 7,274 |
Tags: combinatorics, graphs
Correct Solution:
```
def dfs(node, my_cc):
vis[node] = True
acc[my_cc]+=1
for i in adj[node]:
if not vis[i]:
dfs(i, my_cc)
def ittDfs(node):
queue = [node]
curr = 0
while(queue):
node = queue.pop()
if vis[node]:
continue
vis[node] = True
acc[cc] += 1
for i in adj[node]:
if not vis[i]:
queue.append(i)
def bfs(node):
vis[node] = True
cola = [node]
cur = 0
while (cur < len(cola)):
x = cola[cur]
acc[cc] += 1
cur += 1;
for i in adj[x]:
if not vis[i]:
vis[i] = True
cola.append(i)
if __name__ == '__main__':
_input = input().split()
n = int(_input[0])
m = int(_input[1])
k = int(_input[2])
adj = []
vis = []
acc = []
cc = 0
for i in range(n):
vis.append(False)
adj.append([])
acc.append(0)
for i in range(m):
_in2 = input().split()
v = int(_in2[0]) - 1
w = int(_in2[1]) - 1
adj[v].append(w)
adj[w].append(v)
for i in range(n):
if not vis[i]:
# dfs(i, cc)
ittDfs(i)
cc+=1
if cc == 1:
print(1 % k)
exit()
ans = 1
for i in range(cc - 2):
ans = ans * n
ans = ans % k
for i in range(cc):
ans = ans * acc[i]
ans = ans % k
print(ans)
``` | output | 1 | 3,637 | 14 | 7,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,698 | 14 | 7,396 |
Tags: brute force, implementation
Correct Solution:
```
n,m = input().split()
n =int(n)
m = int(m)
arr = [int(x) for x in input().split()]
min = 1000000000000
ans= -1
for i in range(m):
sum=0
for j in range(i,n,m):
sum=sum+arr[j]
if sum<min:
min=sum
ans=i
print(ans+1)
``` | output | 1 | 3,698 | 14 | 7,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,699 | 14 | 7,398 |
Tags: brute force, implementation
Correct Solution:
```
n, k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10 ** 9
sol = -1
for i in range(k):
x = n // k;
y = i;
tmp = 0
while(x):
tmp += a[y]
y += k
y %= n
x -= 1
if tmp < ans:
ans = tmp
sol = i + 1
print(sol);
``` | output | 1 | 3,699 | 14 | 7,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,700 | 14 | 7,400 |
Tags: brute force, implementation
Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
m=1000000000
ans=1
for i in range(k):
mt=0
for j in range(i,n,k):
mt+=a[j]
if mt<m:
m=mt
ans=i+1
print(ans)
``` | output | 1 | 3,700 | 14 | 7,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,701 | 14 | 7,402 |
Tags: brute force, implementation
Correct Solution:
```
from sys import *
input = lambda:stdin.readline()
int_arr = lambda : list(map(int,stdin.readline().strip().split()))
str_arr = lambda :list(map(str,stdin.readline().split()))
get_str = lambda : map(str,stdin.readline().strip().split())
get_int = lambda: map(int,stdin.readline().strip().split())
get_float = lambda : map(float,stdin.readline().strip().split())
mod = 1000000007
setrecursionlimit(1000)
n,k = get_int()
arr = int_arr()
res = [0] * k
for i in range(n):
res[i % k] += arr[i]
print(res.index(min(res)) + 1)
``` | output | 1 | 3,701 | 14 | 7,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,702 | 14 | 7,404 |
Tags: brute force, implementation
Correct Solution:
```
n, k = list(map(int, input().split()))
num = list(map(int, input().split()))
dp = [0] * (2 * n)
for i in range(n - 1, -1, -1):
dp[i] = dp[i + k] + num[i]
ans = 2**100
for i in range(0, k):
ans = min(ans, dp[i])
print(dp.index(ans) + 1)
``` | output | 1 | 3,702 | 14 | 7,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,703 | 14 | 7,406 |
Tags: brute force, implementation
Correct Solution:
```
def mi():
return map(int, input().split())
n,k = mi()
a = list(mi())
dp = [0]*k
for i in range(k):
for j in range(i, n, k):
dp[i]+=a[j]
print (1+dp.index(min(dp)))
``` | output | 1 | 3,703 | 14 | 7,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,704 | 14 | 7,408 |
Tags: brute force, implementation
Correct Solution:
```
n,k = map(int,input().split())
arr = list(map(int,input().split()))
min1=100000000000
c=0
for i in range(k):
sum1=sum(arr[i::k])
# print(sum1)
if sum1<min1:
c=i
min1= sum1
print(c+1)
``` | output | 1 | 3,704 | 14 | 7,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | instruction | 0 | 3,705 | 14 | 7,410 |
Tags: brute force, implementation
Correct Solution:
```
data = input().split(" ")
tasks = int(data[0])
k = int(data[1])
cost = input().split(" ")
cost = [int(x) for x in cost]
minCost = sum(cost)
ans = 1
for i in range(k):
start = i
c = 0
for j in range(start, tasks, k):
c += cost[j]
if c < minCost:
minCost = c
ans = i+1
print(ans)
``` | output | 1 | 3,705 | 14 | 7,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
import sys
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def tinput():
return input().split()
def rinput():
return map(int, tinput())
def rlinput():
return list(rinput())
n,k = rinput()
A = rlinput()
ans = []
index = 0
for i in range(k):
cur = 0
for j in range(i,n,k):
cur += A[j]
ans.append(cur)
if i == 0:
min = cur
index = 1
else:
if cur < min:
min = cur
index = i + 1
print(index)
``` | instruction | 0 | 3,706 | 14 | 7,412 |
Yes | output | 1 | 3,706 | 14 | 7,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
start_job = 1
min_hate = 1000 * n + 1
for i in range(k):
hate = sum(a[i::k])
if hate < min_hate:
start_job = i + 1
min_hate = hate
print(start_job)
``` | instruction | 0 | 3,707 | 14 | 7,414 |
Yes | output | 1 | 3,707 | 14 | 7,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n,m = input().split(" ")
n=int(n)
m=int(m)
arr=list()
arr = list(map(int, input().split()))
for i in range(n):
arr.append(arr[i])
d=[None]*2*n
mn=10000000000000000
j=0
for i in range(2*n):
if i-m>=0:
d[i]=d[i-m]+arr[i]
else:
d[i]=arr[i]
if i>=n:
if mn > d[i]-d[i-n]:
mn = d[i]-d[i-n]
j=i-n
print(j+1)
``` | instruction | 0 | 3,708 | 14 | 7,416 |
Yes | output | 1 | 3,708 | 14 | 7,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
print(min((sum(a[i::k]), i) for i in range(k))[1] + 1)
``` | instruction | 0 | 3,709 | 14 | 7,418 |
Yes | output | 1 | 3,709 | 14 | 7,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
ms = 100000000
ind = 0
for i in range(k):
cs = sum(A[i::k])
#print(i,cs)
if ms > cs:
ms = cs
ind = i + 1
print(ind)
``` | instruction | 0 | 3,710 | 14 | 7,420 |
No | output | 1 | 3,710 | 14 | 7,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
import math
n,k = map(int, input().strip().split(' '))
lst = list(map(int, input().strip().split(' ')))
if k==1:
print(1)
elif n%k==0:
l=[0]*(n//k)
for j in range(n):
l[(j)%(n//k)]+=lst[j]
l1=l.index(min(l))
print(l1+1)
else:
lst=lst*2
l1=math.ceil(n/k)
i=0
m=2*(10**8)
t=0
while(i<n):
i1=i
c=0
m1=0
while(c<l1):
m1+=lst[i1]
c+=1
i1+=k
#print(i1)
if m1<m:
t=i
m=m1
i+=1
print(t+1)
``` | instruction | 0 | 3,711 | 14 | 7,422 |
No | output | 1 | 3,711 | 14 | 7,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(n):
d[i] = a[i]
maxi = 999999999999999999999
imd = 0
for i in range(k):
c = i+k
powe = a[i]
while(c>i):
powe+=a[c]
c = (c+k)%n
print(powe, c)
if maxi>powe:
maxi = powe
inde = i
print(inde+1)
``` | instruction | 0 | 3,712 | 14 | 7,424 |
No | output | 1 | 3,712 | 14 | 7,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
Input
The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.
It is guaranteed that n is divisible by k.
Output
In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.
Examples
Input
6 2
3 2 1 6 5 4
Output
1
Input
10 5
1 3 5 7 9 9 4 1 8 5
Output
3
Note
Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
Submitted Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=n//k
if l==1:
print(1)
else:
m=0
c=9999999
for i in range(n):
if i+k>n-1:
break
else:
if c>(a[i]+a[i+k]):
c=a[i]+a[i+k]
m=i
print(m+1)
``` | instruction | 0 | 3,713 | 14 | 7,426 |
No | output | 1 | 3,713 | 14 | 7,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.
One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.
Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
Input
The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.
Output
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
Examples
Input
3
5 6
1 2
Output
5
Input
3
5 6
1 3
Output
11 | instruction | 0 | 3,715 | 14 | 7,430 |
Tags: implementation
Correct Solution:
```
n=int(input())
d=list(map(int,input().split()))
a,b=list(map(int,input().split()))
a-=1
b-=1
ans,i=0,0
while a<b:
ans+=d[a]
a+=1
print ('{0}'.format(ans))
``` | output | 1 | 3,715 | 14 | 7,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.
One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.
Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
Input
The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.
Output
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
Examples
Input
3
5 6
1 2
Output
5
Input
3
5 6
1 3
Output
11 | instruction | 0 | 3,718 | 14 | 7,436 |
Tags: implementation
Correct Solution:
```
n = int(input())
times = [int(x) for x in input().split()]
a, b = [int(x) for x in input().split()]
res = 0
for i in range (a - 1, b - 1):
res += times[i]
print(res)
``` | output | 1 | 3,718 | 14 | 7,437 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.
One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.
Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
Input
The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.
Output
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
Examples
Input
3
5 6
1 2
Output
5
Input
3
5 6
1 3
Output
11 | instruction | 0 | 3,721 | 14 | 7,442 |
Tags: implementation
Correct Solution:
```
n=int(input())
ls=list(map(int,input().split()))
a,b=map(int,input().split())
years=0
for i in range(a-1,b-1):
years+=ls[i]
print(years)
``` | output | 1 | 3,721 | 14 | 7,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once Vasya needed to transport m goats and m wolves from riverbank to the other as quickly as possible. The boat can hold n animals and Vasya, in addition, he is permitted to put less than n animals in the boat. If in one place (on one of the banks or in the boat) the wolves happen to strictly outnumber the goats, then the wolves eat the goats and Vasya gets upset. When Vasya swims on the boat from one shore to the other, he must take at least one animal to accompany him, otherwise he will get bored and he will, yet again, feel upset. When the boat reaches the bank, first all the animals get off simultaneously, and then the animals chosen by Vasya simultaneously get on the boat. That means that at the moment when the animals that have just arrived have already got off and the animals that are going to leave haven't yet got on, somebody might eat someone. Vasya needs to transport all the animals from one river bank to the other so that nobody eats anyone and Vasya doesn't get upset. What is the minimal number of times he will have to cross the river?
Input
The first line contains two space-separated numbers m and n (1 ≤ m, n ≤ 105) — the number of animals and the boat's capacity.
Output
If it is impossible to transport all the animals so that no one got upset, and all the goats survived, print -1. Otherwise print the single number — how many times Vasya will have to cross the river.
Examples
Input
3 2
Output
11
Input
33 3
Output
-1
Note
The first sample match to well-known problem for children.
Submitted Solution:
```
a = "2 3"
a = a.split()
m = int(a[0])
n = int(a[1])
l, c, ll, cc, lll, ccc = m, m, 0, 0, 0, 0
nb = 0
if(n>=m):
nb = 0
else:
while((lll!= m or ccc!=m) and nb >= 0):
#print(l, c, "-", ll, cc, "-", lll, ccc)
nb += 1
if(nb%2 == 1):
while(ll+cc<n and l+c>0):
if(l >= c and l > 0):
l -= 1
ll += 1
elif(c > 0):
c -= 1
cc += 1
else:
while(ll+cc>1):
if(cc>=1):
cc -= 1
ccc +=1
else:
ll -= 1
lll += 1
if(ll>=1 and ccc == m):
ll -= 1
lll += 1
if((lll>ccc and ccc>0) or (l>c and c>0) or (ll == 0 and cc == 0 and ccc != m and lll != m) or (ll>cc and cc>0) or nb > 1000):
nb = -2
#print(l, c, "-", ll, cc, "-", lll, ccc)
print(nb+1)
``` | instruction | 0 | 3,739 | 14 | 7,478 |
No | output | 1 | 3,739 | 14 | 7,479 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.