Problem:


Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.



Sample input:
2
5 2 8 5 1 5


Sample output:
10

Submission status: Runtime Error

Solution code:
N = int(input())
a = [list(map(int, input().split()))]
a = a.sort()
array = []
for i in range(N):
  array = array.append(a[3*N-2*(i+1)])
ans = sum(array)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
data = list(map(int,input().split()))
data = sorted(data, reverse=True)
sum = 0
while data != []
    del data[0]
    del data[-1]
    sum += data[0]
    del data[0]
print(sum)

Submission status: Runtime Error

Solution code:
Not

Submission status: Runtime Error

Solution code:
n = int(input())
v = map(int, input().split())
v.sort(reverse = True)
s = 0
for i in range(0, n):
  s += v[1+2*i]
print(s)

Submission status: Runtime Error

Solution code:
s=int(input())
i= list(map(int, input().split()))
 
i.sort(reverse=True)
sum=0
ks=0
for j in range(s):
    sum+=i[j+1]
for j in range(n,2*n):
  ks+=i[j]
print(max(sum,ks))

Submission status: Runtime Error

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

print(sum(N//3:2N//3))

Submission status: Runtime Error

Solution code:
N = int(input())
a = sorted(map(int, input().split())) [::-1]
ans = 0
for i in range(0, 3*N, 2):
  ans += a[i+1]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = sorted(map(int, input().split())) [::-1]
ans = 0
for i in range(0, 3N, 2):
  ans += a[i+1]
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = a[:n+1]
a.sort()
ans = 0
for i in range(n*2):
  if (i)%2 == 0:
    ans += a[i]
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8

n = int(input().rstrip())

numbers = list(reversed(sorted(map(int, input().rstrip().split(" ")))))

ans = 0
for i in range(n):
    ans += numbers[n*i+1]

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort(reverse = True)
res = 0
i = 1
#L[1] + L[4] + ...
while i < len(L):
  res += L[i]
  i += 3
return res

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
ans = 0
for i in range(N):
    li = [A[2*i],A[2*i+1],A[3*N+i-2]]
    ans += li[1]
print(ans)

Submission status: Runtime Error

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

#強さを出力
def T(x,y,z):
    ma = max(x,y,z)
    mi = min(x,y,z)
    return (x+y+z) - (ma+mi)

b = a[0::2]
c = a[1::2]


ans = 0
for i in range(0,3*n//2,3):
    #print(b[i:i+3],c[i:i+3])
    ans += T(*b[i:i+3]) + T(*c[i:i+3])
print(ans)

Submission status: Runtime Error

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

#強さを出力
def T(x,y,z):
    ma = max(x,y,z)
    mi = min(x,y,z)
    return (x+y+z) - (ma+mi)

b = a[0::2]
c = a[1::2]


ans = 0
for i in range(0,3*n//2,3):
    print(b[i:i+3],c[i:i+3])
    ans += T(*b[i:i+3]) + T(*c[i:i+3])
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    a.sort(
    print(sum(a[n::2]))
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    a.sort(
    print(sum[n::2])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
print(sum[1:2*n+2:2])

Submission status: Runtime Error

Solution code:
from typing import Tuple, List

def main()->None:
    # ヒント見たけど参考にしてない
    N, x = map(int, input().split())
    a:List[int] = list(map(int, input().split()))
    sorted_a:List[int] = sorted(a) # 小さい順にならべる
    count:int = 0 # 何回配ったか
    for d in sorted_a:
        x -= d
        if x < 0: # 持ってた量が配る総量より少なかった時
            break
        count += 1
    # ちょうど配れない、必要な量より多く持ってた時はおおもらう人が出てくる。
    # 幸福人数の最大を求めればいいから余りを全部一人にあげればいいから−１
    if x > 0:
        print(count-1)
    else: # ちょうどの時はcount=N,持ってた量が配る総量より少なかった時は配れた人数
        print(count)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

a = reversed(sorted(a))
m = 0

for i in range(n, 2 * n):
    m += a[i]

print(m)

Submission status: Runtime Error

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


for i in range(n):
        sum=sum(a[2*i+1])
print(sum)

Submission status: Runtime Error

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

if n>=2:
    for i in range(n):
        sum=sum(a[2*i+1])
    
else:
    print(a[1])

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
s_sort = sorted(a, reverse=True)
score = 0
for i in range(1, 3N, 2):
    score += s_sort[i]
print(score)

Submission status: Runtime Error

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

a.sort(reverse=True)
ans = 0

A = []
n = N * 3
for i in range(1, n+1, 2):
    A.append(a[i])

for i in range(N):
    ans += A[i]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(sorted(list(map(int, input().split()))))
# N = 2
# A = list(sorted([5,2,8,5,1,5]))
group = []
ret = 0
for a in A:
    print(A)
    group.append([A.pop(0), A.pop(), A.pop()])
for g in group:
    ret += list(sorted(g))[1]
print(ret)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
a = [int(i) for i in a]

candidate = a[n:len(a)+1]
print(sum([candidate[i] for i in (1 , 2n+1, 2)]))

Submission status: Runtime Error

Solution code:
N = int(input())

a_s = input().split()

for i in ragne(3*N):
  a_s[i] = int(a_s[i])
  
a_s = sorted(a_s)

a_1 = a_s[:N]
a_2 = a_s[N:]

ans = sum(a_2[0::2])

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input)

a_s = input().split()

for i in ragne(3*N):
  a_s[i] = int(a_s[i])
  
a_s = sorted(a_s)

a_1 = a_s[:N]
a_2 = a_s[N:]

ans = sum(a_2[0::2])

print(ans)

Submission status: Runtime Error

Solution code:
###ABC 012A
N = input()
A = list(map(int, input().split()))
A = sorted(A,reverse = True)
ans = 0
for i in range(1, 2*N, 2):
    ans += A[i]
print(ans)

Submission status: Runtime Error

Solution code:
N = input()
A = list(map(int, input().split()))
A = sorted(A,reverse = True)
ans = 0
for i in range(1, 2*N, 2):
  ans += A[i]

Submission status: Runtime Error

Solution code:
N = input()
A = list(map(int, input().split()))
A = sorted(A,reverse = True)
ans = 0
for i in range(1, 2*N, 2):
    ans += A[i]

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
s = 0
for i in range(n):
    s += a[i * 2 + n]
print(sum(s))

Submission status: Runtime Error

Solution code:
n,a=int(input()),sorted(list(map(int,input().split())))print(sum([a[i]*(i%3==1)for i in range(3*n)]))

Submission status: Runtime Error

Solution code:
n = int(input())

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

sorted_a = sorted(a)
reversed_a = reversed(a)

aa = []
for i in range(3*n):
    sorted_a.pop(-1)
    reversed_a.pop(-1)
    aa.append(sorted_a.pop(-1))

print(sum(aa))

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
a=sorted(a)[::-1]
print(sum[1:2*n:2])

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) in input().split()]
a=sorted(a)[::-1]
print(sum[1:2*n:2])

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) in input().split()]
a=a.sort()[::-1]
print(sum[1:2*n:2])

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) in input().split()]
a.sort()[::-1]
print(sum[1:2*n:2])

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
print(sum(A[N:2*N])

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
print(sum(A[N:2*N])

Submission status: Runtime Error

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

def quicksort(seq):
    if len(seq) < 1:
        return seq
    pivot = seq[0]
    left = []
    right = []
    for x in range(1, len(seq)):
        if seq[x] <= pivot:
            left.append(seq[x])
        else:
            right.append(seq[x])
    left = quicksort(left)
    right = quicksort(right)
    foo = [pivot]
    return left + foo + right

a = quicksort(a)

for i in range(N):
    ans += a[-2]
    a = a[1:-2]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
print(sum(A[1::2][N]))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()


def calc(l):
    if len(l) == 3:
        return l[1]
    power = l[-2]
    l = l[1:-2]
    return calc(l) + power

print(calc(l))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map (int, input().split()))
l.sort()

def calc(l):
    if len(l) == 3:
        return l[1]
    power = l[-2]
    l = l[1:-2]
    return calc(l) + power

print(calc(l))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map (int, input().split()))
l.sort()

