Problem:


Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.



Sample input:
3
1 2 3


Sample output:
1

Submission status: Runtime Error

Solution code:
n = input()
a = list(map(int,input().split()))
a = str(a)
a_average = sum(a)/n
mix = 0
a_near = [mix=a[i]-a_average for i in range(n) if a[i]-a_average < mix]
print(a)

Submission status: Runtime Error

Solution code:
input();a=map(int,input().split());b=[abs(i-sum(a)/n)for i in a];print(b.index(min(b)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
input = list(map(int, input().split()))

count, sum, start = 0, 0, 0
substring = []
while (True):
    sum += input[count]
    substring.append(sum)
    if start == len(input) - 1:
        break
    if count == len(input) - 1:
        start += 1
        count = start
        sum = 0
        continue

    count += 1
substring.sort(reverse=True)

result = substring[0]
for i in range(1, K):
    result = result & substring[i]

print(result)

Submission status: Runtime Error

Solution code:
_ = (input())
num = list(map(int, input().split()))
ave = int(sum(num) / len(num))
diff = [abs(x - ave) for x in num]
print(num.index(min(diff)+1))

Submission status: Runtime Error

Solution code:
import numpy as np
 
def main():
	n = int(input())
	s = input()
	q = int(input())
	klist = list(map(int, input().split()))
	
	for times in range(q):
		r = 0
		k = klist[times]
		nowd, nowm, nowdm = 0, 0, 0
		for i in range(n):
			c = s[i]
			if (c == "D"):
				nowd += 1
			elif (c == "M"):
				nowm += 1
				nowdm += nowd
			elif(c == "C"):
				r += nowdm
			if(i >= k-1):
				cc = s[i-k+1]
				if (cc == "D"):
					nowd -= 1
					nowdm -= nowm
				elif (cc == "M"):
					nowm -= 1
		print(r)
main()

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

ave = sum(a) / N

aa =[abs(a[i] - ave) for i in range(N)]

x = min(aa)

print(aa.find(x))

Submission status: Runtime Error

Solution code:
N = int(input())
li = list(map(int, input()split()))

s = sum(li)
mean = s / N

li_div = [ ]
for i in range(N):
	div = abs(li[i] - mean)
	li_div.append(div)

min_div = min(li_div)

for i in range(N):
	if li_div == min_div:
		print(i)
		break

Submission status: Runtime Error

Solution code:
N = int(input())
li = list(map(int, input()split()))

s = sum(li)
N = len(li)
mean = s / N

li_div = []
for i in range(N):
	div = abs(li[i] - mean)
	li_div.append(div)

min_div = min(li_div)

for i in range(N):
	if li_div == min_div:
		print(i)
		break

Submission status: Runtime Error

Solution code:
N = int(input())

mean = 0
frames = []
for i range(N):
  frames.append(int(input()))
  mean += frames[i]

mean = mean / N

sa = 100
ind = 0
for i in range(N):
  if sa > abs(frames[i] - mean):
    sa = abs(frames[i] - mean)
    ind = i
    
print(ind)

Submission status: Runtime Error

Solution code:
N = int(input())

frames = []
for i range(N):
  frames.append(int(input()))

mean = sum(frames) / N

sa = 100
ind = 0
for i in range(N):
  if sa > abs(frames[i] - mean):
    sa = abs(frames[i] - mean)
    ind = i
    
print(ind)

Submission status: Runtime Error

Solution code:
N = int(input())

frames = []
for i range(N):
  frames.append(int(input()))

mean = sum(frames) / N

sa = 100
for i in range(N):
  if sa > abs(frames[i] - mean):
    sa = abs(frames[i] - mean)
    ind = i
    
print(ind)

Submission status: Runtime Error

Solution code:
s = input()
w = input().split()
for i in range(int(s)):
  sum += int(w[i])
average = sum / int(s)
for i in range(int(s)):
    if abs(int(w[i]) - average) < limit:
        limit = abs(int(w[i]) - average)
print(limit)

Submission status: Runtime Error

Solution code:
T, A, B, C, D = map(int, raw_input().split())

if A + C <= T:
    print B + D
elif C <= T:
    print D
elif A <= T:
    print B
else:
    print 0

Submission status: Runtime Error

Solution code:
n=int(input())
l=map(int,input().split())
su=sum(l)
best=10000000000000000
res=0
for i in range(len(l)):
  if(su-n*l[i]<best):
    best=su-n*l[i]
    res=i
print(res)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))

