message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a circular pond with a perimeter of K meters, and N houses around them.
The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.
When traveling between these houses, you can only go around the pond.
Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.
Constraints
* 2 \leq K \leq 10^6
* 2 \leq N \leq 2 \times 10^5
* 0 \leq A_1 < ... < A_N < K
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
K N
A_1 A_2 ... A_N
Output
Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.
Examples
Input
20 3
5 10 15
Output
10
Input
20 3
0 5 15
Output
10
Submitted Solution:
```
K,N = map(int,input().split())
A = [0] + list(map(int,input().split())) + [K]
B = sorted([(A[i+1]- A[i]) for i in range(N+1)])
ans = K - B[-1]
print(ans)
``` | instruction | 0 | 76,488 | 1 | 152,976 |
No | output | 1 | 76,488 | 1 | 152,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,684 | 1 | 153,368 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
from sys import stdin, stdout
def main():
p = 998244353 # Constante brindada por el problema
n = int(stdin.readline())
a = list(readline())
answer = a[-1]
pow_ = 1 # Potencia de 2
for i in range(n - 1, 0, -1): # Se analizan todas las dificultades
answer = (answer + a[i - 1] * (2 + n - i) * pow_ % p) % p # Se calcula la expresion
pow_ = pow_ * 2 % p # Se aumenta la potencia de 2
return answer
def readline(): # Metodo para leer una linea completa, dividirla en elementos y convertirlos en numeros enteros
return map(int, stdin.readline().strip().split())
if __name__ == '__main__':
stdout.write(str(main()) + '\n')
``` | output | 1 | 76,684 | 1 | 153,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,685 | 1 | 153,370 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
m=998244353
a=a[::-1]
s=a[0]
for i in range(1,n):
s+=(a[i]*pow(2,i-1,m)*(i+2))%m
s=s%m
print(s%m)
``` | output | 1 | 76,685 | 1 | 153,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,686 | 1 | 153,372 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
from sys import stdin
n = int(input())
a = list(map(int, stdin.readline().split()))
mod = 998244353
def solve(a):
if len(a) == 1:
return a[0]
dp = [0] * n
dp[0] = a[0]
dp[1] = a[0] + a[1]
for i in range(2, len(a)):
dp[i] = ( a[i] + a[i-1] + (dp[i-1] - a[i-1]) * 2 ) % mod
for i in range(1, len(a)):
dp[i] = ( 2*dp[i-1] + dp[i] ) % mod
return dp[-1]
print(solve(a))
``` | output | 1 | 76,686 | 1 | 153,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,687 | 1 | 153,374 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
M = 0x3b800001
wa = 0;
n = int(input())
a = list(map(int, input().split()))
now = 1
wa += a[-1]
for i in range(n - 1)[::-1]:
wa += (now * (n - i - 1) + now * 2) * a[i]
wa %= M
now *= 2
now %= M
print(wa % M)
``` | output | 1 | 76,687 | 1 | 153,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,688 | 1 | 153,376 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
difficulty = a[0]
expectation = a[0] % 998244353
for i in range(1, n):
expectation = expectation * 2 + difficulty + a[i]
difficulty = difficulty * 2 + a[i]
expectation %= 998244353
difficulty %= 998244353
print(expectation)
``` | output | 1 | 76,688 | 1 | 153,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,689 | 1 | 153,378 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
n = int(input())
a= list(map(int,input().split()))
sum, cur = a[0] % 998244353, a[0]
for x in a[1:]:
sum = sum * 2 + cur + x
cur = cur * 2 + x
sum %= 998244353
cur %= 998244353
print(sum)
``` | output | 1 | 76,689 | 1 | 153,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,690 | 1 | 153,380 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------------------------------------------------------------
#mod = 9223372036854775807
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: a+b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
class SegmentTree1:
def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
MOD=10**9+7
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod=10**9+7
omod=998244353
#---------------------------------Lazy Segment Tree--------------------------------------
# https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp
class LazySegTree:
def __init__(self, _op, _e, _mapping, _composition, _id, v):
def set(p, x):
assert 0 <= p < _n
p += _size
for i in range(_log, 0, -1):
_push(p >> i)
_d[p] = x
for i in range(1, _log + 1):
_update(p >> i)
def get(p):
assert 0 <= p < _n
p += _size
for i in range(_log, 0, -1):
_push(p >> i)
return _d[p]
def prod(l, r):
assert 0 <= l <= r <= _n
if l == r:
return _e
l += _size
r += _size
for i in range(_log, 0, -1):
if ((l >> i) << i) != l:
_push(l >> i)
if ((r >> i) << i) != r:
_push(r >> i)
sml = _e
smr = _e
while l < r:
if l & 1:
sml = _op(sml, _d[l])
l += 1
if r & 1:
r -= 1
smr = _op(_d[r], smr)
l >>= 1
r >>= 1
return _op(sml, smr)
def apply(l, r, f):
assert 0 <= l <= r <= _n
if l == r:
return
l += _size
r += _size
for i in range(_log, 0, -1):
if ((l >> i) << i) != l:
_push(l >> i)
if ((r >> i) << i) != r:
_push((r - 1) >> i)
l2 = l
r2 = r
while l < r:
if l & 1:
_all_apply(l, f)
l += 1
if r & 1:
r -= 1
_all_apply(r, f)
l >>= 1
r >>= 1
l = l2
r = r2
for i in range(1, _log + 1):
if ((l >> i) << i) != l:
_update(l >> i)
if ((r >> i) << i) != r:
_update((r - 1) >> i)
def _update(k):
_d[k] = _op(_d[2 * k], _d[2 * k + 1])
def _all_apply(k, f):
_d[k] = _mapping(f, _d[k])
if k < _size:
_lz[k] = _composition(f, _lz[k])
def _push(k):
_all_apply(2 * k, _lz[k])
_all_apply(2 * k + 1, _lz[k])
_lz[k] = _id
_n = len(v)
_log = _n.bit_length()
_size = 1 << _log
_d = [_e] * (2 * _size)
_lz = [_id] * _size
for i in range(_n):
_d[_size + i] = v[i]
for i in range(_size - 1, 0, -1):
_update(i)
self.set = set
self.get = get
self.prod = prod
self.apply = apply
MIL = 1 << 20
def makeNode(total, count):
# Pack a pair into a float
return (total * MIL) + count
def getTotal(node):
return math.floor(node / MIL)
def getCount(node):
return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)
def nodeOp(node1, node2):
return node1 + node2
# Equivalent to the following:
return makeNode(
getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2)
)
identityMapping = -1
def mapping(tag, node):
if tag == identityMapping:
return node
# If assigned, new total is the number assigned times count
count = getCount(node)
return makeNode(tag * count, count)
def composition(mapping1, mapping2):
# If assigned multiple times, take first non-identity assignment
return mapping1 if mapping1 != identityMapping else mapping2
#-------------------------------------------------------------------------
prime = [True for i in range(10)]
pp=[0]*10
def SieveOfEratosthenes(n=10):
p = 2
c=0
while (p * p <= n):
if (prime[p] == True):
c+=1
for i in range(p, n+1, p):
pp[i]+=1
prime[i] = False
p += 1
#---------------------------------Binary Search------------------------------------------
def binarySearch(arr, n, key):
left = 0
right = n-1
mid = 0
res=arr[n-1]
while (left <= right):
mid = (right + left)//2
if (arr[mid] >= key):
res=arr[mid]
right = mid-1
else:
left = mid + 1
return res
def binarySearch1(arr, n, key):
left = 0
right = n-1
mid = 0
res=arr[0]
while (left <= right):
mid = (right + left)//2
if (arr[mid] > key):
right = mid-1
else:
res=arr[mid]
left = mid + 1
return res
#---------------------------------running code------------------------------------------
n = int(input())
a = list(map(int, input().split()))
difficulty = a[0]
expectation = a[0] % 998244353
for i in range(1, n):
expectation = expectation * 2 + difficulty + a[i]
difficulty = difficulty * 2 + a[i]
expectation %= 998244353
difficulty %= 998244353
print(expectation)
``` | output | 1 | 76,690 | 1 | 153,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60 | instruction | 0 | 76,691 | 1 | 153,382 |
Tags: combinatorics, math, probabilities
Correct Solution:
```
#!/usr/bin/python3
MOD = 998244353
def solve(N, A):
ans = 0
k = 1
ans += k * A[-1]
ans %= MOD
for i in range(N - 2, -1, -1):
ans += k * (N + 1 - i) * A[i]
ans %= MOD
k *= 2
k %= MOD
return ans
def main():
N = int(input())
A = [int(e) for e in input().split(' ')]
assert len(A) == N
print(solve(N, A))
if __name__ == '__main__':
main()
``` | output | 1 | 76,691 | 1 | 153,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
a = int(input())
b = list(map(int,input().split()))
c = b[0]%998244353
d = b[0]%998244353
for i in range(1,a):
c = (2*c + d + b[i])%998244353
d = (2*d + b[i])%998244353
print(c)
``` | instruction | 0 | 76,692 | 1 | 153,384 |
Yes | output | 1 | 76,692 | 1 | 153,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
MOD = 998244353
def main():
n = int(input())
a = [int(x) for x in input().split(' ')]
p, sp, s, ss = 0, 0, 0, 0
for x in a:
ss = (2 * ss + s) % MOD
s = (s + x) % MOD
p = (ss + sp + s) % MOD
sp = (sp + p) % MOD
print(p)
if __name__ == '__main__':
main()
``` | instruction | 0 | 76,693 | 1 | 153,386 |
Yes | output | 1 | 76,693 | 1 | 153,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
# from math import *
from collections import *
# from fractions import *
# from heapq import*
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=998244353
EPS=1e-6
def Ceil(a,b): return a//b+int(a%b>0)
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n=Int()
a=array()
series = [1]
fact = 1
for i in range(n+1):
series.append(((series[-1]*2)%M + fact)%M)
fact = (fact*2)%M
ind = n-1
ans=0
for i in range(n):
ans = (ans + (a[i]*series[ind])%M )%M
ind-=1
print(ans)
``` | instruction | 0 | 76,694 | 1 | 153,388 |
Yes | output | 1 | 76,694 | 1 | 153,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
mod = 998244353
def calc(nn, costs):
total = costs[-1]
mult = 1
for a in range(nn-1, 0, -1):
total = (total + ((costs[a - (nn+1)] * mult) % mod) * (nn - a + 2) % mod) % mod
mult = (mult * 2) % mod
return total
# calc(1000000, [1000000 for a in range(1000000)])
print(calc(int(input()), list(map(int, input().split(" ")))))
``` | instruction | 0 | 76,695 | 1 | 153,390 |
Yes | output | 1 | 76,695 | 1 | 153,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
n = int(input())
ans = 0
mod = 998244353
a = list(map(int, input().split()))
p = 1 / 2
for i in range(n):
ans = (ans + (i + 2) * p * a[n - i - 1] % mod) % mod
p = (2 * p) % mod
print(int(ans) % mod)
``` | instruction | 0 | 76,696 | 1 | 153,392 |
No | output | 1 | 76,696 | 1 | 153,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
#/user/bin/python
if __name__ == '__main__':
sum = 0
_n = input()
_s = input()
s = _s.split(' ')
n = int(_n)
taxi = 0
group3 = 0
group2 = 0
group1 = 0
for i in range(0, n):
if int(s[i]) == 4:
taxi += 1
elif int(s[i]) == 3:
group3 += 1
elif int(s[i]) == 2:
group2 += 1
elif int(s[i]) == 1:
group1 += 1
print()
if group2 >= 2:
taxi += group2/2
if group2%2 != 0:
group2 = 1
else:
group2 = 0
if group3 <= group1:
taxi += group3
group1 -= group3
elif group3 > group1:
taxi += group3
if group1 >= 4:
if group1%4 == 0:
taxi += group1/4
if (group2 > 0) or (group1 == 2) or (group1 == 1):
taxi += group2
group1 -= group2*2
if group1 > 0:
taxi += 1
print(int(taxi))
``` | instruction | 0 | 76,697 | 1 | 153,394 |
No | output | 1 | 76,697 | 1 | 153,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
difficulty = a[0]
expectation = a[0]
for i in range(1, n):
difficulty = difficulty - (a[i - 1] - a[i]) // 2**(i - 1)
expectation += difficulty * (i + 1)
print(expectation)
``` | instruction | 0 | 76,698 | 1 | 153,396 |
No | output | 1 | 76,698 | 1 | 153,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov β at coordinate n km.
Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i β [1, n - 1] a_i β€ a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it β difficulty a_2, and so on.
For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one β a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth β a_2, and the last one β a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p β the expected value of difficulty of his journey.
Obviously, p β
2^{n - 1} is an integer number. You have to calculate it modulo 998244353.
Input
The first line contains one number n (1 β€ n β€ 10^6) β the distance from Moscow to Saratov.
The second line contains n integer numbers a_1, a_2, ..., a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ 10^6), where a_i is the difficulty of i-th kilometer after Leha has rested.
Output
Print one number β p β
2^{n - 1}, taken modulo 998244353.
Examples
Input
2
1 2
Output
5
Input
4
1 3 3 7
Output
60
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
m = 998244353
total = 0
prev = 0
diff = []
diff.append(a[0])
for i in range(1,n):
d = diff[-1] - a[i-1]/2**i + a[i] / 2 ** i
diff.append(d)
print (int(sum(diff) * 2 ** (n-1) % m))
``` | instruction | 0 | 76,699 | 1 | 153,398 |
No | output | 1 | 76,699 | 1 | 153,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,736 | 1 | 153,472 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
c=list(map(int,input().split()))
p=n-1
while c[0]==c[p] and c[0]==c[n-1-p]: p-=1
print(p)
``` | output | 1 | 76,736 | 1 | 153,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,737 | 1 | 153,474 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
t = [int(i) for i in input().split()]
for i in range(n-1, 0, -1):
if t[i] != t[0]:
f = i
break
for i in range(n):
if t[i] != t[-1]:
l = i
break
print(max(f, n-l-1))
``` | output | 1 | 76,737 | 1 | 153,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,738 | 1 | 153,476 |
Tags: greedy, implementation
Correct Solution:
```
### Ilya and a Colorful Walk ###
### https://codeforces.com/contest/1119/problem/A ###
N = int(input())
Nhouse = [int(c) for c in input().split()]
kq = []
i = 0
j = N-2
while j !=0 and i != N-2:
if Nhouse[N-1] != Nhouse[i]:
kq.append(N-1-i)
if Nhouse[0] != Nhouse[i]:
kq.append(i)
i = i+1
if Nhouse[0] != Nhouse[j]:
kq.append(j)
if Nhouse[N-1] != Nhouse[j]:
kq.append(N-1-j)
j = j-1
print(max(kq))
``` | output | 1 | 76,738 | 1 | 153,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,739 | 1 | 153,478 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
c = list(map(int, input().split()))
if c[0] != c[n - 1]:
print(n - 1)
else:
color = c[0]
i = 1
while c[i] == color == c[n - i - 1]:
i += 1
print(n - i - 1)
``` | output | 1 | 76,739 | 1 | 153,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,740 | 1 | 153,480 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
l_d = 0
r_d = 0
for i in range(n):
if a[i] != a[0]:
if i > l_d:
l_d = i
if a[n - 1 - i] != a[n - 1]:
if i > r_d:
r_d = i
print(max(l_d, r_d))
``` | output | 1 | 76,740 | 1 | 153,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,741 | 1 | 153,482 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
L=list(map(int,input().split()))
i=0
j=n-1
while L[i]==L[-1]:i+=1
while L[j]==L[0]:j-=1
print(max(n-1-i,j))
``` | output | 1 | 76,741 | 1 | 153,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,742 | 1 | 153,484 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
data = set()
for i in range(n//2):
data.add(a[i])
data.add(a[-i-1])
if len(data)>1:
print(max(n-i,i)-1)
break
else:
print(max(n-n//2,n//2)-1)
``` | output | 1 | 76,742 | 1 | 153,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units. | instruction | 0 | 76,743 | 1 | 153,486 |
Tags: greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
c = list(map(int, input().split()))
for i in range(n):
if c[i] != c[0]:
lastDiff = i
for i in range(n-1, -1, -1):
if c[i] != c[-1]:
firstDiff = i
print(max(lastDiff, n-1-firstDiff))
``` | output | 1 | 76,743 | 1 | 153,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
def power(x, p,m):
res = 1
while p:
if p & 1:
res = (res * x) % m
x = (x * x) % m
p >>= 1
return res
def inar():
return [int(k) for k in input().split()]
def lcm(num1,num2):
return (num1*num2)//gcd(num1,num2)
def main():
#t=int(input())
t=1
for _ in range(t):
n=int(input())
dic=defaultdict(list)
arr=inar()
for i in range(n):
dic[arr[i]].append(i)
ans=0
for key,item in dic.items():
first=item[0]
last=item[-1]
if first!=0:
ans=max(ans,last)
if last!=n-1:
ans=max(ans,n-first-1)
print(ans)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
#threadin.Thread(target=main).start()
``` | instruction | 0 | 76,744 | 1 | 153,488 |
Yes | output | 1 | 76,744 | 1 | 153,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
n = int(input())
a = [int(e) for e in input().split()]
ans = -1
for i in range(n)[::-1]:
if a[i] != a[0]:
ans = i
break
for i in range(n):
if a[i] != a[-1]:
ans = max(ans, n - i - 1)
break
print(ans)
``` | instruction | 0 | 76,745 | 1 | 153,490 |
Yes | output | 1 | 76,745 | 1 | 153,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
n = int(input())
x = list(map(int, input().split()))
first_index, last_index = 0, 0
if x[0] != x[-1]:
print(n - 1)
else:
for i in range(1, n - 1):
if x[i] != x[0]:
if first_index == 0:
first_index = i
last_index = i
print(max(last_index, n - first_index - 1))
``` | instruction | 0 | 76,746 | 1 | 153,492 |
Yes | output | 1 | 76,746 | 1 | 153,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
total_count = int(input().strip())
color_list = [int(x) for x in input().strip().split(" ")]
item1 = color_list[0]
item_last = color_list[total_count-1]
if item_last != item1:
print(total_count - 1)
exit()
f_count = 0
b_count = 0
for i in range(total_count):
item_f = color_list[i]
item_b = color_list[total_count-i-1]
if item_f == item1:
f_count += 1
else:
break
if item_b == item_last:
b_count += 1
else:
break
print(total_count-min(f_count, b_count)-1)
``` | instruction | 0 | 76,747 | 1 | 153,494 |
Yes | output | 1 | 76,747 | 1 | 153,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
for i in range(n-1):
if l[i] != l[-1]:
distance = n-i-1
for j in range(n-1,0,-1):
if l[j] != l[0]:
if j > distance:
distance = j
print(distance)
``` | instruction | 0 | 76,748 | 1 | 153,496 |
No | output | 1 | 76,748 | 1 | 153,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
n=int(input())
ll=list(map(int,input().split()))
ll2=[0]*(n)
for i in range(n):
if (i==0):
ll2[ll[i]]=1
elif (ll2[ll[i]]<i):
ll2[ll[i]]=i
ll=[]
for i in range(n):
if(ll2[i]!=0):
ll.append(ll2[i])
ll2=[]
print(max(ll)-min(ll)+1)
``` | instruction | 0 | 76,749 | 1 | 153,498 |
No | output | 1 | 76,749 | 1 | 153,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
n = int(input())
colors = list(map(int, input().split()))
maxdist = 0
for i in range(n-2):
j = n - 1
while j > i and colors[j] == colors[i]:
j -= 1
if j-i > maxdist:
maxdist = j - i
print(maxdist)
print(maxdist)
``` | instruction | 0 | 76,750 | 1 | 153,500 |
No | output | 1 | 76,750 | 1 | 153,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya lives in a beautiful city of Chordalsk.
There are n houses on the street Ilya lives, they are numerated from 1 to n from left to right; the distance between every two neighboring houses is equal to 1 unit. The neighboring houses are 1 and 2, 2 and 3, ..., n-1 and n. The houses n and 1 are not neighboring.
The houses are colored in colors c_1, c_2, β¦, c_n so that the i-th house is colored in the color c_i. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses i and j so that 1 β€ i < j β€ n, and they have different colors: c_i β c_j. He will then walk from the house i to the house j the distance of (j-i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer n (3 β€ n β€ 300 000) β the number of cities on the street.
The second line contains n integers c_1, c_2, β¦, c_n (1 β€ c_i β€ n) β the colors of the houses.
It is guaranteed that there is at least one pair of indices i and j so that 1 β€ i < j β€ n and c_i β c_j.
Output
Print a single integer β the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5-1 = 4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 1 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7-3 = 4 units.
Submitted Solution:
```
#7/4/19
#1119A
#BlueyNeilo
n=int(input())
c=list(map(int,input().split()))
if c[0]!=c[n-1]:
print(n-1)
else:
init = c[0]
lo = 1
hi = n-2
while c[lo]==init: lo+=1
while c[hi]==init: hi-=1
print(max(lo,n-1-hi))
``` | instruction | 0 | 76,751 | 1 | 153,502 |
No | output | 1 | 76,751 | 1 | 153,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends dΒ·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d (3 β€ n β€ 100, 103 β€ d β€ 105) β the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 β€ ai β€ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 β€ xi, yi β€ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer β the answer to the problem.
Examples
Input
3 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000 | instruction | 0 | 77,034 | 1 | 154,068 |
Tags: binary search, graphs, shortest paths
Correct Solution:
```
f = lambda: list(map(int, input().split()))
n, d = f()
r = range(n)
a = [0] + f() + [0]
p = [f() for i in r]
s = [1e9] * n
q, s[0] = 1, 0
while q:
q = 0
for i in r:
for j in r:
if i != j:
t = s[i] + (abs(p[i][0] - p[j][0]) + abs(p[i][1] - p[j][1])) * d - a[j]
if t < s[j]: q, s[j] = 1, t
print(s[-1])
``` | output | 1 | 77,034 | 1 | 154,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends dΒ·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d (3 β€ n β€ 100, 103 β€ d β€ 105) β the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 β€ ai β€ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 β€ xi, yi β€ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer β the answer to the problem.
Examples
Input
3 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000 | instruction | 0 | 77,035 | 1 | 154,070 |
Tags: binary search, graphs, shortest paths
Correct Solution:
```
from sys import stdin
from math import inf
def readline():
return map(int, stdin.readline().strip().split())
def dijkstra():
n, d = readline()
a = [0] + list(readline()) + [0]
x = [0] * n
y = [0] * n
for i in range(n):
x[i], y[i] = readline()
lower_cost = [inf] * n
lower_cost[0] = 0
visited = [False] * n
for i in range(n - 1):
lower_value = inf
position = 0
for j in range(n):
if not visited[j] and lower_value > lower_cost[j]:
lower_value = lower_cost[j]
position = j
if position == n - 1:
break
visited[position] = True
for k in range(n):
if not visited[k]:
diff = lower_cost[position] + d * (abs(x[k] - x[position]) + abs(y[k] - y[position])) - a[position]
if lower_cost[k] > diff:
lower_cost[k] = diff
return lower_cost[-1]
if __name__ == '__main__':
print(dijkstra())
``` | output | 1 | 77,035 | 1 | 154,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends dΒ·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d (3 β€ n β€ 100, 103 β€ d β€ 105) β the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 β€ ai β€ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 β€ xi, yi β€ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer β the answer to the problem.
Examples
Input
3 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000 | instruction | 0 | 77,036 | 1 | 154,072 |
Tags: binary search, graphs, shortest paths
Correct Solution:
```
f = lambda: list(map(int, input().split()))
n, d = f()
a = [0] + f() + [0]
p = [f() for i in range(n)]
r = range(n)
s = [[d * (abs(p[i][0] - p[j][0]) + abs(p[i][1] - p[j][1])) - a[j] * (i != j) for j in r] for i in r]
for k in r: s = [[min(s[i][j], s[i][k] + s[k][j]) for i in r] for j in r]
print(s[-1][0])
# Made By Mostafa_Khaled
``` | output | 1 | 77,036 | 1 | 154,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends dΒ·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d (3 β€ n β€ 100, 103 β€ d β€ 105) β the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 β€ ai β€ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 β€ xi, yi β€ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer β the answer to the problem.
Examples
Input
3 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000 | instruction | 0 | 77,038 | 1 | 154,076 |
Tags: binary search, graphs, shortest paths
Correct Solution:
```
R = lambda: map(int, input().split())
n, d = R()
a = [0] + list(R()) + [0]
x = []
y = []
INF = float('inf')
for i in range(n):
xi, yi = R()
x += [xi]
y += [yi]
b = [INF] * n
b[0] = 0
c = True
while c:
c = False
for i in range(n):
for j in range(1, n):
if i != j and b[i] != -1:
t = b[i] + (abs(x[i] - x[j]) + abs(y[i] - y[j])) * d - a[j]
if t < b[j]:
b[j] = t
c = True
print(b[-1])
``` | output | 1 | 77,038 | 1 | 154,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends dΒ·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d (3 β€ n β€ 100, 103 β€ d β€ 105) β the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 β€ ai β€ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 β€ xi, yi β€ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer β the answer to the problem.
Examples
Input
3 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000 | instruction | 0 | 77,039 | 1 | 154,078 |
Tags: binary search, graphs, shortest paths
Correct Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
n, dc = mints()
a = list(mints())
a.append(0)
x = [0]*n
y = [0]*n
for i in range(n):
x[i], y[i] = mints()
d = [1<<30]*n
d[0] = 0
was = [False]*n
for i in range(n):
m = 1<<30
mi = 0
for j in range(n):
if not was[j] and m > d[j]:
m = d[j]
mi = j
j = mi
was[j] = True
for k in range(n):
if not was[k]:
dd = d[j] + (abs(x[k]-x[j])+abs(y[k]-y[j]))*dc
dd -= a[k-1]
if d[k] > dd:
d[k] = dd
print(d[-1])
solve()
``` | output | 1 | 77,039 | 1 | 154,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point d.
Iahub has n positive integers a1, a2, ..., an. The sum of those numbers is d. Suppose p1, p2, ..., pn is a permutation of {1, 2, ..., n}. Then, let b1 = ap1, b2 = ap2 and so on. The array b is called a "route". There are n! different routes, one for each permutation p.
Iahub's travel schedule is: he walks b1 steps on Ox axis, then he makes a break in point b1. Then, he walks b2 more steps on Ox axis and makes a break in point b1 + b2. Similarly, at j-th (1 β€ j β€ n) time he walks bj more steps on Ox axis and makes a break in point b1 + b2 + ... + bj.
Iahub is very superstitious and has k integers which give him bad luck. He calls a route "good" if he never makes a break in a point corresponding to one of those k numbers. For his own curiosity, answer how many good routes he can make, modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (1 β€ n β€ 24). The following line contains n integers: a1, a2, ..., an (1 β€ ai β€ 109).
The third line contains integer k (0 β€ k β€ 2). The fourth line contains k positive integers, representing the numbers that give Iahub bad luck. Each of these numbers does not exceed 109.
Output
Output a single integer β the answer of Iahub's dilemma modulo 1000000007 (109 + 7).
Examples
Input
3
2 3 5
2
5 7
Output
1
Input
3
2 2 2
2
1 3
Output
6
Note
In the first case consider six possible orderings:
* [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him.
* [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him.
* [3, 2, 5]. He will stop at the unlucky 5.
* [3, 5, 2]. This is a valid ordering.
* [5, 2, 3]. He got unlucky twice (5 and 7).
* [5, 3, 2]. Iahub would reject, as it sends him to position 5.
In the second case, note that it is possible that two different ways have the identical set of stopping. In fact, all six possible ways have the same stops: [2, 4, 6], so there's no bad luck for Iahub.
Submitted Solution:
```
def f(currentSum, bitset):
if m[bitset] >= 0 :
return m[bitset]
elif currentSum in unlucky:
m[bitset] = 0
return 0
elif currentSum > max(unlucky):
m[bitset] = 1
return 1
else:
step = 0
for i in range(len(steps)):
if (bitset >> i) & 1 == 0:
bitset2 = bitset | (1 << i)
step += f(currentSum + steps[i], bitset | (1 << i))
if step > (10**9+7):
step -= (10**9+7)
m[bitset] = step
return step
n = int(input())
lst1 = input().split(" ")
for i in range(n):
lst1[i] = int(lst1[i])
k = int(input())
if k > 0:
lst2 = input().split(" ")
for i in range(k):
lst2[i] = int(lst2[i])
m = [-1] * (1 << n)
m[ (1 << n) - 1 ] = 1 #terminal condition
steps = tuple(lst1)
unlucky = tuple(lst2)
print(f(0, 0))
else:
from math import factorial
print(factorial(n) % (10**9+7))
``` | instruction | 0 | 77,048 | 1 | 154,096 |
No | output | 1 | 77,048 | 1 | 154,097 |
Provide a correct Python 3 solution for this coding contest problem.
We have decided to introduce an automatic ticket gate to the railway network of a certain country. One of the difficult issues to implement is determining whether a given ticket can move between designated stations. Each ticket has a boarding station and a getting-off station. With this ticket, you can not only "get on at the boarding station and get off at the getting off station", but you are also allowed to get on and off the train.
There are S stations on this rail network, of which Group R stations are adjacent and can be traversed in both directions without going through other stations. There is only one railroad track connecting adjacent stations. The distance between adjacent stations is the distance measured along this railroad track. There are multiple possible routes from one station to another, depending on the shape of the railway network, but the route with the shortest distance is called the shortest route. If there are multiple such routes, both are accepted as the shortest route.
You can move from station c to station d with a ticket for boarding station a and getting off station b if there is a route p that meets all of the following conditions.
* Route p is the shortest route from station a to station b.
* Route p is a route that starts from station a, goes through station c, then station d, and ends at station b. The section from station c to station d is the shortest route between these two stations.
You will be given route map and ticket information. Next, you will be given several pairs of start and end points, so write a program that determines whether you can move from the start point to the end point with that ticket.
input
The input consists of one dataset. Input data is given in the following format.
S R
u1 v1 w1
u2 v2 w2
::
uR vR wR
a b Q
c1 d1
::
cQ dQ
The numbers given on each line are separated by a single space.
The first line consists of two integers. S (2 β€ S β€ 100000) is the number of stations that appear on the railroad map, and R (1 β€ R β€ 200000) is the number of pairs of adjacent stations. The following R line is given information on the railroad tracks that directly connect adjacent stations. ui and vi (1 β€ ui, vi β€ S) indicate the station numbers at both ends of the i-th line. wi (1 β€ wi β€ 1000) is an integer representing the distance between these stations. However, numbers from 1 to S are assigned to each station without duplication, and ui β vi.
The next line consists of three integers. The first two integers represent the ticket sections, where a is the boarding station and b is the getting-off station (1 β€ a, b β€ S). The third integer Q (1 β€ Q β€ 40000) indicates the number of questions. The question is given on the following Q line. ci and di (1 β€ ci, di β€ S) indicate the boarding and alighting stations of the i-th question. However, a β b and ci β di.
output
For each question, print Yes if you can move with the given ticket, or No if you can't.
Example
Input
6 7
1 2 3
1 4 1
2 3 5
4 3 1
3 6 2
4 5 2
5 6 1
1 6 6
1 6
4 3
4 6
5 6
2 6
2 5
Output
Yes
Yes
Yes
Yes
No
No | instruction | 0 | 77,508 | 1 | 155,016 |
"Correct Solution:
```
from heapq import heappush, heappop
s, r = map(int, input().split())
edges = [[] for _ in range(s)]
for _ in range(r):
u, v, w = map(int, input().split())
u -= 1
v -= 1
edges[u].append((v, w))
edges[v].append((u, w))
a, b, q = map(int, input().split())
a -= 1
b -= 1
def dijkstra(start):
INF = 10 ** 20
dist = [INF] * s
dist[start] = 0
parents = [[] for _ in range(s)]
que = []
heappush(que, (0, start))
while que:
score, node = heappop(que)
for to, w in edges[node]:
if dist[to] > score + w:
dist[to] = score + w
parents[to] = {node}
heappush(que, (score + w, to))
elif dist[to] == score + w:
parents[to].add(node)
return dist, parents
def on_shortest_path(c, d, mem):
if c == d:return True
if d in mem:return False
mem.add(d)
if dist_from_a[c] >= dist_from_a[d]:return False
for parent in parents[d]:
if on_shortest_path(c, parent, mem):return True
return False
dist_from_a, parents = dijkstra(a)
dist_from_b, _ = dijkstra(b)
shortest = dist_from_a[b]
for _ in range(q):
c, d = map(int, input().split())
c -= 1
d -= 1
if dist_from_a[c] + dist_from_b[c] == shortest and \
dist_from_a[d] + dist_from_b[d] == shortest and \
on_shortest_path(c, d, set()):
print("Yes")
else:
print("No")
``` | output | 1 | 77,508 | 1 | 155,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have decided to introduce an automatic ticket gate to the railway network of a certain country. One of the difficult issues to implement is determining whether a given ticket can move between designated stations. Each ticket has a boarding station and a getting-off station. With this ticket, you can not only "get on at the boarding station and get off at the getting off station", but you are also allowed to get on and off the train.
There are S stations on this rail network, of which Group R stations are adjacent and can be traversed in both directions without going through other stations. There is only one railroad track connecting adjacent stations. The distance between adjacent stations is the distance measured along this railroad track. There are multiple possible routes from one station to another, depending on the shape of the railway network, but the route with the shortest distance is called the shortest route. If there are multiple such routes, both are accepted as the shortest route.
You can move from station c to station d with a ticket for boarding station a and getting off station b if there is a route p that meets all of the following conditions.
* Route p is the shortest route from station a to station b.
* Route p is a route that starts from station a, goes through station c, then station d, and ends at station b. The section from station c to station d is the shortest route between these two stations.
You will be given route map and ticket information. Next, you will be given several pairs of start and end points, so write a program that determines whether you can move from the start point to the end point with that ticket.
input
The input consists of one dataset. Input data is given in the following format.
S R
u1 v1 w1
u2 v2 w2
::
uR vR wR
a b Q
c1 d1
::
cQ dQ
The numbers given on each line are separated by a single space.
The first line consists of two integers. S (2 β€ S β€ 100000) is the number of stations that appear on the railroad map, and R (1 β€ R β€ 200000) is the number of pairs of adjacent stations. The following R line is given information on the railroad tracks that directly connect adjacent stations. ui and vi (1 β€ ui, vi β€ S) indicate the station numbers at both ends of the i-th line. wi (1 β€ wi β€ 1000) is an integer representing the distance between these stations. However, numbers from 1 to S are assigned to each station without duplication, and ui β vi.
The next line consists of three integers. The first two integers represent the ticket sections, where a is the boarding station and b is the getting-off station (1 β€ a, b β€ S). The third integer Q (1 β€ Q β€ 40000) indicates the number of questions. The question is given on the following Q line. ci and di (1 β€ ci, di β€ S) indicate the boarding and alighting stations of the i-th question. However, a β b and ci β di.
output
For each question, print Yes if you can move with the given ticket, or No if you can't.
Example
Input
6 7
1 2 3
1 4 1
2 3 5
4 3 1
3 6 2
4 5 2
5 6 1
1 6 6
1 6
4 3
4 6
5 6
2 6
2 5
Output
Yes
Yes
Yes
Yes
No
No
Submitted Solution:
```
from heapq import heappush, heappop
def main():
INF = 10 ** 20
s, r = map(int, input().split())
edges = [[] for _ in range(s)]
for _ in range(r):
u, v, w = map(int, input().split())
u -= 1
v -= 1
edges[u].append((v, w))
edges[v].append((u, w))
a, b, q = map(int, input().split())
a -= 1
b -= 1
costs = [(INF, [])] * s
costs[a] = (0, [[a]])
que = []
heappush(que, (0, a))
while que:
dist, node = heappop(que)
total, paths = costs[node]
for to, weight in edges[node]:
to_total, to_paths = costs[to]
if to_total > total + weight:
costs[to] = (total + weight, [lst + [to] for lst in paths])
heappush(que, (total + weight, to))
elif to_total == total + weight:
costs[to] = (to_total, to_paths + [lst + [to] for lst in paths])
_, atob = costs[b]
from_to = [set() for _ in range(s)]
for path in atob:
for i, p in enumerate(path):
from_to[p] = from_to[p] | set(path[i + 1:])
for _ in range(q):
start, goal = map(int, input().split())
start -= 1
goal -= 1
if goal in from_to[start]:
print("Yes")
else:
print("No")
main()
``` | instruction | 0 | 77,509 | 1 | 155,018 |
No | output | 1 | 77,509 | 1 | 155,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have decided to introduce an automatic ticket gate to the railway network of a certain country. One of the difficult issues to implement is determining whether a given ticket can move between designated stations. Each ticket has a boarding station and a getting-off station. With this ticket, you can not only "get on at the boarding station and get off at the getting off station", but you are also allowed to get on and off the train.
There are S stations on this rail network, of which Group R stations are adjacent and can be traversed in both directions without going through other stations. There is only one railroad track connecting adjacent stations. The distance between adjacent stations is the distance measured along this railroad track. There are multiple possible routes from one station to another, depending on the shape of the railway network, but the route with the shortest distance is called the shortest route. If there are multiple such routes, both are accepted as the shortest route.
You can move from station c to station d with a ticket for boarding station a and getting off station b if there is a route p that meets all of the following conditions.
* Route p is the shortest route from station a to station b.
* Route p is a route that starts from station a, goes through station c, then station d, and ends at station b. The section from station c to station d is the shortest route between these two stations.
You will be given route map and ticket information. Next, you will be given several pairs of start and end points, so write a program that determines whether you can move from the start point to the end point with that ticket.
input
The input consists of one dataset. Input data is given in the following format.
S R
u1 v1 w1
u2 v2 w2
::
uR vR wR
a b Q
c1 d1
::
cQ dQ
The numbers given on each line are separated by a single space.
The first line consists of two integers. S (2 β€ S β€ 100000) is the number of stations that appear on the railroad map, and R (1 β€ R β€ 200000) is the number of pairs of adjacent stations. The following R line is given information on the railroad tracks that directly connect adjacent stations. ui and vi (1 β€ ui, vi β€ S) indicate the station numbers at both ends of the i-th line. wi (1 β€ wi β€ 1000) is an integer representing the distance between these stations. However, numbers from 1 to S are assigned to each station without duplication, and ui β vi.
The next line consists of three integers. The first two integers represent the ticket sections, where a is the boarding station and b is the getting-off station (1 β€ a, b β€ S). The third integer Q (1 β€ Q β€ 40000) indicates the number of questions. The question is given on the following Q line. ci and di (1 β€ ci, di β€ S) indicate the boarding and alighting stations of the i-th question. However, a β b and ci β di.
output
For each question, print Yes if you can move with the given ticket, or No if you can't.
Example
Input
6 7
1 2 3
1 4 1
2 3 5
4 3 1
3 6 2
4 5 2
5 6 1
1 6 6
1 6
4 3
4 6
5 6
2 6
2 5
Output
Yes
Yes
Yes
Yes
No
No
Submitted Solution:
```
from heapq import heappush, heappop
def main():
INF = 10 ** 20
s, r = map(int, input().split())
edges = [[] for _ in range(s)]
for _ in range(r):
u, v, w = map(int, input().split())
u -= 1
v -= 1
edges[u].append((v, w))
edges[v].append((u, w))
a, b, q = map(int, input().split())
a -= 1
b -= 1
costs = [(INF, [])] * s
costs[a] = (0, [[a]])
que = []
heappush(que, a)
while que:
node = heappop(que)
total, paths = costs[node]
for to, weight in edges[node]:
to_total, to_paths = costs[to]
if to_total > total + weight:
costs[to] = (total + weight, [lst + [to] for lst in paths])
heappush(que, to)
elif to_total == total + weight:
costs[to] = (to_total, to_paths + [lst + [to] for lst in paths])
_, atob = costs[b]
from_to = [set() for _ in range(s)]
for path in atob:
for i, p in enumerate(path):
from_to[p] = from_to[p] | set(path[i + 1:])
for _ in range(q):
start, goal = map(int, input().split())
start -= 1
goal -= 1
if goal in from_to[start]:
print("Yes")
else:
print("No")
main()
``` | instruction | 0 | 77,510 | 1 | 155,020 |
No | output | 1 | 77,510 | 1 | 155,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are n stations on the line, a_i people are waiting for the train at the i-th station at the beginning of the game. The game starts at the beginning of the 0-th hour. At the end of each hour (couple minutes before the end of the hour), b_i people instantly arrive to the i-th station. If at some moment, the number of people at the i-th station is larger than c_i, you lose.
A player has several trains which he can appoint to some hours. The capacity of each train is k passengers. In the middle of the appointed hour, the train goes from the 1-st to the n-th station, taking as many people at each station as it can accommodate. A train can not take people from the i-th station if there are people at the i-1-th station.
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
The player wants to stay in the game for t hours. Determine the minimum number of trains he will need for it.
Input
The first line contains three integers n, t, and k (1 β€ n, t β€ 200, 1 β€ k β€ 10^9) β the number of stations on the line, hours we want to survive, and capacity of each train respectively.
Each of the next n lines contains three integers a_i, b_i, and c_i (0 β€ a_i, b_i β€ c_i β€ 10^9) β number of people at the i-th station in the beginning of the game, number of people arriving to i-th station in the end of each hour and maximum number of people at the i-th station allowed respectively.
Output
Output a single integer number β the answer to the problem.
Examples
Input
3 3 10
2 4 10
3 3 9
4 2 8
Output
2
Input
4 10 5
1 1 1
1 0 1
0 5 8
2 7 100
Output
12
Note
<image>
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains.
Submitted Solution:
```
pass
``` | instruction | 0 | 77,552 | 1 | 155,104 |
No | output | 1 | 77,552 | 1 | 155,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are n stations on the line, a_i people are waiting for the train at the i-th station at the beginning of the game. The game starts at the beginning of the 0-th hour. At the end of each hour (couple minutes before the end of the hour), b_i people instantly arrive to the i-th station. If at some moment, the number of people at the i-th station is larger than c_i, you lose.
A player has several trains which he can appoint to some hours. The capacity of each train is k passengers. In the middle of the appointed hour, the train goes from the 1-st to the n-th station, taking as many people at each station as it can accommodate. A train can not take people from the i-th station if there are people at the i-1-th station.
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
The player wants to stay in the game for t hours. Determine the minimum number of trains he will need for it.
Input
The first line contains three integers n, t, and k (1 β€ n, t β€ 200, 1 β€ k β€ 10^9) β the number of stations on the line, hours we want to survive, and capacity of each train respectively.
Each of the next n lines contains three integers a_i, b_i, and c_i (0 β€ a_i, b_i β€ c_i β€ 10^9) β number of people at the i-th station in the beginning of the game, number of people arriving to i-th station in the end of each hour and maximum number of people at the i-th station allowed respectively.
Output
Output a single integer number β the answer to the problem.
Examples
Input
3 3 10
2 4 10
3 3 9
4 2 8
Output
2
Input
4 10 5
1 1 1
1 0 1
0 5 8
2 7 100
Output
12
Note
<image>
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains.
Submitted Solution:
```
import time
def get_input_list():
return list(map(int, input().split()))
def takes_people(d,k):
for i in range(len(d)):
if d[i] < k:
k -=d[i]
d[i] = 0
else:
d[i] -= k
break
return d
def zeros(n):
return [0 for i in range(n)]
def sum_list(a,b):
s = a
for i in range(len(b)):
s[i] += b[i]
return s
def mul_list(a,n):
return [i*n for i in a]
def check(a,b,c):
for i in range(len(a)):
if a[i] + b[i] > c[i]:
return False
return True
n,t,k = get_input_list()
a = []
b = []
c = []
for _ in range(n):
ai,bi,ci = get_input_list()
a.append(ai)
b.append(bi)
c.append(ci)
d = zeros(n) # num of people are waiting
d = sum_list(d,a)
result = 0 # num of train appointed
for _ in range(t):
i = 0
while check(d,b,c) != True:
i = i + 1
d = takes_people(d, k*i)
print(_,i)
d = sum_list(d,b)
result = result + i
print(result)
``` | instruction | 0 | 77,553 | 1 | 155,106 |
No | output | 1 | 77,553 | 1 | 155,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are n stations on the line, a_i people are waiting for the train at the i-th station at the beginning of the game. The game starts at the beginning of the 0-th hour. At the end of each hour (couple minutes before the end of the hour), b_i people instantly arrive to the i-th station. If at some moment, the number of people at the i-th station is larger than c_i, you lose.
A player has several trains which he can appoint to some hours. The capacity of each train is k passengers. In the middle of the appointed hour, the train goes from the 1-st to the n-th station, taking as many people at each station as it can accommodate. A train can not take people from the i-th station if there are people at the i-1-th station.
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
The player wants to stay in the game for t hours. Determine the minimum number of trains he will need for it.
Input
The first line contains three integers n, t, and k (1 β€ n, t β€ 200, 1 β€ k β€ 10^9) β the number of stations on the line, hours we want to survive, and capacity of each train respectively.
Each of the next n lines contains three integers a_i, b_i, and c_i (0 β€ a_i, b_i β€ c_i β€ 10^9) β number of people at the i-th station in the beginning of the game, number of people arriving to i-th station in the end of each hour and maximum number of people at the i-th station allowed respectively.
Output
Output a single integer number β the answer to the problem.
Examples
Input
3 3 10
2 4 10
3 3 9
4 2 8
Output
2
Input
4 10 5
1 1 1
1 0 1
0 5 8
2 7 100
Output
12
Note
<image>
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains.
Submitted Solution:
```
import time
def get_input_list():
return list(map(int, input().split()))
def takes_people(d,k):
for i in range(len(d)):
if d[i] < k:
k -=d[i]
d[i] = 0
else:
d[i] -= k
break
return d
def zeros(n):
return [0 for i in range(n)]
def sum_list(a,b):
s = a
for i in range(len(b)):
s[i] += b[i]
return s
def mul_list(a,n):
return [i*n for i in a]
def check(a,b,c):
for i in range(len(a)):
if a[i] + b[i] > c[i]:
return False
return True
n,t,k = get_input_list()
a = []
b = []
c = []
for _ in range(n):
ai,bi,ci = get_input_list()
a.append(ai)
b.append(bi)
c.append(ci)
d = zeros(n) # num of people are waiting
d = sum_list(d,a)
result = 0 # num of train appointed
for _ in range(t):
i = 0
while check(d,b,c) != True:
i = i + 1
d = takes_people(d, k)
d = sum_list(d,b)
result = result + i
print(result)
``` | instruction | 0 | 77,554 | 1 | 155,108 |
No | output | 1 | 77,554 | 1 | 155,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Land of Fire there are n villages and n-1 bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (so it becomes a stone road if it was a sand road and vice versa). Also everyone here loves ramen, that's why every morning a ramen pavilion is set in the middle of every stone road, and at the end of each day all the pavilions are removed.
For each of the following m days, after another road is flipped, Naruto and Jiraiya choose a simple path β that is, a route which starts in a village and ends in a (possibly, the same) village, and doesn't contain any road twice. Since Naruto and Jiraiya also love ramen very much, they buy a single cup of ramen on each stone road and one of them eats it. Since they don't want to offend each other, they only choose routes where they can eat equal number of ramen cups. Since they both like traveling, they choose any longest possible path. After every renovation find the maximal possible length of a path (that is, the number of roads in it) they can follow.
Input
The first line contains the only positive integer n (2 β€ n β€ 500 000) standing for the number of villages in the Land of Fire.
Each of the following (n-1) lines contains a description of another road, represented as three positive integers u, v and t (1 β€ u, v β€ n, t β \{0,1\}). The first two numbers denote the villages connected by the road, and the third denotes the initial type of the road: 0 for the sand one and 1 for the stone one. Roads are numbered from 1 to (n-1) in the order from the input.
The following line contains a positive integer m (1 β€ m β€ 500 000) standing for the number of days Naruto and Jiraiya travel for.
Each of the following m lines contains the single integer id (1 β€ id β€ n-1) standing for the index of the road whose type is flipped on the morning of corresponding day.
It is guaranteed that there is a road path between any pair of villages.
Output
Output m lines. In the i-th of them print the only integer denoting the maximal possible length of any valid path on the i-th day.
Example
Input
5
1 2 0
1 3 0
3 5 0
3 4 0
5
3
4
1
3
4
Output
3
2
3
3
2
Note
After the renovation of the 3-rd road the longest path consists of the roads 1, 2 and 4.
After the renovation of the 4-th road one of the longest paths consists of the roads 1 and 2.
After the renovation of the 1-st road one of the longest paths consists of the roads 1, 2 and 3.
After the renovation of the 3-rd road the longest path consists of the roads 1, 2 and 4.
After the renovation of the 4-rd road one of the longest paths consists of the roads 2 and 4.
Submitted Solution:
```
1
``` | instruction | 0 | 77,734 | 1 | 155,468 |
No | output | 1 | 77,734 | 1 | 155,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest.
The track's map is represented by a rectangle n Γ m in size divided into squares. Each square is marked with a lowercase Latin letter (which means the type of the plot), with the exception of the starting square (it is marked with a capital Latin letters S) and the terminating square (it is marked with a capital Latin letter T). The time of movement from one square to another is equal to 1 minute. The time of movement within the cell can be neglected. We can move from the cell only to side-adjacent ones, but it is forbidden to go beyond the map edges. Also the following restriction is imposed on the path: it is not allowed to visit more than k different types of squares (squares of one type can be visited an infinite number of times). Squares marked with S and T have no type, so they are not counted. But S must be visited exactly once β at the very beginning, and T must be visited exactly once β at the very end.
Your task is to find the path from the square S to the square T that takes minimum time. Among all shortest paths you should choose the lexicographically minimal one. When comparing paths you should lexicographically represent them as a sequence of characters, that is, of plot types.
Input
The first input line contains three integers n, m and k (1 β€ n, m β€ 50, nΒ·m β₯ 2, 1 β€ k β€ 4). Then n lines contain the map. Each line has the length of exactly m characters and consists of lowercase Latin letters and characters S and T. It is guaranteed that the map contains exactly one character S and exactly one character T.
Pretest 12 is one of the maximal tests for this problem.
Output
If there is a path that satisfies the condition, print it as a sequence of letters β the plot types. Otherwise, print "-1" (without quotes). You shouldn't print the character S in the beginning and T in the end.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
Examples
Input
5 3 2
Sba
ccc
aac
ccc
abT
Output
bcccc
Input
3 4 1
Sxyy
yxxx
yyyT
Output
xxxx
Input
1 3 3
TyS
Output
y
Input
1 4 1
SxyT
Output
-1 | instruction | 0 | 77,945 | 1 | 155,890 |
Tags: graphs, greedy, shortest paths
Correct Solution:
```
import sys
from array import array # noqa: F401
from itertools import combinations
from collections import deque
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
chars = (
['}' * (m + 2)]
+ ['}' + ''.join('{' if c == 'S' else '|' if c == 'T' else c for c in input().rstrip()) + '}' for _ in range(n)]
+ ['}' * (m + 2)]
)
cbit = [[1 << (ord(c) - 97) for c in chars[i]] for i in range(n + 2)]
si, sj, ti, tj = 0, 0, 0, 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if chars[i][j] == '{':
si, sj = i, j
cbit[i][j] = 0
if chars[i][j] == '|':
ti, tj = i, j
ans = inf = '*' * (n * m)
for comb in combinations([1 << i for i in range(26)], r=k):
enabled = sum(comb)
dp = [[inf] * (m + 2) for _ in range(n + 2)]
dp[ti][tj] = ''
dq = deque([(ti, tj, '')])
while dq:
i, j, s = dq.popleft()
if dp[i][j] < s:
continue
for di, dj in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
if (cbit[di][dj] & enabled) != cbit[di][dj]:
continue
pre = chars[di][dj] if cbit[di][dj] else ''
l = 1 if cbit[di][dj] else 0
if (len(dp[di][dj]) > len(s) + l or len(dp[di][dj]) == len(s) + l and dp[di][dj] > pre + s):
dp[di][dj] = pre + s
if l:
dq.append((di, dj, pre + s))
if len(ans) > len(dp[si][sj]) or len(ans) == len(dp[si][sj]) and ans > dp[si][sj]:
ans = dp[si][sj]
print(ans if ans != inf else -1)
``` | output | 1 | 77,945 | 1 | 155,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland has n cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.
The president of Berland decided to split country into r states in such a way that each city will belong to exactly one of these r states.
After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads.
The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it.
Please help president to find smallest possible r for which such a split exists.
Input
The input contains one or several test cases. The first input line contains a single integer number t β number of test cases. Then, t test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.
Then t blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers n, m (1 β€ n β€ 2000, 0 β€ m β€ 10000) β the number of cities and number of roads in the Berland. Each of the next m lines contains two space-separated integers β x_i, y_i (1 β€ x_i, y_i β€ n; x_i β y_i), which denotes that the i-th road connects cities x_i and y_i. Each pair of cities are connected by at most one road.
Sum of values n across all test cases doesn't exceed 2000. Sum of values m across all test cases doesn't exceed 10000.
Output
For each test case first print a line containing a single integer r β smallest possible number of states for which required split is possible. In the next line print n space-separated integers in range from 1 to r, inclusive, where the j-th number denotes number of state for the j-th city. If there are multiple solutions, print any.
Example
Input
2
Β
5 3
1 2
2 5
1 5
Β
6 5
1 2
2 3
3 4
4 2
4 1
Output
1
1 1 1 1 1
2
2 1 1 1 1 1
Submitted Solution:
```
v=int(input())
for i in range(0,v,1):
input("\n")
p=1
i=0
n,k=map(int, input().split())
r=1
b=[]
sum=0
a=[1 for i in range(0,n)]
b = [[0 for c in range(0,n,1)] for d in range(0,n,1)]
for i in range(0,k,1):
w,q=map(int, input().split())
b[w-1][q-1]+=1
b[q-1][w-1]+=1
#computation
for i in range(0,n):
for j in range(0,n):
if b[i][j]==1:
sum+=1
if sum%2==1:
for j in range(0,n):
if b[i][j]==1:
b[i][j]=0
b[j][i]=0
r+=1
if r>p:
p=r
a[j]=r
sum-=1
break
sum=0
r=1
print(p)
for i in range(0,n):
print (a[i], end=" ")
``` | instruction | 0 | 78,303 | 1 | 156,606 |
No | output | 1 | 78,303 | 1 | 156,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland has n cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.
The president of Berland decided to split country into r states in such a way that each city will belong to exactly one of these r states.
After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads.
The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it.
Please help president to find smallest possible r for which such a split exists.
Input
The input contains one or several test cases. The first input line contains a single integer number t β number of test cases. Then, t test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.
Then t blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers n, m (1 β€ n β€ 2000, 0 β€ m β€ 10000) β the number of cities and number of roads in the Berland. Each of the next m lines contains two space-separated integers β x_i, y_i (1 β€ x_i, y_i β€ n; x_i β y_i), which denotes that the i-th road connects cities x_i and y_i. Each pair of cities are connected by at most one road.
Sum of values n across all test cases doesn't exceed 2000. Sum of values m across all test cases doesn't exceed 10000.
Output
For each test case first print a line containing a single integer r β smallest possible number of states for which required split is possible. In the next line print n space-separated integers in range from 1 to r, inclusive, where the j-th number denotes number of state for the j-th city. If there are multiple solutions, print any.
Example
Input
2
Β
5 3
1 2
2 5
1 5
Β
6 5
1 2
2 3
3 4
4 2
4 1
Output
1
1 1 1 1 1
2
2 1 1 1 1 1
Submitted Solution:
```
v = int(input())
for c in range(0, v):
f=input("\n")
p = 1
n, k = map(int, input().split())
r = 1
w = 0
q = 0
a = [1 for i in range(0, n)]
b = [[0 for i in range(0, n)] for j in range(0, n)]
z = [0 for i in range(0, n)]
d = [0 for i in range(0, n)]
x = [1 for i in range(0, n)]
for i in range(0, k):
w, q = map(int, input().split())
b[w - 1][q - 1] += 1
b[q - 1][w - 1] += 1
z[w-1] += 1
z[q-1] += 1
d[w- 1] += 1
d[q - 1] += 1
for i in range(0, n):
for j in range(0, n):
if z[j] % 2 == 1 and b[i][j] == 1:
b[i][j] = 0
b[j][i] = 0
z[i] -= 1
z[j] -= 1
t = 0
r += 1
x[i] += 1
p = r
for t in range(n):
if b[i][t] == 1 and z[t]%2==1:
b[i][t] = 0
b[t][i] = 0
z[i] -= 1
z[t] -= 1
p = r
print(p)
for i in range(0, n):
if z[i]==d[i]:
print(1+j, end=" ")
else:
j+=1
print(1 + j, end=" ")
``` | instruction | 0 | 78,304 | 1 | 156,608 |
No | output | 1 | 78,304 | 1 | 156,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland has n cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.
The president of Berland decided to split country into r states in such a way that each city will belong to exactly one of these r states.
After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads.
The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it.
Please help president to find smallest possible r for which such a split exists.
Input
The input contains one or several test cases. The first input line contains a single integer number t β number of test cases. Then, t test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.
Then t blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers n, m (1 β€ n β€ 2000, 0 β€ m β€ 10000) β the number of cities and number of roads in the Berland. Each of the next m lines contains two space-separated integers β x_i, y_i (1 β€ x_i, y_i β€ n; x_i β y_i), which denotes that the i-th road connects cities x_i and y_i. Each pair of cities are connected by at most one road.
Sum of values n across all test cases doesn't exceed 2000. Sum of values m across all test cases doesn't exceed 10000.
Output
For each test case first print a line containing a single integer r β smallest possible number of states for which required split is possible. In the next line print n space-separated integers in range from 1 to r, inclusive, where the j-th number denotes number of state for the j-th city. If there are multiple solutions, print any.
Example
Input
2
Β
5 3
1 2
2 5
1 5
Β
6 5
1 2
2 3
3 4
4 2
4 1
Output
1
1 1 1 1 1
2
2 1 1 1 1 1
Submitted Solution:
```
v=int(input())
for c in range(0,v):
f=input("\n")
p=1
n,k=map(int, input().split())
r=1
w=0
q=0
sum=0
a=[1 for i in range(0,n)]
b = [[0 for i in range(0,n)] for j in range(0,n)]
for i in range(0,k):
w, q = map(int, input().split())
b[w-1][q-1]+=1
b[q-1][w-1]+=1
for i in range(0,n):
for j in range(0,n):
if b[i][j]==1:
sum+=1
if sum%2==1:
t=0
while t<n:
if b[i][t]==1:
b[i][t]=0
b[t][i]=0
r+=1
if r>p:
p=r
if a[i]==1:
a[t]=r
sum-=1
break
else:t+=1
sum=0
r=1
print(p)
for i in range(0,n):
print (a[i], end=" ")
``` | instruction | 0 | 78,305 | 1 | 156,610 |
No | output | 1 | 78,305 | 1 | 156,611 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.