def calc(l):
    if len(l) == 3:
        return l[1]
    l = l[1:-2]
    return calc(l) + l[-2]

print(calc(l))

Submission status: Runtime Error

Solution code:
a =int(input())
x =list(map(int, input().split()))
x.sort()
s=0
for i in reversed(range(0,a//3,1):
  s+=x[i+1]
print(s)

Submission status: Runtime Error

Solution code:
a =int(input())
x =list(map(int, input().split()))
x.sort()
s=0
for i in reversed(range(1,(a+1)//3,1):
  s+=x[i]
print(s)

Submission status: Runtime Error

Solution code:
a =int(input())
x =list(map(int, input().split()))
x.sort()
s=0
for I in reversed(range(1,a//3,1):
  s+=x[i]
print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = map(int, input().strip().split())
arr.sort(reverse=True)
print(sum(arr[1:2*n:2]))

Submission status: Runtime Error

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

"""
agc012 C
"""


n, m = map(int, input().split())

graph = [[] for i in range(n+1)]
for i in range(m):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

q = int(input())
qlist = [tuple(map(int, input().split())) for i in range(q)]
qlist.reverse()

dp = [[-1]*11 for i in range(n+1)]
col = [0]


def updatedp(v, t, d):
    if dp[v][d] == -1:
        dp[v][d] = t
        if d > 0:
            updatedp(v, t, d-1)
            for u in graph[v]:
                updatedp(u, t, d-1)


t = 0
for v, d, c in qlist:
    col.append(c)
    updatedp(v, t, d)
    t += 1

for i in range(1, n+1):
    print(col[dp[i][0]+1])

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
a.sort()
m=a[n:2*n]
ans=sum(m)
for i in range(0,n,2):
    if m[i]+m[i+1]<=a[2*n+i]+a[n-i-1]:
        ans+= a[2*n+i]+a[n-i-1]-m[i]-m[i+1]
    else:break
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input)
a = sorted(list(map(int, input().split())))
print(sum(a[n:3*n]))

Submission status: Runtime Error

Solution code:
n = int(input)
a = sorted(list(map(int, input().split())))
print(sum(a[n:3n]))

Submission status: Runtime Error

Solution code:
n = int(input)
a = sorted(list(map(int, input().split())))
print(sum(a[n:3]))

Submission status: Runtime Error

Solution code:
for t in range(1):
    graph = {}
    colors = {}
    n, m = list(map(int, input().split()))
    for i in range(m):
        a, b = list(map(int, input().split()))
        if a not in graph:
            graph[a] = [b]
        else:
            graph[a].append(b)
        if b not in graph:
            graph[b] = [a]
        else:
            graph[b].append(a)              #Generating the graph as a dictionary.
        colors[a] = 0
        colors[b] = 0                               #Generating a colors dictionary to record color of each vertex.
    for q in range(int(input())):
        v, d, c = list(map(int, input().split()))
        first, sec = [v], []
        colors[v] = c
        for level in range(d):               
            for i in first:
                for j in graph[i]:
                    sec.append(j)
                    colors[j] = c
            first, sec = sec, []                       #Loop to color all vertices within d distance of given vertex v with color c.
    for i in range(1, n+1):
        print(colors[i])# your code goes here

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


color, depth, graph = [], [], {}
def func(v, d, c):
    if not color[v]:
        color[v] = c
    if depth[v] >= d or d == 0:
        return

    for u in graph[v]:
        func(u, d - 1, c)


def main():
    global color, depth, graph
    N, M = map(int, input().split())
    graph = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)

    Q = int(input())
    q = []
    for _ in range(Q):
        v, d, c = map(int, input().split())
        q.append((v - 1, d, c))

    color = [0] * N
    depth = [0] * N
    for i in range(Q - 1, -1, -1):
        func(q[i][0], q[i][1], q[i][2])
    print(*color, sep="\n")

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Submission status: Runtime Error

Solution code:

def main():
    N = int(input())
    a_list = get_a_list()

    a_list.sort(reverse=True)

    sum_second_a = 0
    
    import pdb; pdb.set_trace()
    while len(a_list) > 0:
        a_list.pop(0)
        sum_second_a += a_list.pop(0)
        a_list.pop(-1)

    print(sum_second_a)

def get_a_list():
    a_list = []
    strs = input().split()

    for a in strs:
        a_list.append(int(a))

    return a_list


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

C = []
W = []
for i in range( N ):
  c, w = map( int, input().split() )
  C.append( c - 1 )
  W.append( w )

c2w = [ [] for i in range( N ) ]
for i in range( N ):
  c2w[ C[ i ] ].append( W[ i ] )
for i in range( N ):
  c2w[ C[ i ] ].sort()

minw, _minwp = 2e9, -1
for i in range( N ):
  if minw > c2w[ i ][ 0 ]:
    minw = c2w[ i ][ 0 ]
    _minwp = i
minw2 = 2e9
for i in range( N ):
  if i == _minwp: continue
  if minw2 > c2w[ i ][ 0 ]:
    minw2 = c2w[ i ][ 0 ]

merged = [ False for i in range( N ) ]
hi = [ 1 for i in range( N ) ]
for i in range( N ):
  if len( c2w[ i ] ) == 0: continue
  lb, ub = 2, len( c2w[ i ] )
  while lb <= ub:
    mid = lb + ub >> 1
    if c2w[ i ][ 0 ] + c2w[ i ][ mid - 1 ] <= X:
      hi[ i ] = max( hi[ i ], mid )
      lb = mid + 1
    else:
      ub = mid - 1
  lb, ub = 1, len( c2w[ i ] )
  www = minw2 if minw != minw2 and c2w[ i ][ 0 ] == minw else minw
  while lb <= ub:
    mid = lb + ub >> 1
    if www + c2w[ i ][ mid - 1 ] <= Y:
      hi[ i ] = max( hi[ i ], mid )
      merged[ i ] = True
      lb = mid + 1
    else:
      ub = mid - 1

MOD = int( 1e9 + 7 )

fact = [ 1 ]
inv_fact = [ 1 ]
for i in range( 1, N + 1, 1 ):
  fact.append( fact[ i - 1 ] * i % MOD )
  inv_fact.append( pow( fact[ i ], MOD - 2, MOD ) )

ans = fact[ sum( merged[ i ] * hi[ i ] for i in range( N ) ) ]
for i in range( N ):
  if not merged[ i ]: continue
  ans = ans * inv_fact[ hi[ i ] ] % MOD

print( ans )

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
edges = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
edges += [[i, i] for i in range(n)]

q = int(input())
color = [0] * q
dp = [[0 for j in range(n)] for i in range(11)]
for i in range(q):
    v, d, color[i] = map(int, input().split())
    dp[d][v - 1] = i
 
for i in reversed(range(1, 11)):
    for u, v in edges:
        dp[i - 1][u] = max(dp[i - 1][u], dp[i][v])
        dp[i - 1][v] = max(dp[i - 1][v], dp[i][u])
 
print(*[color[dp[0][i]] for i in range(n)], sep = '\n')

Submission status: Runtime Error

Solution code:
import numpy as np
N,M = map(int,input().split())
nodes = [[False]* N for j in range(N)]
for m in range(M):
	a,b = map(int,input().split())
	nodes[a-1][b-1] = True
	nodes[b-1][a-1] = True
mat1 = np.matrix(nodes,dtype=bool)
mat2 = mat1.copy()
A = [mat1.tolist()]
for i in range(10):
	mat2 = mat2.dot(mat1)
	a = mat2.tolist()
	A.append(a)

white = [i for i in range(N)]
Colour = [0] * N
paint = []
Q = int(input())
for q in range(Q):
	line = list(map(int,input().split()))
	paint.append(line)
white2 = white[:]
paint.reverse()
for p in paint:
	v,d,c = p[0],p[1],p[2]
	if d == 0 and v - 1 in w:
		Colour[v - 1] = c
		white2.remove(v - 1)
		continue
	elif d == 0:
		continue
	else:
		for w in white:
			if d == 1:
				if not A[0][v - 1][w]:
					continue
				else:
					Colour[w] = c
					white2.remove(w)
			else:
				if A[d - 1][v - 1][w] or A[d - 2][v - 1][w]:
					Colour[w] = c
					white2.remove(w)
		if len(white2) == 0:
			break
	if v - 1 in white:
		Colour[v - 1] = c
		if v - 1 in white2:
			white2.remove(v - 1)
	white = white2[:]

for C in Colour:
	print(C)

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=sorted(a)
ans1=sum(b[N:2*N])

ans2=0
for i in range(N):
    ans2 += b[i*3+1]

ans3=0
for i in range(N):
    ans3 += b[i*2+4]

print(max(ans1,ans2,ans3))

Submission status: Runtime Error

Solution code:
import numpy as np

n,m=map(int,input().split())
e = []

for _ in range(1,m+1):
    a,b=map(int,input().split())
    e.append((a,b))

q = int(input())
cs = [0] * (q+1)
dp = np.zeros((n+1, 11), int)
for i in range(1, q+1):
    v,d,c=map(int,input().split())
    cs[i] = c
    dp[v,d] = i

for d in range(9, -1, -1):
    for i in range(1,n+1):
        dp[i,d] = max(dp[i,d], dp[i,d+1])
    for a,b in e:
        dp[a,d] = max(dp[a,d], dp[b,d+1])
        dp[b,d] = max(dp[b,d], dp[a,d+1])

for i in range(1, n+1):
    print(cs[dp[i,0]])

Submission status: Runtime Error

Solution code:
print(sum(sorted(map(int,input().split()))[::-1][1:2*int(input()):2]))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict

memo = {}
def dp(v, d, c):
    if memo.get((v, d), False):
        return None

    memo[(v, d)] = True
    if d == 0:
        res[v-1] = c
        return None

    dp(v, d-1, c)
    for e in dic.get(v, []):
        dp(e, d-1, c)


N, M = tuple(map(int, input().split(' ')))

dic = defaultdict(list)

for m in range(M):
    a, b = tuple(map(int, input().split(' ')))
    dic[a].append(b)
    dic[b].append(a)

Q = int(input())

vdc = [None]*Q

for q in range(Q):
    vdc[q] = tuple(map(int, input().split(' ')))

res = np.zeros(N)

for q in range(Q-1, -1, -1):
    v, d, c = vdc[q]
    dp(v, d, c)


for i in range(N):
    print(int(res[i]))

Submission status: Runtime Error

Solution code:
from collections import deque

G = dict()
N, M = map(int, input().split())
color = [0]*(N+1)
for n in range(1, N+1):
    G[n] = []
for _ in range(M):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
Q = int(input())




data = [list(map(int, input().split())) for _ in range(Q)]
data.reverse()
color_d = [-1]*(N+1)

for v, d, c in data:
    if color_d[v] >= d:
        pass
    # BFS
    deq = deque([v])
    if color[v] == 0:
        color[v] = c
    color_d[v] = d
    while len(deq) > 0:
        s = deq.popleft()
        for t in G[s]:
            if color_d[t] >= color_d[s] - 1:
                pass
            else:
                color_d[t] = color_d[s] - 1
                if color[t] == 0:
                    color[t] = c
                if color_d[t] > 0:
                    deq.append(t)
                
for n in range(1, N+1):
    print(color[n])

Submission status: Runtime Error

Solution code:
import numpy as np
N,M = map(int,input().split())
nodes = [[False]* N for j in range(N)]
for m in range(M):
	a,b = map(int,input().split())
	nodes[a-1][b-1] = True
	nodes[b-1][a-1] = True
mat1 = np.matrix(nodes,dtype=bool)
mat2 = mat1.copy()
A = [mat1.tolist()]
for i in range(10):
	mat2 = mat2.dot(mat1)
	a = mat2.tolist()
	A.append(a)
'''
for ai,a in enumerate(A):
	for k in a:
		print(k)
	print(ai)
'''
Colour = [0] * N
Q = int(input())
for q in range(Q):
	v,d,c = map(int,input().split())
	Colour[v - 1] = c
	if d == 0:
		continue
	for n in range(N):
		if d == 1 :
			if (not A[0][v - 1][n]):
				continue
			else:
				Colour[n] = c
		else:
			if A[d - 1][v - 1][n] or A[d - 2][v - 1][n] > 0:
				Colour[n] = c
	
for C in Colour:
	print(C)

Submission status: Runtime Error

Solution code:
import numpy as np
from numpy import linalg as LA
import math

mat_dic = {}

def f(mat, n):
    if mat_dic.get(n, None) is not None:
        return mat_dic.get(n, None)

    if n == 1:
        return mat

    if int(math.sqrt(n))**2 == n:
        return LA.matrix_power(f(mat, int(math.sqrt(n))), 2)

    return mat.dot(f(mat, n-1))



N, M = tuple(map(int, input().split(' ')))

mat = np.identity(N)
res = np.zeros(N)

for m in range(M):
    a, b = tuple(map(int, input().split(' ')))
    mat[a-1, b-1] = 1
    mat[b-1, a-1] = 1
    mat[a-1, a-1] = 1
    mat[b-1, b-1] = 1

Q = int(input())

for q in range(Q):
    v, d, c = tuple(map(int, input().split(' ')))
    x = f(mat, d)
    res[x[v-1,:] > 0] = c


for i in range(N):
    print(int(res[i]))

Submission status: Runtime Error

Solution code:
n = input()
x = [int(_) for _ in input().split()]

x.sort()
print(sum(x[n:n*2]))

Submission status: Runtime Error

Solution code:
import numpy as np


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

x = []
for n in range(N):
    x.append([])
    for i in range(3):
        x[n].append(0)

for i in range(N*3):
    x[i % N][i // N] = a[i]

for i in range(N-1, -1, -1):
    for j in range(i-1, -1, -1):
        if x[i][1] == x[j][1] and [j][2] < x[i][1] and x[i][2] > x[j][2]:
            x[j][2], x[i][1] = x[i][1], x[j][2]
            break
        else:
            break


res = 0

for n in range(N):
    res += x[n][1]

print(res)

Submission status: Runtime Error

Solution code:
import collections

if __name__ == '__main__':
    n, m = map(int, input().split())

    routes = collections.defaultdict(list)
    for _ in range(m):
        a, b = (int(x) - 1 for x in input().split())
        routes[a].append(b)
        routes[b].append(a)

    matrix = [[None for _ in range(n)] for _ in range(n)]
    for i in range(n):
        cnt = 1
        buffer = set([i])
        matrix[i][i] = 0

        while buffer and not all(matrix[i]):
            temp = set()
            for x in buffer:
                for j in routes[x]:
                    if matrix[i][j] is None:
                        matrix[i][j] = cnt
                        temp.add(j)

            buffer = temp
            cnt += 1

    colors = [0 for _ in range(n)]
    q = int(input())
    for _ in range(q):
        v, d, c = map(int, input().split())
        v -= 1

        for w in range(n):
            if matrix[v][w] is not None and matrix[v][w] <= d: colors[w] = c

    for color in colors: print(color)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  1 22:07:55 2017

@author: taichiNakamura
"""

N = int(input())
# スペース区切りの整数の入力
num = map(int, input().split())

import numpy as np
num = np.array(num)
num = np.sort(num)
sum = 0
for i in range(N):
    sum += num[3*N - 2 - i*2]
# 出力
print(sum)

Submission status: Runtime Error

Solution code:
input_lines = str(input())
input_lines2 = str(input())

n = int(input_lines)
aAry = input_lines2.split(' ')
aAry = map(int,aAry)
aAry = sorted(aAry)
#print(aAry)

answer1 = 0L
for i in range(n):
    answer1 += int(aAry[(-i-1)*2])

answer2 = 0L
for i in range(n):
    answer2 += int(aAry[-i-3])

if answer1 >= answer2:
    answer = answer1
else:
    answer = answer2
print(answer)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
N = int(raw_input())
a = raw_input().split(" ")
output = 0

a.sort()

for i in a[N:2*N]:
    output = output + int(i)

print output

Submission status: Runtime Error

Solution code:
import sys

INF = 10**9+7

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())
def nil(): return list(map(int, stdin.readline().split()))

n = ni()

a = nil()

a.sort()

ans = 0

for i in range(n):
    ans += a[-2-i*n]
print(ans)