b = []
for i in range(n):
    r = 0
    for j in range(i,n):
        r += a[j]
        b.append(r)


n2 = n*(n+1)//2

res = 0
for i in range(31):
    p = res + 2**(30-i)
    
    c = 0
    for j in range(n2):
        if p == p&b[j]:
            c+=1
    
    if c >= k:
        res = p

print(res)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))

b = []
for i in range(n):
    r = 0
    for j in range(n-i):
        r += a[i+j]
        b.append(r)


n2 = n*(n+1)//2

res = 0
for i in range(41):
    p = res + 2**(40-i)
    
    c = 0
    for j in range(n2):
        if p == p&b[j]:
            c+=1
    
    if c >= k:
        res = p

print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
a = map(int, input().split())

mean = sum(a)/N

a = map(lambda x: abs(x-mean), a)

print(min(a).index)

Submission status: Runtime Error

Solution code:
from math import log2
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [[a[i]] for i in range(n)]
c = list(a)

for i in range(n-1):
    for j in range(i+1, n):
        b[i].append(a[j]+b[i][-1])
        c.append(b[i][-1])

m = 40 #ceil(log2((10**9)*1000))=40よりbit幅を40としておけば十分
ans = 0
for i in range(m, -1, -1):
    log = 2**i
    tmp = [x for x in c if x&log==log]
    if len(tmp) >= k:
        ans += log
        c = tmp
print(ans)

Submission status: Runtime Error

Solution code:



import sys
import collections
import itertools
import math
import re
from collections import Counter
from collections import deque
from collections import defaultdict

N = int(input())
S = input()
Q = int(input())
K = [int(x) for x in input().split()]


for k in K:
    ret = 0
    d = 0
    m = 0
    dm = 0

    for i in range(N):
        s = S[i]

        if s == "C":
            ret += dm

        if s == "D":
            d += 1
        elif s == "M":
            m += 1
            dm += d

        j = i - k + 1
        if j >= 0:
            s2 = S[j]
            if s2 == "D":
                d -= 1
                dm -= m
            elif s2 == "M":
                m -= 1
    print(ret)



sys.exit(0)

Submission status: Runtime Error

Solution code:
from sys import stdin
n,k=[int(x) for x in stdin.readline().rstrip().split()]
data=[int(x) for x in stdin.readline().rstrip().split()]

tmp=[]
cnt=0
for i in range(len(data)):
    for j in range(len(data)-i):
        tmp.append(((sum(data[i:1+i+j]))))
        cnt+=1
beat_v=sorted(tmp)
tmp2 = list(map(lambda x: len(bin(x)), beat_v))

#同じ桁のbit列がK個以上あるか探索
cnt2=0
maxbit=tmp2[-1]
res=0
for l in reversed(tmp2):
    if maxbit==l:
        cnt2+=1
    maxbit=l
    if cnt2==k:
        res=maxbit
        break
cnt3=0
for m in reversed(tmp2):
    if m != res:
        cnt3+=1
    else :
        break
        
res_=list(reversed(beat_v))[cnt3:cnt3+k]
result=res_[0]
for g in res_:
    result=result&g
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
Q = int(input())
Ks= list(map(int, input().split()))

anss = []
for K in Ks:
    cnt_d, cnt_m, sum_dm, ans = 0,0,0,0
    for i in range(N):
        if i-K>=0:
            if S[i-K]=="D":
                cnt_d -= 1
                sum_dm -= cnt_m
            elif S[i-K]=="M":
                cnt_m -= 1

        if S[i]=="D":
            cnt_d += 1
        elif S[i]=="M":
            sum_dm += cnt_d
            cnt_m += 1
        elif S[i]=="C":
            ans += sum_dm
    anss.append(ans)
    
