message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
from operator import attrgetter, itemgetter
n = int(input());
arr = [[int(x) for x in input().split(' ')] for y in range(n)];
arr = sorted(arr, key=itemgetter(1,0,2), reverse=True);
dp = [0 for x in range(n)];
s = [];
for i in range(n):
while (s != [] and arr[s[-1]][0] >= arr[i][1]):
s.pop();
if (s != []):
dp[i] = dp[s[-1]];
dp[i] += arr[i][2];
s.append(i);
print(max(dp));
``` | instruction | 0 | 49,345 | 8 | 98,690 |
Yes | output | 1 | 49,345 | 8 | 98,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
n=int(input())
j=[]
for i in range(n):
ch=input()
l=ch.split(' ')
j.append(l)
i=n-1
k=[]
while i>=0:
test=True
s=int(j[i][2])
i=i-1
while (test==True)and(i>=0):
if int(j[i][1])>int(j[i+1][0]):
s=s+int(j[i][2])
else:
test=False
i=i-1
k.append(s)
if test==False:
i=i+1
print(max(k))
``` | instruction | 0 | 49,346 | 8 | 98,692 |
No | output | 1 | 49,346 | 8 | 98,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
n=int(input())
j=[]
for i in range(n):
ch=input()
l=ch.split(' ')
j.append(l)
i=0
k=[]
for i in range(n):
k.append(int(j[i][1]))
i=0
while k!=sorted(k):
i=0
while i<n-1:
if k[i]>k[i+1]:
k[i],k[i+1]=k[i+1],k[i]
j[i],j[i+1]=j[i+1],j[i]
if k[i]==k[i+1]:
if j[i][1] <= j[i+1][0]:
j[i],j[i+1]=j[i+1],j[i]
i=i+1
i=n-1
k=[]
while i>=0:
s=int(j[i][2])
h=i-1
while h>=0:
if int(j[i][0])<int(j[h][1]):
s=s+int(j[h][2])
i=h
h=h-1
i=i-1
k.append(s)
print(max(k))
``` | instruction | 0 | 49,347 | 8 | 98,694 |
No | output | 1 | 49,347 | 8 | 98,695 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
import operator
n = int(input())
arr=[]
for i in range(n):
arr.append([int(element) for element in input().split()])
arr = sorted(arr, key=operator.itemgetter(1,0,2), reverse= True)
heigh = [0 for i in range(n)]
s = []
for i in range(n):
if len(s) > 0 and arr[s[-1]][0] >= arr[i][1] :
s.clear()
if len(s) > 0 :
heigh[i] = heigh[s[-1]]
heigh[i] += arr[i][2]
s.append(i)
print(max(heigh))
``` | instruction | 0 | 49,348 | 8 | 98,696 |
No | output | 1 | 49,348 | 8 | 98,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
n=int(input())
j=[]
for i in range(n):
ch=input()
l=ch.split(' ')
j.append(l)
i=n-1
k=[]
while i>=0:
test=True
s=int(j[i][2])
i=i-1
while (test==True)and(i>=0):
if (int(j[i][1])>int(j[i+1][0]))and(int(j[i][1])<=int(j[i+1][1])):
s=s+int(j[i][2])
else:
test=False
i=i-1
k.append(s)
if test==False:
i=i+1
print(max(k))
``` | instruction | 0 | 49,349 | 8 | 98,698 |
No | output | 1 | 49,349 | 8 | 98,699 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
* Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.
* Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
* The total height of all rings used should be maximum possible.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.
The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
Examples
Input
3
1 5 1
2 6 2
3 7 3
Output
6
Input
4
1 2 1
1 3 3
4 6 2
5 7 1
Output
4
Note
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.
In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
Submitted Solution:
```
""" Python 3 compatibility tools. """
from __future__ import division, print_function
import sys
import os
if False:
from typing import List, Set, Dict, Tuple, Text, Optional, Callable, Any
from collections import deque
import collections
from types import GeneratorType
import itertools
import operator
import functools
import random
import copy
import heapq
import math
from atexit import register
from io import BytesIO, IOBase
import __pypy__ # type: ignore
EPS = 10**-12
#########
# INPUT #
#########
class Input(object):
def __init__(self):
if 'CPH' not in os.environ:
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
def rawInput(self):
# type: () -> str
return sys.stdin.readline().rstrip('\r\n')
def readInt(self):
return int(self.rawInput())
##########
# OUTPUT #
##########
class Output(object):
def __init__(self):
self.out = __pypy__.builders.StringBuilder()
def write(self, text):
# type: (str) -> None
self.out.append(str(text))
def writeLine(self, text):
# type: (str) -> None
self.write(str(text) + '\n')
def finalize(self):
if sys.version_info[0] < 3:
os.write(1, self.out.build())
else:
os.write(1, self.out.build().encode())
###########
# LIBRARY #
###########
def bootstrap(f, stack=[]):
# Deep Recursion helper.
# From: https://github.com/cheran-senthil/PyRival/blob/c1972da95d102d95b9fea7c5c8e0474d61a54378/docs/bootstrap.rst
# Usage:
# @bootstrap
# def recur(n):
# if n == 0:
# yield 1
# yield (yield recur(n-1)) * n
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
int_add = __pypy__.intop.int_add
int_sub = __pypy__.intop.int_sub
int_mul = __pypy__.intop.int_mul
def make_mod_mul(mod):
fmod_inv = 1.0 / mod
def mod_mul(a, b, c=0):
res = int_sub(int_add(int_mul(a, b), c), int_mul(
mod, int(fmod_inv * a * b + fmod_inv * c)))
if res >= mod:
return res - mod
elif res < 0:
return res + mod
else:
return res
return mod_mul
class FenwickTreeMax:
'''
>>> x = FenwickTreeMax([1, 2, 3, 2, 1])
>>> x.query(0)
1
>>> x.query(2)
3
>>> x.query(4)
3
>>> x.set(2, 5)
>>> x.query(1)
2
>>> x.query(2)
5
>>> x.query(3)
5
>>> x.set(1, 10)
>>> x.set(3, 13)
>>> x.query(4)
13
>>> x.query(2)
10
'''
def __init__(self, data):
"""transform list into BIT"""
self._bit = copy.deepcopy(data)
self._n = n = len(data)
for i in range(n):
j = i | (i + 1)
if j < n:
self._bit[j] = max(self._bit[j], self._bit[i])
def set(self, idx, value):
"""updates bit[idx] = value"""
assert 0 <= idx < self._n
while idx < self._n:
self._bit[idx] = max(self._bit[idx], value)
idx |= idx + 1
def query(self, end):
"""calc max(bit[:end])"""
assert 0 <= end < self._n
end += 1
x = self._bit[end - 1]
end &= end - 1
while end:
x = max(x, self._bit[end - 1])
end &= end - 1
return x
def Normalize(arr):
'''
>>> Normalize([5, 1, 3, 5, 3, 1, 4])
[3, 0, 1, 3, 1, 0, 2]
>>> Normalize([])
[]
>>> Normalize([1.0, 3.0])
[0, 1]
>>> Normalize([1, 2, 3])
[0, 1, 2]
>>> Normalize([-1, -2, -3])
[2, 1, 0]
'''
# transform arbitrary valued array into array of int, maintaining relative order.
sorted_arr = map(
operator.itemgetter(0),
itertools.groupby(sorted(arr))
)
result = [0] * len(arr)
for i in range(len(arr)):
ele = arr[i]
low = 0
high = len(sorted_arr)
while low < high:
mid = low + (high - low) // 2
if sorted_arr[mid] > ele:
high = mid
else:
low = mid + 1
result[i] = low - 1
return result
#########
# LOGIC #
#########
def main(inp, out):
# type: (Input, Output) -> None
n = inp.readInt()
tabi = []
heights = []
for i in range(n):
u, v, w = map(int, inp.rawInput().split())
tabi.append(u)
tabi.append(v)
heights.append(w)
tabi = Normalize(tabi)
towers = []
for i in range(n):
towers.append((tabi[2*i], tabi[2*i+1], heights[i]))
towers.sort()
towers.reverse()
z = len(tabi)+2
cv = [0] * z
fenwick_tree = FenwickTreeMax(cv)
for lb, ub, height in towers:
# best = maximum of [lb+1..]
ulimit = ub - 1
best = fenwick_tree.query(ulimit) + height
if best > cv[lb]:
cv[lb] = best
fenwick_tree.set(lb, best)
out.writeLine(fenwick_tree.query(z-1))
###############
# BOILERPLATE #
###############
output_obj = Output()
main(Input(), output_obj)
output_obj.finalize()
``` | instruction | 0 | 49,350 | 8 | 98,700 |
No | output | 1 | 49,350 | 8 | 98,701 |
Provide a correct Python 3 solution for this coding contest problem.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4 | instruction | 0 | 49,577 | 8 | 99,154 |
"Correct Solution:
```
from collections import deque
while 1:
n, m = map(int, input().split())
if n == m == 0:
break
*S, = map(int, input().split())
D = sorted(map(int, input().split()))
SA = sum(S)
rest = sum(d-SA for d in D if SA <= d)
memo = {2**n-1: rest}
def dfs(state, su, idx):
if state in memo:
return memo[state]
res = 10**18
for i in range(n):
if (state >> i) & 1 == 0:
s = 0; j = idx
nxt = su + S[i]
while j < m and D[j] <= nxt:
s += min(nxt - D[j], D[j] - su); j += 1
res = min(res, s + dfs(state | (1 << i), su + S[i], j))
memo[state] = res
return res
print(dfs(0, 0, 0))
``` | output | 1 | 49,577 | 8 | 99,155 |
Provide a correct Python 3 solution for this coding contest problem.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4 | instruction | 0 | 49,578 | 8 | 99,156 |
"Correct Solution:
```
from bisect import bisect_right as br
def main():
while True:
n, m = map(int, input().split())
if n == 0:break
ofuton = list(map(int, input().split()))
dlst = sorted(map(int, input().split()))
INF = 10 ** 20
end = 2 ** n - 1
dic = {}
def min_score(stat, power, index):
if stat in dic:return dic[stat]
if stat == end:return 0
mask = 1
ret = INF
for i in range(n):
if mask & stat:
mask <<= 1
continue
new_power = power + ofuton[i]
new_stat = stat | mask
new_index = br(dlst, new_power) - 1
add = (m - new_index - 1) * ofuton[i]
for j in range(index + 1, new_index + 1):
if new_power - dlst[j] < dlst[j] - power:
add += (dlst[j] - power) - (new_power - dlst[j])
ret = min(ret, -add + min_score(new_stat, new_power, new_index))
mask <<= 1
dic[stat] = ret
return ret
print(sum(dlst) + min_score(0, 0, -1))
main()
``` | output | 1 | 49,578 | 8 | 99,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4
Submitted Solution:
```
while 1:
n, m = map(int, input().split())
if n == m == 0:
break
*S, = map(int, input().split())
*D, = map(int, input().split())
from itertools import permutations
ans = 10**18
for P in permutations(S):
Q = [0]*(n+1)
for i, e in enumerate(P):
Q[i+1] = P[i] + Q[i]
ans = min(ans, sum(min(abs(d - q) for q in Q) for d in D))
print(ans)
``` | instruction | 0 | 49,579 | 8 | 99,158 |
No | output | 1 | 49,579 | 8 | 99,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4
Submitted Solution:
```
import sys
sys.setrecursionlimit(10000000)
input=lambda : sys.stdin.readline().rstrip()
while True:
n,m=map(int,input().split())
if n==m==0:
break
s=list(map(int,input().split()))
d=[0]+list(sorted(map(int,input().split())))
dp=[[9999999999 if i>0 else 0 for j in range(2**n)] for i in range(m+1)]
for i in range(m):
dp[i+1][0]=dp[i][0]+d[i+1]
for j in range(2**n):
tmp=0
for k in range(n):
if 1<<k & j:
tmp+=s[k]
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+abs(d[i+1]-tmp))
for k in range(n):
if not (1<<k & j):
dp[i+1][j|1<<k]=min(dp[i][j]+abs(d[i+1]-(tmp+s[k])),dp[i+1][j|1<<k])
print(min(dp[m]))
``` | instruction | 0 | 49,580 | 8 | 99,160 |
No | output | 1 | 49,580 | 8 | 99,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4
Submitted Solution:
```
from itertools import permutations
while 1:
n, m = map(int, input().split())
if n == m == 0:
break
*S, = map(int, input().split())
*D, = map(int, input().split())
def solve(S, D):
for P in permutations(S):
Q = [0]*(n+1)
for i, e in enumerate(P):
Q[i+1] = P[i] + Q[i]
yield sum(min(abs(d - q) for q in Q) for d in D)
print(min(solve(S, D)))
``` | instruction | 0 | 49,581 | 8 | 99,162 |
No | output | 1 | 49,581 | 8 | 99,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
My futon
You bought N futons in preparation for your new life. The i-th futon has the warmth supply capacity of si. From the temperature forecast for the next M days, the warmth demand of dj is expected on the jth day. If the warmth is not enough or too much, the comfort will be impaired, so the absolute value of the difference between the sum of the warmth supply capacity of the futon on the j day and the dj is called the discomfort on the j day. To do. I want to make the total discomfort for M days as small as possible.
By the way, your room is unfortunately very small, with only a bed and a closet. Therefore, the only way to add one futon to the bed is to put the futon at the top of the closet on the top of the bed. Conversely, the only way to reduce one bed futon is to put the futon at the top of the bed at the top of the closet. There is no limit to the number of futons that can be moved per day, but only one can be moved at a time.
By the way, you plan to put the futon you bought in the closet. Only at this time can the futons be put in the closet in any order. How do you store your futon in the closet, and then how do you put it in and out every day so that you can spend your days comfortably? When minimizing the sum of discomfort for M days, find the value of that sum. There may be futons that have never been used, and there may be days when no futons are used.
Input
The input consists of multiple datasets. Each dataset has the following format.
> N M
> s1 s2 ... sN
> d1 d2 ... dM
The first line of the dataset is given an integer representing the number of futons N and the number of days M for which the temperature is predicted, separated by spaces. On the second line, N integers s1, s2, ..., sN are given separated by spaces, and si represents the warmth supply capacity of the i-th futon. On the third line, M integers d1, d2, ..., dM are given separated by spaces, and dj represents the warmth demand on day j. These integers satisfy 1 ≤ N ≤ 15, 1 ≤ M ≤ 100, 1 ≤ si, dj ≤ 1,000,000.
The end of the input is represented by a dataset of N = M = 0. Do not output for this dataset.
Output
For each dataset, output the minimum sum of discomfort for M days on one line.
Sample Input
1 1
Five
6
1 1
Five
2
1 1
20
Five
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
twenty five
twenty five
2 5 2 5 2
0 0
Output for Sample Input
1
2
Five
1
1
Five
Four
For the 5th case, put it in the closet with 5, 2, 3, 1 from the top, and take out 3 sheets on the 1st day | 10-(5 + 2 + 3) | = 0, 2 sheets on the 2nd day | 4 -5 | = Take out one sheet on the 1st and 3rd days | 7-(5 + 2) | = 0, for a total of 0 + 1 + 0 = 1.
Example
Input
1 1
5
6
1 1
5
2
1 1
20
5
4 1
2 4 5 9
8
4 3
3 5 2 1
10 4 7
5 5
2 2 2 2 2
1 3 5 7 9
2 5
2 5
2 5 2 5 2
0 0
Output
1
2
5
1
1
5
4
Submitted Solution:
```
from itertools import permutations
while 1:
n, m = map(int, input().split())
if n == m == 0:
break
*S, = map(int, input().split())
*D, = map(int, input().split())
def solve(S, D):
Q = [0]*(n+1)
for P in permutations(S):
for i, e in enumerate(P):
Q[i+1] = e + Q[i]
yield sum(min(abs(d - q) for q in Q) for d in D)
print(min(solve(S, D)))
``` | instruction | 0 | 49,582 | 8 | 99,164 |
No | output | 1 | 49,582 | 8 | 99,165 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,768 | 8 | 99,536 |
Tags: graphs, implementation
Correct Solution:
```
for t in range(int(input())):
n,m = map(int,input().split())
l = list(map(int,input().split()))
a = []
if m<n or n==2:
print(-1)
else:
print(2*sum(l))
for i in range(1,n):
print(i,i+1)
print(1,n)
``` | output | 1 | 49,768 | 8 | 99,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,769 | 8 | 99,538 |
Tags: graphs, implementation
Correct Solution:
```
import heapq
from copy import deepcopy
T = int(input())
for _ in range(T):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = deepcopy(a)
d = []
s = sum(a)
heapq.heapify(a)
if n == 2 or n > m:
print(-1)
else:
for i in range(n-1):
d.append([i+1, i+2])
d.append([1, n])
x = heapq.heappop(a)
y = heapq.heappop(a)
xi = b.index(x)
b[xi] = 10 ** 6
yi = b.index(y)
for i in range(m-n):
d.append([xi+1, yi+1])
print(s * 2 + (m - n) * (x + y))
for i in range(len(d)):
print(*d[i])
``` | output | 1 | 49,769 | 8 | 99,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,770 | 8 | 99,540 |
Tags: graphs, implementation
Correct Solution:
```
for t in range(int(input())):
n,m=map(int,input().split())
a=list(map(int,input().split()))
if n>m or n==2:
print(-1)
else:
print(sum(a)*2)
for i in range (0,m-1):
print(i+1,i+2)
print(n,1)
``` | output | 1 | 49,770 | 8 | 99,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,771 | 8 | 99,542 |
Tags: graphs, implementation
Correct Solution:
```
for i in range(int(input())):
a, b = map(int, input().split())
lst = list(map(int, input().split()))
if a == 2:
print(-1)
elif a >= 3:
if b < a:
print(-1)
continue
elif a == b:
print(2*sum(lst))
for c in range(1, a):
print(c, c+1)
print(a, 1)
``` | output | 1 | 49,771 | 8 | 99,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,772 | 8 | 99,544 |
Tags: graphs, implementation
Correct Solution:
```
R=lambda:map(int,input().split())
t,=R()
for _ in[0]*t:
n,m=R();r=range(1,n+1);a=*R(),;(x,i),(y,j)=sorted(zip(a,r))[:2]
if n<3or m<n:print(-1)
else:
print(2*sum(a)+(x+y)*(m-n),)
for k in r:print(k,k%n+1)
for _ in range(m-n):print(i,j)
``` | output | 1 | 49,772 | 8 | 99,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,773 | 8 | 99,546 |
Tags: graphs, implementation
Correct Solution:
```
def sol():
T = int(input())
for i in range(T):
n,m = map(int,input().split())
a = list(map(int, input().split()))
if n==2 or m<n :print('-1');continue
dict = {}
sum = 0
for j in range(n):
sum += a[j]
dict[j+1] = a[j]
d = sorted(dict.items(),key=lambda x:x[1])
ans = sum*2 + (m-n)*(d[0][1]+d[1][1])
print(ans)
for j in range(1,n):
print(j,' ',j+1)
print(n,' ',1)
for j in range(m-n):
print(d[0][0],' ',d[1][0])
if __name__ == '__main__':
sol()
``` | output | 1 | 49,773 | 8 | 99,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,774 | 8 | 99,548 |
Tags: graphs, implementation
Correct Solution:
```
for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
if m < n or n == 2:
print(-1)
continue
print(2*sum(a))
for i in range(1, n):
print(i, i+1)
print(n, 1)
``` | output | 1 | 49,774 | 8 | 99,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1 | instruction | 0 | 49,775 | 8 | 99,550 |
Tags: graphs, implementation
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource
# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")
def main():
t = I()
rr = []
for _ in range(t):
n,m = LI()
a = LI()
if n < 3 or m < n:
rr.append(-1)
else:
s = sum(a)
rr.append(s*2)
for i in range(n):
rr.append(JA([i+1,(i+1)%n+1], " "))
return JA(rr, "\n")
print(main())
``` | output | 1 | 49,775 | 8 | 99,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
for _ in range(int(input())):
n,m=map(int,input().split())
arr=list(map(int,input().split()))
if n>m or n==2:
print(-1)
else:
print(2*sum(arr))
for i in range(1,n):
print(i,i+1)
print(1,n)
``` | instruction | 0 | 49,776 | 8 | 99,552 |
Yes | output | 1 | 49,776 | 8 | 99,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
def small(arr):
freq={}
for i in range(len(arr)):
freq[arr[i]]=i
#print(freq)
arr=list(set(arr))
if(len(arr)==1 ):
return (0,1)
#print(arr)
arr.sort()
small=freq[arr[0]]
sec_small=freq[arr[1]]
return (small,sec_small)
def graph(arr,n,m):
empty=[]
a=1
b=a+1
temp=n
s=0
s=sum(arr)*2
while(temp>0):
#print(a,b)
if(a==n):
a=n
b=1
empty.append((a,b))
a=b
b=a+1
temp-=1
print(s)
for i in empty:
print(i[0],i[1])
t=int(input())
for i in range(t):
inp=input().split()
n=int(inp[0])
m=int(inp[1])
arr=list(map(int,input().split()))
if(m<=n-1 or n==2):
print(-1)
else:
graph(arr,n,m)
``` | instruction | 0 | 49,777 | 8 | 99,554 |
Yes | output | 1 | 49,777 | 8 | 99,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
t = int(input())
for _ in range(t):
n,m = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
if n == 2 or m<n:
print(-1)
continue
ans = 0
for i in range(1,n):
ans += (arr[i] + arr[i-1])
ans += (arr[0] + arr[n-1])
print(ans)
for i in range(1,n):
print(i,i+1)
print(n,1)
``` | instruction | 0 | 49,778 | 8 | 99,556 |
Yes | output | 1 | 49,778 | 8 | 99,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
k=int(input())
for i in range(k):
n,m=map(int,input().split())
ch=input()
s=ch.split(' ')
for i in range(n):
s[i]=int(s[i])
if (n==2) or (m<n):
print(-1)
else:
h=2*sum(s)
s1=sorted(s)
h+=(m-n)*(s1[0]+s1[1])
print(h)
for i in range(n-1):
print(i+1,i+2)
print('1',n)
if (n<m):
a=s.index(s1[0])+1
s[a-1]=h
b=s.index(s1[1])+1
for i in range(m-n):
print(a,b)
``` | instruction | 0 | 49,779 | 8 | 99,558 |
Yes | output | 1 | 49,779 | 8 | 99,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
for _ in [0]*int(input()):
n,m=map(int,input().split())
l=list(map(int,input().split()))
if m<n:print(-1)
else:
print(sum(l)*2)
for i in range(n):
print(i+1,(i+1)%n+1)
``` | instruction | 0 | 49,780 | 8 | 99,560 |
No | output | 1 | 49,780 | 8 | 99,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
def inp():
return map(int, input().split())
def arr_enu():
return [[i+1, int(x)] for i, x in enumerate(input().split())]
def get_col(arr, i):
return [row[i] for row in arr]
for i in range(int(input())):
n, m = inp()
a = arr_enu()
if (m < n):
print(-1)
else:
a.sort(key=lambda x:x[1])
print(sum(get_col(a,1)) * 2)
for j in range(n):
if (j == n - 1):
print(a[j][0], a[0][0])
else:
print(a[j][0], a[j + 1][0])
``` | instruction | 0 | 49,781 | 8 | 99,562 |
No | output | 1 | 49,781 | 8 | 99,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10**9 + 7
INF = 10**20
I = lambda:list(map(int,input().split()))
from collections import defaultdict as dd
from math import gcd
t, = I()
while t:
t -= 1
n, m = I()
a = I()
for i in range(n):
a[i] = [a[i], i]
a.sort()
if m < n or n <= 2:
print(-1)
continue
cost = 0
ans = []
for i in range(0, n, 3):
if i + 2 >= n:
if n%3 == 1:
cost += a[-1][0] + a[0][0]
cost += a[1][0] + a[-1][0]
ans.append([a[-1][1], a[0][1]])
ans.append([a[1][1], a[-1][1]])
elif n%3 == 2:
cost += 2*(a[i][0] + a[i+1][0] + a[0][0])
ans.append([a[i][1], a[i+1][1]])
ans.append([a[i+1][1], a[0][1]])
ans.append([a[i][1], a[0][1]])
break
# print(i+2)
cost += 2*(a[i][0] + a[i+1][0] + a[i+2][0])
ans.append([a[i][1], a[i+1][1]])
ans.append([a[i+1][1], a[i+2][1]])
ans.append([a[i][1], a[i+2][1]])
for j in range(len(ans), m):
cost += a[0][0] + a[1][0]
ans.append([a[0][1], a[1][1]])
print(cost)
for i in ans:
print(i[0]+1, i[1]+1)
``` | instruction | 0 | 49,782 | 8 | 99,564 |
No | output | 1 | 49,782 | 8 | 99,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.
n fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of n people can open it.
<image> For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1-4 and 1-2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a_1, a_2, …, a_n. To make a steel chain connecting fridges u and v, you have to pay a_u + a_v dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly m steel chains so that all fridges are private. A fridge is private if and only if, among n people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge i is not private if there exists the person j (i ≠ j) that the person j can open the fridge i.
For example, in the picture all the fridges are private. On the other hand, if there are n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly m chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 ≤ T ≤ 10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers n, m (2 ≤ n ≤ 1000, 1 ≤ m ≤ n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^4) — weights of all fridges.
Output
For each test case:
* If there is no solution, print a single integer -1.
* Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), meaning that the i-th steel chain connects fridges u_i and v_i. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
Submitted Solution:
```
#to sort the second element use w.sort(key = takeSecond)
# but u have to define takeSecond
def takeSecond(elem):
return elem[1]
t = int(input())
for i in range(t):
n, m = input().split(" ")
n = int(n)
m = int(m)
w = []
ii = 1
for j in input().split():
w.append((int(j),ii))
ii+=1
w.sort()
if n<3:
print(-1)
continue
elif n == 3 and m<3 :
print(-1)
continue
elif m < ((n-2)*2):
print(-1)
continue
smallest = w[0]
secondSmallest = w[1]
mini = 0
count = 0
for k in w[2:]:
count += 1
mini += int(smallest[0]) + int(secondSmallest[0]) + (2 * k[0])
if count == 1:
mini += smallest[0]+secondSmallest[0]
print(mini)
if n==3:
for jk in range(3):
print(smallest[1], " ", secondSmallest[1])
else:
for jk in range((n-2)*2):
print(smallest[1], " ", secondSmallest[1])
``` | instruction | 0 | 49,783 | 8 | 99,566 |
No | output | 1 | 49,783 | 8 | 99,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
Input
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Output
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
Example
Input
5
5 5 5 5 5
3
1 3 2
2 2 1
4 4 1
Output
2
4
5
Note
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
Submitted Solution:
```
def cin():
return list(map(int,input().split()))
n=int(input())
A=cin()
m=int(input())
for i in range(m):
l,r,d = cin()
A[l-1:r]=[i+d for i in A[l-1:r]]
ans=0
s=0
c1=False
for i in range(n-1):
if A[i]==A[i+1]:
ans=max(ans,s)
s=0
if (A[i]<A[i+1] and not c1) or (A[i]>A[i+1] and c1):
s+=1
elif A[i]>A[i+1] and not c1:
c1=True
s+=1
elif A[i]<A[i+1] and c1:
ans=max(ans,s)
s=0
if i==n-2:
ans = max(ans,s)
print(ans+1)
``` | instruction | 0 | 50,120 | 8 | 100,240 |
No | output | 1 | 50,120 | 8 | 100,241 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
Input
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Output
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
Example
Input
5
5 5 5 5 5
3
1 3 2
2 2 1
4 4 1
Output
2
4
5
Note
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
Submitted Solution:
```
n = int(input())
a = []
for sd in input().split(" "):
a.append(int(sd))
m = int(input())
l = []
r = []
d = []
for i in range(m):
lkd = input().split(" ")
#print(lkd)
l.append(int(lkd[0]))
r.append(int(lkd[1]))
d.append(int(lkd[2]))
def windth(d):
counter = 0;
start = True
peek = False
maxw = []
for i in range(len(d)):
if i == 0:
continue
if start and d[i-1] > d[i]:
peek = True
if start and peek and d[i-1] <= d[i]:
#print("Добавляем в список")
maxw.append(counter)
counter = 0
peek = False
if i == len(d)-1:
#print("Дошли до конца", counter)
counter = counter + 1;
maxw.append(counter)
break
if start and (d[i-1] < d[i]) or (peek and (d[i-1] > d[i])):
counter = counter + 1
#rint("Попал на увеличение", counter)
#print("список ", maxw)
return max(maxw) + 1
for i in range(m):
for ii in range(l[i]-1,r[i]):
a[ii] += d[i]
#print(a)
print(windth(a))
``` | instruction | 0 | 50,121 | 8 | 100,242 |
No | output | 1 | 50,121 | 8 | 100,243 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
Input
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Output
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
Example
Input
5
5 5 5 5 5
3
1 3 2
2 2 1
4 4 1
Output
2
4
5
Note
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
Submitted Solution:
```
def cin():
return list(map(int,input().split()))
n=int(input())
A=cin()
m=int(input())
for i in range(m):
l,r,d = cin()
A[l-1:r]=[i+d for i in A[l-1:r]]
ans=0
s=0
c1=False
for i in range(n-1):
if (A[i]<A[i+1] and not c1) or (A[i]>A[i+1] and c1):
s+=1
elif A[i]>A[i+1] and not c1:
c1=True
s+=1
elif A[i]<A[i+1] and c1:
ans=max(ans,s)
s=0
if i==n-2:
ans = max(ans,s)
print(ans+1)
``` | instruction | 0 | 50,122 | 8 | 100,244 |
No | output | 1 | 50,122 | 8 | 100,245 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
Input
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Output
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
Example
Input
5
5 5 5 5 5
3
1 3 2
2 2 1
4 4 1
Output
2
4
5
Note
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
Submitted Solution:
```
def printBigHill(s):
i=0
biggestHill=0
currHill = 0
climbing=getSlope(s[0],s[1])
while i<len(s):
if s[i]==0:
currHill=0
elif s[i]>0:
if climbing>0: currHill += 1
else: currHill = 1
else:
if climbing<0: currHill += 1
else: currHill = 1
i += 1
biggestHill = max(biggestHill,currHill)
print(biggestHill+1)
def getSlope(a,b):
if b>a: return 1
elif b<a: return -1
else: return 0
n= int(input())
a = list(map(lambda x:int(x),input().split()))
s = [0]*(n-1)
for i in range(0,len(a)-1):
s[i] = getSlope(a[i],a[i+1])
m= int(input())
for i in range(m):
l,r,d = map(lambda x:int(x),input().split())
for x in range(l-1,r): a[x] += d
if l>1: s[l-2] = getSlope(a[l-2],a[l-1])
if r<len(a): s[r-1] = getSlope(a[r-1],a[r])
printBigHill(s)
``` | instruction | 0 | 50,123 | 8 | 100,246 |
No | output | 1 | 50,123 | 8 | 100,247 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
import sys
import math
def count_day():
n, m = str(sys.stdin.readline()).split()
n, m = int(n), int(m)
if n <= m:
return n
min_k = math.floor(math.sqrt( 2*(n-m) )) - 1
max_k = 1 + math.ceil(math.sqrt( 2*(n-m) ))
for k in range(min_k, max_k):
if ( k*(k+1) >= 2*(n-m) ):
return m+k
if __name__ == '__main__':
day = count_day()
print(day)
``` | instruction | 0 | 50,148 | 8 | 100,296 |
Yes | output | 1 | 50,148 | 8 | 100,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
n, m = map(int, input().split())
if m >= n:
print(n)
exit(0)
l, r = 0, n
while l < r:
x = (l + r) // 2
if x * (x + 1) // 2 + m >= n:
r = x
else:
l = x + 1
print(m + l)
``` | instruction | 0 | 50,149 | 8 | 100,298 |
Yes | output | 1 | 50,149 | 8 | 100,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
def Zerno(n, x, m):
if m >= x:
return n - x
else:
return n - abs(((x - m - 1) * (x + m)) // 2) + m * abs((x - m - 1)) - x
n, m = map(int, input().split())
l = -1
r = 10 ** 18 + 1
while l + 1 < r:
x = (l + r) // 2
if Zerno(n, x, m) <= 0:
r = x
else:
l = x
print(r)
``` | instruction | 0 | 50,150 | 8 | 100,300 |
Yes | output | 1 | 50,150 | 8 | 100,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
""" Created by Henrikh Kantuni on 3/15/17 """
from decimal import *
if __name__ == "__main__":
n, m = [int(x) for x in input().split()]
if m >= n:
print(n)
else:
days = Decimal((-1 + Decimal(1 + 8 * (n - m)) ** Decimal(0.5)) / 2)
answer = days.to_integral_exact(rounding=ROUND_CEILING) + m
print(answer)
``` | instruction | 0 | 50,151 | 8 | 100,302 |
Yes | output | 1 | 50,151 | 8 | 100,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
from math import ceil
n,m = map(int,input().split())
if m >= n:
print(n)
else:
#num = 0 <= n2**2 + n2 - (n - m)*2
num2 = (n - m)*2
num2 = num2 * 4 + 1.0
num3 = num2
num2 = num2 ** 0.5
num = int((-1 + (num2))/2)
num -= 10
while True:
if (n - m)*2 - (num * (num+1)) <= 0:
break
num += 1
print(num + m)
``` | instruction | 0 | 50,152 | 8 | 100,304 |
No | output | 1 | 50,152 | 8 | 100,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
import math
n,m=map(int,input().split())
if(m>=n):
print(n)
else:
temp=min(n,m)
a=temp+1
res=temp
start=0
end=10**36
while(start<=end):
mid=(start+end)//2
if((mid/2)*(2*a+mid-1)>=n+(mid-1)*m):
end=mid-1
elif(((mid/2)*(2*a+mid-1)<n+(mid-1)*m)):
start=mid+1
print(start+m)
#res+=(1+2*m-2*a+math.sqrt((1+2*m-2*a)**2+8*(n-m)))/2
#print(math.ceil(res))
``` | instruction | 0 | 50,153 | 8 | 100,306 |
No | output | 1 | 50,153 | 8 | 100,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
__author__ = 'zihaozhu'
from sys import stdin
import math
def findx(x,y,startingday):
if (y*2)<(x+startingday) * (x-startingday+1):
return True
capacity,grain = map(int,stdin.readline().split())
# print(capacity,grain)
totalcap = capacity+grain
startingday = grain+1
# print(startingday)
# print(totalcap)
#days = math.floor(((totalcap * 2) + (startingday**2))**0.5)
days=0
start = startingday
lo = 0
hi =totalcap
while lo<hi:
mid = (lo+hi)//2
if findx(mid,totalcap,startingday):
hi = mid
else:
lo = mid +1
#print("mid",mid)
print(mid)
``` | instruction | 0 | 50,154 | 8 | 100,308 |
No | output | 1 | 50,154 | 8 | 100,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Submitted Solution:
```
n, m = map(int, input().split())
# NNNNYYYY
if m>=n:
print(1)
else:
low =1
high=1e18
while low<high:
mid = low + (int)((high-low)/2)
if mid*(mid+1) >= (n-m)*2:
high=mid
else:
low = mid+1
print(low+m)
``` | instruction | 0 | 50,155 | 8 | 100,310 |
No | output | 1 | 50,155 | 8 | 100,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
n,k,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
def cost(j):
if j < 0 or j + n - 1 >= k:
return 10000000000
maximum = 0
for i in range(n):
t = abs(a[i] - b[i+j]) + abs(b[i+j] - p)
maximum = max(maximum,t)
return maximum
l = 0
r = k
#Binary Search on l,r
while l < r:
mid = (l+r)//2
cost1 = cost(mid)
cost1r = cost(mid+1)
if cost1 < cost1r:
r = mid
else:
l = mid+1
print(cost(mid))
``` | instruction | 0 | 50,180 | 8 | 100,360 |
No | output | 1 | 50,180 | 8 | 100,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
n,k,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
def cost(j):
if j < 0 or j + n - 1 >= k:
return 1000000000
maximum = 0
for i in range(n):
t = abs(a[i] - b[i+j]) + abs(b[i+j] - p)
maximum = max(maximum,t)
return maximum
l = 0
r = k
#Binary Search on l,r
while l < r:
mid = (l+r)//2
cost1 = cost(mid)
cost1r = cost(mid+1)
if cost1 < cost1r:
r = mid
else:
l = mid+1
print(cost(mid))
``` | instruction | 0 | 50,181 | 8 | 100,362 |
No | output | 1 | 50,181 | 8 | 100,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
n,k,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
def cost(j):
if j < 0 or j + n - 1 >= k:
return 1000000000000
maximum = 0
for i in range(n):
t = abs(a[i] - b[i+j]) + abs(b[i+j] - p)
maximum = max(maximum,t)
return maximum
l = 0
r = k
#Binary Search on l,r
while l < r:
mid = (l+r)//2
cost1 = cost(mid)
cost1r = cost(mid+1)
if cost1 < cost1r:
r = mid
else:
l = mid+1
print(cost(mid))
``` | instruction | 0 | 50,182 | 8 | 100,364 |
No | output | 1 | 50,182 | 8 | 100,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
n, k, p = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
A.sort()
B.sort()
ans = 10**18
for start in range(k):
curmax = 0
curans = 10**18
if start+n >= k:
continue
for i in range(n):
curmax = max(curmax, abs(A[i] - B[start+i]) + abs(B[start+i] - p))
ans = min(ans, curmax)
print(ans)
``` | instruction | 0 | 50,183 | 8 | 100,366 |
No | output | 1 | 50,183 | 8 | 100,367 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,429 | 8 | 100,858 |
"Correct Solution:
```
while True:
n = int(input())
if not n: break
a = [list(map(int, input().split())) for _ in range(n)]
s = max(min(a[i]) for i in range(n))
c = [max(a[i][j] for i in range(n)) for j in range(n)]
for j in range(n):
if c[j] == s:
print(s)
break
else:
print(0)
``` | output | 1 | 50,429 | 8 | 100,859 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,430 | 8 | 100,860 |
"Correct Solution:
```
import sys
def get_min_value_in_row(data):
if(len(data) < 1):
return 0
min = 0
for i in range(0,len(data)):
if(data[i] < data[min]):
min = i
return min
def is_max(Min,index,data):
max = data[index][Min]
for i in range(index,-1,-1):
if(data[i][Min] > max):
max = data[i][Min]
for i in range(index,len(data)):
if(data[i][Min] > max):
max = data[i][Min]
return True if max == data[index][Min] else False
def print_both(data):
for i in range(0,len(data)):
indexmin = get_min_value_in_row(data[i])
if(is_max(indexmin,i,data) == True):
print(data[i][indexmin])
return
print(0)
l = []
for i in sys.stdin:
l.append(i)
i = 0
while(i < len(l)):
if(len(l[i]) == 2):
Matrix = []
for j in range(i+1,int(l[i])+i+1):
temp = [l[j].split()]
for k in range(0,len(temp[0])):
temp[0][k] = int(temp[0][k])
Matrix.append(temp[0])
i += int(l[i]) + 1
print_both(Matrix)
else:
i += 1
``` | output | 1 | 50,430 | 8 | 100,861 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,431 | 8 | 100,862 |
"Correct Solution:
```
import sys
while True:
n = int(sys.stdin.readline().rstrip())
if n == 0:
break;
students = []
for i in range(n):
students.append(list(map(int, sys.stdin.readline().rstrip().split(' '))))
s_list=[[min(row)==s for s in row] for row in students]
t_list=[[max(col)==s for s in col] for col in zip(*students)]
ret = [0]
for i,data in enumerate(zip(s_list, zip(*t_list))):
for j,d in enumerate(zip(*data)):
if all(d):
ret.append(students[i][j])
print(max(ret))
``` | output | 1 | 50,431 | 8 | 100,863 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,432 | 8 | 100,864 |
"Correct Solution:
```
while True:
n = int(input())
if not n:
break
#map???????????£????????£??¨????????????????°???????????????????????????????????????¨
f = [[int(j) for j in input().split()] for i in range(n)]
ans = max(min(f[i]) for i in range(n))
tans = [max(f[i][j] for i in range(n)) for j in range(n)]
for i in range(n):
if tans[i] == ans:
break
else:
ans = 0
print(ans)
``` | output | 1 | 50,432 | 8 | 100,865 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,433 | 8 | 100,866 |
"Correct Solution:
```
# AOJ 1005: Advanced Algorithm Class
# Python3 2018.7.5 bal4u
import sys
from sys import stdin
input = stdin.readline
while True:
n = int(input())
if n == 0: break
a = [list(map(int, input().split())) for r in range(n)]
b = [[0 for c in range(n)] for r in range(n)]
for r in range(n):
mi = min(a[r])
for c in range(n):
if a[r][c] == mi: b[r][c] |= 1
for c in range(n):
ma = max([a[r][c] for r in range(n)])
for r in range(n):
if a[r][c] == ma: b[r][c] |= 2
ans = 0
for r in range(n):
for c in range(n):
if b[r][c] == 3: ans = a[r][c]
print(ans)
``` | output | 1 | 50,433 | 8 | 100,867 |
Provide a correct Python 3 solution for this coding contest problem.
In the advanced algorithm class, n2 students sit in n rows and n columns. One day, a professor who teaches this subject comes into the class, asks the shortest student in each row to lift up his left hand, and the tallest student in each column to lift up his right hand. What is the height of the student whose both hands are up ? The student will become a target for professor’s questions.
Given the size of the class, and the height of the students in the class, you have to print the height of the student who has both his hands up in the class.
Input
The input will consist of several cases. the first line of each case will be n(0 < n < 100), the number of rows and columns in the class. It will then be followed by a n-by-n matrix, each row of the matrix appearing on a single line. Note that the elements of the matrix may not be necessarily distinct. The input will be terminated by the case n = 0.
Output
For each input case, you have to print the height of the student in the class whose both hands are up. If there is no such student, then print 0 for that case.
Example
Input
3
1 2 3
4 5 6
7 8 9
3
1 2 3
7 8 9
4 5 6
0
Output
7
7 | instruction | 0 | 50,434 | 8 | 100,868 |
"Correct Solution:
```
while 1:
N = int(input())
if N == 0:
break
S = [list(map(int, input().split())) for i in range(N)]
B = {max(S[i][j] for i in range(N)) for j in range(N)} & {min(S[i][j] for j in range(N)) for i in range(N)}
if B:
e, = B
print(e)
else:
print(0)
``` | output | 1 | 50,434 | 8 | 100,869 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.