for a in anss:
  print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
Q = int(input())
Ks= list(map(int, input().split()))

for K in Ks:
    cnt_d, cnt_m, sum_dm = 0,0,0
    ans = 0
    for i in range(N):
        if i-K>=0:
            if S[i-K]=="D":
                cnt_d -= 1
                sum_dm -= cnt_m
            elif S[i-K]=="M":
                cnt_m -= 1

        if S[i]=="D":
            cnt_d += 1
        elif S[i]=="M":
            sum_dm += cnt_d
            cnt_m += 1
        elif S[i]=="C":
            ans += sum_dm

    print(ans)

Submission status: Runtime Error

Solution code:
line = str(input())
lines = line.split(' ')

n = int(lines[0])
k = int(lines[1])

d = list()

line = str(input())
lines = line.split(' ')

for l in lines:
    d.append(int(l))

ds = list()

for i in range(0,n):
    for len in range(1,n-i+1):
        sum = 0
        for j in range(i,i+len):
            sum += d[j]
        ds.append(sum)

digit = 0
o = list()

for i in range(40,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

    if count >= k :
        ds = o.copy()

    if count == k :
        break

sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]

print(sum)

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np
N = int(input())
S = [i for i in list(input())]
Q = int(input())
k = [int(i) for i in input().split()]
S = np.array(S, dtype=None)
 
index_D = np.where(S == 'D')[0]
index_M = np.where(S == 'M')[0]
index_C = np.where(S == 'C')[0]
xx, yy, zz = np.meshgrid(index_D, index_M, index_C)
a_b_c = np.c_[xx.ravel(), yy.ravel(), zz.ravel()]

for k_i in k:
    count = 0
    for abc in a_b_c:
        if abc[0] < abc[1] < abc[2] and abc[2] - abc[0] < k_i:
            count += 1
    print(count)

Submission status: Runtime Error

Solution code:
import itertools

line = str(input())
lines = line.split(' ')

n = int(lines[0])
k = int(lines[1])

d = list()

line = str(input())
lines = line.split(' ')

for l in lines:
    d.append(int(l))

ds = list()

for i in range(0,n):
    for len in range(1,n-i+1):
        sum = 0
        for j in range(i,i+len):
            sum += d[j]
        ds.append(sum)

digit = 0
o = list()

for i in range(31,-1,-1):
    count = 0
    o.clear()
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)
    if count >= k :
        ds = o.copy()

    if count == k :
        break

sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]

print(sum)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())

A_sum = [0]
[A_sum.append(A_sum[-1]+int(a)) for a in input().split()]

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]

print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;


int main() {
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        int tmp = 0;
        cin >> tmp;
        vec.push_back(tmp);
    }
    if(vec.size() == 1){
        cout << 0 << endl;
        exit(0);
    }
    long int avg = (float) accumulate(vec.begin(), vec.end(), 0) / (float) vec.size();
//    cout << "avg: " << avg << endl;
    int mi = 0;
    float m_from_avg = abs(avg - vec[0]);
    for (size_t i = 1; i < n; i++) {
        float tmp = abs(avg - vec[i]);
        if (tmp < m_from_avg) {
            m_from_avg = tmp;
            mi = i;
        }
    }

    cout << mi << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

from sys import stdin

def load():
    n = int(stdin.readline())
    s = stdin.readline()
    q = int(stdin.readline())
    k = [int(x) for x in stdin.readline().split()]
    
    return (s, k)

def calc(s, ks):
    dmcmap = dmc(s)
    ans = [sum(dmcmap[:k]) for k in ks]
    print(*ans, sep='\n', flush=True)

def dmc(src):
    ret = [0 for _ in range(len(src))]

    dmap = [i for i, x in enumerate(src) if x == 'D']
    mmap = [i for i, x in enumerate(src) if x == 'M']
    cmap = [i for i, x in enumerate(src) if x == 'C']

    for d in dmap:
        for m in filter(lambda x: x > d, mmap):
            for c in filter(lambda x: x > m, cmap):
                ret[c-d] += 1
    return ret

stdin = open('data.txt')
s, ks = load()
calc(s,ks)

Submission status: Runtime Error

Solution code:
import itertools
n,d = map(int,input().split())
a = list(map(int,input().split()))

b = []
c = 0
m = n
for i in range(n):
    for j in range(m):
        for k in range(j+1):
            c += a[k]
        b.append(c)
        c = 0
    m -= 1
    del a[0]
b = list(set(b))

h = list(itertools.permutations(range(len(b)), d))
p = []
q = []
for i in range(len(h)):
    for j in range(d):
        p.append(b[h[i][j]])
        
    for k in range(d-1):
        k += 1
        if k == 1:
            e = bin(p[k])
            f = bin(p[k-1])
            g = int(e,0) & int(f,0)
        g = int(bin(g),0) & int(bin(p[k]),0)
    q.append(g)
    p = []

print(max(q))

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline())
S = list(sys.stdin.readline())
Q = int(sys.stdin.readline())
k = list(map(int, sys.stdin.readline().split(' ')))

# chache = []

oms = [(i, S[i]) for i in range(N) if S[i] == 'D' or S[i] == 'M' or S[i] == 'C']
oml = len(oms)

def process(ki, index, selected = 'n'):
    global cache

    if index == oml:
      return []

    if cache[index][selected] is not None:
        # print('use cache')
        return cache[index][selected]

    rets = []
    # char = oms[index][1]
    if selected == 'b':
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'C':
                rets.append(oms[ind][0])
    elif selected == 'a':
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'M':
                rets += process(ki,ind+1, 'b')
    else:
        for ind in range(index, oml):
            char = oms[ind][1]
            if char == 'D':
                cind = process(ki,ind+1, 'a')
                ret = []
                for v in cind:
                    if v != False and ((v - oms[ind][0]) < ki):
                      ret.append(True)
                rets += ret

    cache[index][selected] = rets

    return rets


for ki in k:
    cache = [{'a': None, 'b': None, 'c': None, 'n': None} for i in range(oml)]
    ret = process(ki, 0)
    print(len(ret))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

input = raw_input().split(" ")
N = int(input[0])
K = long(input[1])

aVec = raw_input().split(" ")
a = []

for i in aVec:
    a.append(long(i))


VecNum = N * (N + 1) / 2


subarray = []

for i in range(0, N):
    a_bubun = a[i:]
    for k in range(0, len(a_bubun)):
        subarray.append(a_bubun[k:])


#print subarray

beauty = []
for i in subarray:
    beauty.append(sum(i))
beauty.sort(reverse=True)


#K個大きい物を選ぶ
aK = []
for i in range(0, K):
    aK.append(beauty[i])


#論理積の最大値なので、最小値を求める
print min(aK)

Submission status: Runtime Error

Solution code:



def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append("{:061b}".format(sum(As[i:j])))
    print("alls", len(alls))
    def find(i, alls):
        if i > 60:
            return []
        ones = [a for a in alls if a[i] == "1"]
        if len(ones) < K:
            return find(i + 1, alls)
        elif len(ones) == K:
            return ones
        elif len(ones) > K:
            ret = find(i + 1, ones)
            if len(ret) == 0:
                return ones[0:K]
            else:
                return ret

    ans = find(0, alls)
    if len(ans):
        a = 0xFFFFFFFFFFF
        for p in ans:
            a &= int(p, 2)
        return a
    else:
        return 0

if __name__ == "__main__":
    l = input()
    N = int(l.split(" ")[0])
    K = int(l.split(" ")[1])
    As = [int(c) for c in input().split()]
    print(solve(N, K, As))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import numpy as np
import itertools

and_memo = {}


def list_subarray_sums(arr, length):
    cumsum = np.insert(np.cumsum(arr), 0, 0)
    for l in range(1, length + 1):
        for i in range(length - l + 1):
            yield cumsum[l + i] - cumsum[i]


def logical_and(arr, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = np.array(list(map(int, input().split(' '))))

    sums = set(list_subarray_sums(A, N))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr, K), combinations)

    print(max(ands))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import numpy as np
import itertools
import sys

sys.setrecursionlimit(3000)

and_memo = {}


def list_subarray_sums(arr, length):
    cumsum = np.insert(np.cumsum(arr), 0, 0)
    for l in range(1, length + 1):
        for i in range(length - l + 1):
            yield cumsum[l + i] - cumsum[i]


def logical_and(arr, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = np.array(list(map(int, input().split(' '))))

    sums = set(list_subarray_sums(A, N))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr, K), combinations)

    print(max(ands))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split))
aver = int(sum(a)/N)

inta = (0, -1, 1, -2, 2, -3, 3)

for atest in inta:
    if a in atest+aver:
        print(atest+aver)
        break

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

from sys import stdin
from functools import reduce

def load():
    n = int(stdin.readline())
    s = stdin.readline()
    q = int(stdin.readline())
    k = [int(x) for x in stdin.readline().split()]
    
    return (s, k)

def calc(s, ks):
    dmcmap = dmc(s, max(ks))
    for k in ks:
        print(sum(dmcmap[:k]))

def dmc(src, kmax):
    ret = [0 for _ in range(kmax)]

    for ind in range(len(src)):
        if src[ind] == 'D':
            for inm in range(ind+1, len(src)):
                if src[inm] == 'M':
                    for inc in range(inm+1, len(src)):
                        if src[inc] == 'C':
                            ret[inc-ind] += 1
    return ret

s, ks = load()
calc(s,ks)

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
k = [int(x) for x in sys.stdin.readline().rstrip().split(' ')]

def counter(S, ki):
  count = 0
  for i in range(0, len(S)): 
    for j in range( i+2, len(S)):
      if j - i <=1 or j - i >= ki:
        continue
      for l in range(i+1, j):
        if (S[i], S[l], S[j]  ) == ('D', 'M', 'C'):
          count += 1
  return count
  
for ki in k:
  print(counter(S, ki))

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
k = [int(x) for x in sys.stdin.readline().rstrip().split(' ')]

def counter(S, ki):
  count = 0
    for i in range(0, len(S)): 
      for j in range( i+2, len(S)):
        if j - i <=1 or j - i >= ki:
          continue
        for l in range(i+1, j):
          if (S[i], S[l], S[j]  ) == ('D', 'M', 'C'):
            count += 1
  return count
  
for ki in k:
  print(counter(S, ki))

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
k = [int(x) for x in sys.stdin.readline().rstrip().split(' ')]

def counter(S, ki):
	count = 0
	for i in range(0, len(S)): 
	  for j in range( i+2, len(S)):
	  	if j - i <=1 or j - i >= ki:
	  		continue
	  	for l in range(i+1, j):
	  		if (S[i], S[l], S[j]  ) == ('D', 'M', 'C'):
	  			count += 1
	return count
  
for ki in k:
	print(counter(S, ki))

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort()
b=b[::-1]
for sp in range(0,N+1-K):
    if [len(bin(b[i])[2:]) for i in range(sp,sp+K)] == [len(bin(b[sp])[2:])for _ in range(sp,sp+K)]:
        break
s=b[sp:sp+K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort()
b=b[::-1]
for sp in range(0,N+1-K):
    if [len(bin(b[i])[2:]) for i in range(sp,sp+K)] == [len(bin(b[sp])[2:])for i in range(sp,sp+K)]:
        break
s=b[sp:sp+K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
i = 0
while i < k*10:
    m = n - i
    for j in range(i+1):
        c = a[j:j + m]
        if sum(c) != 0:
            b.append(sum(c))
    i += 1
d = sorted(b, reverse=True)
for i in range(k):
    c.append(d[i])

ans = d[0]
for i in range(k):
    if i+1 == len(c):
        break
    ans = ans & c[i+1]
print(ans)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
i = 0
while i < n*(n-1)//2:
    m = n - i
    for j in range(i+1):
        c = a[j:j + m]
        if sum(c) != 0:
            b.append(sum(c))
    i += 1
d = sorted(b, reverse=True)
for i in range(k):
    c.append(d[i])

ans = d[0]
for i in range(k):
    if i+1 == len(c):
        break
    ans = ans & c[i+1]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N, K = map(int, input().split())
a = list(map(int, input().split()))

dp = [[-1 for k in range(N*(N+1)//2+1)] for n in range(1000+1)]

def generate_substring(a):
    for i in range(N):
        for j in range(i, N):
            yield a[i:j+1]

def find_max_and(i, k):

    if dp[i][k] >= 0:
        return dp[i][k];
    
    if i == N_SUBSTR:
        res = 0xFFFF if k == 0 else 0
    elif k == 0:
        res = find_max_and(i + 1, k)
    else:
        a = find_max_and(i + 1, k)
        b = find_max_and(i + 1, k - 1) & scores[i]
        
        res = b if a == 0xFFFF else max(a, b)

    dp[i][k] = res
    return res

scores = list(map(sum, generate_substring(a)))
N_SUBSTR = len(scores)
print(find_max_and(0, K))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N, K = map(int, input().split())
a = list(map(int, input().split()))

dp = [[-1 for k in range(N*(N+1)//2+1)] for n in range(1000+1)]

def generate_substring(a):
    for i in range(N):
        for j in range(i, N):
            yield a[i:j+1]

def find_max_and(i, k):

    if dp[i][k] >= 0:
        return dp[i][k];
    
    if i == N_SUBSTR:
        res = 0xFFFF
    elif k == 0:
        res = find_max_and(i + 1, k)
    else:
        a = find_max_and(i + 1, k)
        b = find_max_and(i + 1, k - 1) & scores[i]
        
        res = b if a == 0xFFFF else max(a, b)

    dp[i][k] = res
    return res

scores = list(map(sum, generate_substring(a)))
N_SUBSTR = len(scores)
print(find_max_and(0, K))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
	long long n, m, t=0, ans=0;
	cin >> n >> m;
	long long A[n];
	for(int i=0; i<n; i++)
	{
		cin >> A[i];
		ans+=A[i];
	}
	if(n==1)
		cout << 0;
	else{	
	sort(A, A+n);
	long long q=0;
	for(int i=n-1; i>=1; i--)
	{
		if(A[i]!=A[i-1])
		{
			if(t>A[i]-A[i-1]-1)
				t-=A[i]-A[i-1]-1;
			else	
				q+=A[i]-A[i-1]-1-t;
				t=0;
		}
		else
			t++;
	}
	//cout << q;
//	if(A[0]!=A[1]){
	if(A[1]-(n-2)>0)
	{
		if(t>max(min(A[1]-(n-2)-2, A[0]-1), 0LL))
			t-=max(min(A[1]-(n-2)-2, A[0]-1), 0LL);
		else	
			q+=max(min(A[1]-(n-2)-2, A[0]-1), 0LL)-t;
			t=0;
	}
//	}
	//cout << ans<< " " << n <<" " <<q;
	ans-=n+q;
	cout << ans;
	}
/*	for(int i=n-1; i>=1; i--)
	{
		if((A[i]-A[i-1])==0){
			ans+=A[i]-1;
			A[i-1]--;
		}
		else
		{
			ans+=A[i-1];
		}
	}
	*/
	return 0;
}

Submission status: Runtime Error

Solution code:
N = int(input())
a = sorted([(i, int(x)) for i, x in enumerate(input().split())], key=lambda x: x[1])
avg = sum([l[1] for l in a]) / N
d = 999999999999
t = -1
pre = -1
for i, n in a:
    if n == pre:
        continue
    p = n - avg if n >= avg else avg - n
    if p == 0:
        t = i
        break
    elif p < d:
        d = p
        t = i
    elif:
        break
print(t)

Submission status: Runtime Error

Solution code:
import itertools as iter

n, k = map(int, input().split())
a = list(map(int, input().split()))

beauty_list = []
for i in range(len(a) + 1):
    for element in iter.combinations(a, i):
        beauty = sum(element)
        beauty_list.append(beauty)

max_and = 0
for combi in iter.combinations(set(beauty_list), k):
    ans = combi[0]
    for element in combi:
        ans &= element
    if ans > max_and:
        max_and = ans

print(max_and)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
s=b[:K]

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
s=b[:K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N

if round(avg) in a:
    print(a.index(round(avg)))
else:
    result = 100
    for i in range(N):
        temp = abs(avg-a[i])
        if temp <= result:
            result = temp
    print(a.index(round(result)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N

if round(avg) in a:
    print(a.index(round(avg)))
else:
    result = 100
    for i in range(N):
        temp = abs(a[i]-avg)
        if temp <= result:
            result = temp
    print(a.index(round(result)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N

if round(avg) in a:
    print(a.index(avg))
else:
    result = 100
    for i in range(N):
        temp = abs(a[i]-avg)
        if temp <= result:
            result = temp
    print(a.index(round(result)))

Submission status: Runtime Error

Solution code:
import sys
N = int(sys.stdin.read().rstrip())
a = [int(x) in x for stdin.readline().rstrip().split(' ')]
d = [abs(sum(a)/float(N) - ai) for ai in a]

if len(set(d))==1:
	print(0)
else:
	 print(d.index(min(d)))

Submission status: Runtime Error

Solution code:
N = int(sys.stdin.read().rstrip())
a = [int(x) in x for stdin.readline().rstrip().split(' ')]
d = [abs(sum(a)/float(N) - ai) for ai in a]

if len(set(d))==1:
	print(0)
else:
	 print(d.index(min(d)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N
result = 100

if round(avg) in a:
    print(a.index(avg))
else:
    for i in range(N):
        temp = abs(avg-a[i])
        if temp <= result:
            result = temp
    print(a.index(round(result)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N
result = 100

if avg in a:
    print(a.index(avg))
else:
    for i in range(N):
        temp = abs(avg-a[i])
        if temp <= result:
            result = temp
    print(a.index(round(result)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

avg = 0
for i in range(N):
    avg += a[i]
avg /= N
a = list(set(a))
result = 100


if avg in a:
    result = a.index(avg)
else:
    for i in range(len(a)):
        temp = abs(avg-a[i])
        if temp <= result:
            result = temp
    result = a.index(round(result))
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
a = sorted([(i, int(x)) for i, x in enumerate(input().split())], key=lambda x: x[1])
avg = sum([l[1] for l in a]) / N
d = 999999999999
t = -1
for i, n in a:
    p = n - avg if n >= ang else avg - n
    if p < d:
        d = p
        t = i
    elif p > d:
        print(t)

Submission status: Runtime Error

Solution code:
N = int(input())
a = sorted([(i, int(input())) for i in range(N)], key=lambda x: x[1])
avg = sum([l[1] for l in a]) / N

d = 999999999999
t = -1

for i, n in a:
    p = n - avg if n >= ang else avg - n
    if p < d:
        d = p
        t = i
    elif p > d:
        print(t)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
a = []
for i in range(N):
    a.append(int(input()))

mean = np.mean(a)

idx = np.abs(np.asarray(a) - mean).argmin()

print(idx)

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))

k = 0
ans = 0
for i in range(N):
    k += a[i]
find = k/N
now = 1000000
for i in range(N):
    if abs(a[i]-find) < now - 0.00001:
        ans = i
        now = abs(a[i]-find)
print(ans)

Submission status: Runtime Error

Solution code:
3
1 2 3

Submission status: Runtime Error

Solution code:
n= int(input())
lis = list(map(int,input().split())
cou = 0
num = sum(lis)/n
for i in range(n):
  lis[i] -= num
  cou = min(cou,abs(lis[i]))
for i in range(n):
  if cou == lis[i]:
    print(i)
    exit()