Problem:


Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6 

\rm{comb}(6,2)=15 

\rm{comb}(6,4)=15 

\rm{comb}(9,2)=36 

\rm{comb}(9,4)=126 

\rm{comb}(9,6)=84 

\rm{comb}(11,2)=55 

\rm{comb}(11,4)=330 

\rm{comb}(11,6)=462 

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0



Sample input:
5
6 9 4 2 11


Sample output:
11 6

Submission status: Time Limit Exceeded

Solution code:
import math
from bisect import bisect_left

def C(a,b):
    return math.factorial(a) // (math.factorial(b)*math.factorial(a-b))

def check(*args):
    n = len(args)
    temp = []
    for i in range(n):
        temp.append(C(args[i][0], args[i][1]))
    return args[temp.index(max(temp))]

n = int(input())
a = sorted(list(map(int, input().split())))
ans = [1, 0]

while len(a) > 1:
    A = a.pop(-1)
    if A//2 in a:
        ans = check([A, A//2], ans)
        break
    elif A % 2 != 0 and A//2 + 1 in a:
        ans = check([A, A//2 + 1], ans)
        break
    else:
        x = A//2
        temp = bisect_left(a, x)
        if temp == 0:
            ans = check([A, a[0]], ans)
        elif temp == len(a):
            ans = check([A, a[-1]], ans)
        else:
            ans = check([A, a[temp]], [A, a[temp-1]], ans)

print(ans[0], ans[1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import collections
import bisect

from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

def main():
    _ = int(input())
    A = list(map(int,input().split()))
    A.sort()
    cand = []
    while A:
        a = A.pop()
        if not A:
            break
        bi = bisect.bisect_left(A, a//2)
        br = A[bi-1]
        if bi < len(A):
            if abs(a/2-A[bi-1]) > abs(a/2-A[bi]):
                br = A[bi]
        x = combinations_count(a,br)
        cand.append((x,a,br))
#        print(A,a,a//2,bi,br,x)
    cand.sort(key=lambda x:x[0], reverse=True)
    print(cand[0][1], cand[0][2])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

def comb(a,b):
  return math.factorial(a)//((math.factorial(b)*math.factorial(a-b)))

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

a.sort()
ans=-1
for i in range(N-1):
  if ans<comb(a[-1],a[i]):
    ans=comb(a[-1],a[i])
    max_i=i
print(a[-1],a[max_i])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

a.sort()

ai=a[n-1]

a0=factorial(ai)//(factorial(ai-a[0])*factorial(a[0]))


for i in range(1,n-1):
    j=a[i]
    temp=a0
    for k in range(1,a[i]-a[i-1]+1):
        temp*=(ai-(a[i-1]+k-1))/(a[i-1]+k)

    if a0>=temp:
        print(str(ai)+" "+str(a[i-1]))
        exit()
    a0=temp

print(str(ai)+" "+str(a[n-2]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

import math
m = {}
def f(n):
    if m.get(n) != None:
        return m[n]
    a = math.factorial(n)
    m[n] = a
    return a
def comb(n, r):
    return f(n) // f(r) // f(n - r)

ans = 0
t = 0
for i in range(N - 1):
    a = comb(L[-1], L[i])
    if ans < a:
        ans = a
        t = i

print(L[-1], L[t])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
from math import factorial as f

def combi(n_k):
    n, k = n_k
    return f(n) // f(n-k) // f(k)

N = int(input())
A = list(map(int, input().split()))

*else_A, max_A = sorted(A)

x = bisect(else_A, max_A//2)

print(*max([(max_A, else_A[y]) for y in [x-1, x] if 0 <= y < N-1], key=combi))

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import mul
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


n = ni()
a = na()
a.sort()
l = a[-1]
ans = -1
for i in range(n):
    if l - a[i] - 1 > a[i]:
        continue
    else:
        if cmb(l, a[i]) > cmb(l, a[i - 1]):
            print(l, a[i])
        else:
            print(l, a[i-1])
        break

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import mul
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


n = ni()
a = na()
a.sort()
r = a[-1]
ans = -1
for i in range(n):
    num = cmb(r, a[i])
    if ans < num:
        ans = num
    else:
        print(r, a[i-1])
        break

Submission status: Time Limit Exceeded

Solution code:
import bisect
import math
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
temp = 0
for j, i in enumerate(a):
    k = bisect.bisect_left(a[:j], (i / 2) + 1) - 1
    if (i - a[k]) > 0:
        if math.factorial(i) // (math.factorial(a[k]) * (math.factorial(i - a[k]))) > temp:
            ans = [i, a[k]]
        else:
            continue
    else:
        continue
print(*ans)

Submission status: Time Limit Exceeded

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

from scipy.misc import comb

temp = 0
for i in range(n-1):
  c = comb(l[-1], l[i], exact=True) 
  if c >= temp:
    p = l[-1]
    q = l[i]
    temp = c
print(p, q)

Submission status: Time Limit Exceeded

Solution code:
# 2019/09/05

n=int(input())
*a,mx=sorted(map(int,input().split()))

def cmb(n):
    fac=[1,1]
    for i in range(2,n+1):
        fac.append(fac[i-1]*i)
    return fac


fac=cmb(mx)
res=0
ans=[]

for r in a:
    tmp=fac[mx]//(fac[r]*fac[mx-r])
    if tmp>res:
        res=tmp
        ans=[mx,r]
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
import math

def comb(p,q):
   kumi =math.factorial(p)/(math.factorial(q)*math.factorial(p-q))
   return kumi

n = int(input())
numbers =list(map(int,input().split()))
sornum =sorted(numbers, reverse=True)

large =0
inum = 0
jmum = 0

for j in range(n-1):
   a = comb(sornum[0],sornum[j+1])
   if a>=large:
      large = a
      inum = sornum[0]
      jnum =sornum[j+1]


print(inum,jnum)

Submission status: Time Limit Exceeded

Solution code:
import math

def comb(p,q):
   kumi =math.factorial(p)/(math.factorial(q)*math.factorial(p-q))
   return kumi

n = int(input())
numbers =list(map(int,input().split()))
sornum =sorted(numbers, reverse=True)

large =0
inum = 0
jmum = 0
for i in range(n-1):
   for j in range(n-i):
      a = comb(sornum[i],sornum[i+j])
      if a>=large:
         large = a
         inum = sornum[i]
         jnum =sornum[i+j]
          
print(inum,jnum)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    numer = reduce(mul, range(n, n - r, -1))
    denom = reduce(mul, range(1, r + 1))
    return numer // denom


n = int(input())
a = list(map(int, input().split()))
ai = max(a)
ans_val = 0
for aj in set(a):
    if cmb(ai, aj) > ans_val:
        ans = [ai, aj]
        ans_val = cmb(ai, aj)
print(' '.join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

def comb(n, r):
  return factorial(n) / factorial(r) / factorial(n - r)

n = int(input())
A = sorted(list(map(int, input().split())))
max_A = A[-1]
half_A = A[0]
for i in range(len(A) - 1):
  if A[i] < max_A / 2 and i < len(A) - 2:
    half_A = A[i]
  else:
    if comb(max_A, half_A) > comb(max_A, A[i]):
      print(max_A, half_A)
    else:
      print(max_A, A[i])
    exit()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def perm(n, r):
    return factorial(n) // factorial(n - r)


def comb(n, r):
    return perm(n, r) // factorial(r)


n = int(input())
A = list(map(int, input().split()))
A.sort()
m = A.pop()
biggest = 0
biggest_r = 0
for r in A:
    var = comb(m, r)
    if var > biggest:
        biggest = var
        biggest_r = r
print(m, biggest_r)

Submission status: Time Limit Exceeded

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

def comb(n, k):
    ans = 1
    for i in range(k):
        ans = ans * (n - i) // (i + 1)
    return ans


i = bisect_left(a, a[-1]/2)
ans = comb(a[-1], a[i])
if i > 0:
    if ans > comb(a[-1], a[i-1]):
        print(a[-1], a[i])
    else:
        print(a[-1], a[i-1])
else:
    print(a[-1], a[i])

Submission status: Time Limit Exceeded

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

def comb(n, r):
	c, m = 1, 1
	for i in range(r):
		c *= (n - i)
		m *= (1 + i)
	return c // m

a = sorted(A)
max_comb, max_n, max_r = 0, a[-1], None
lo, hi = max(0, N // 2 - 1), min(N, N // 2 + 2)
for r in range(lo, hi):
	current_comb = comb(max_n, a[r])
	if max_comb < current_comb:
		max_comb = current_comb
		max_r = a[r]
print(max_n, max_r)

Submission status: Time Limit Exceeded

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

def comb(n, r):
	c, m = 1, 1
	for i in range(r):
		c *= (n - i)
		m *= (1 + i)
	return c // m

a, l, r, max_comb = sorted(A), 0, 1, 0
for n in range(N - 1):
	current_comb = comb(a[-1], a[n])
	if max_comb < current_comb:
		max_comb, l, r = current_comb, a[-1], a[n]
print(l, r)

Submission status: Time Limit Exceeded

Solution code:
import math
import bisect
def logcomp(n,m):
    if m in [0,n]:
        r = 1
    else:
        a = (sum(map(math.log,[i for i in range(max(n-m+1,m+1),n+1)])))
        b = (sum(map(math.log,[i for i in range(1,min(n-m+1,m+1))])))
        r = a-b
    return r



n = int(input())
num_list = sorted([ int(v) for v in input().split() ])


for i in range(1,n):
    x = num_list[i]
    y = bisect.bisect_left(num_list,x/2)
    if y == i:
        z = logcomp(x,num_list[y-1])
        w = num_list[y-1]
    else:
        if abs(num_list[y]-(x/2)) <= abs(num_list[y+1]-(x/2)):
            z = logcomp(x,num_list[y])
            w = num_list[y]
        else:
            z = logcomp(x,num_list[y+1])
            w = num_list[y+1]
    if i == 1:
        ans = (x,w)
        max_z = z
    else:
        if max_z < z:
            max_z = z
            ans = (x,w)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
from bisect import bisect_left
from sys import stdin


def solve(a):
    a.sort()
    max_cmb = cmb(a[1], a[0])
    ans = [a[1], a[0]]
    for ai in a[n-1:1:-1]:
        divided_ai = ai//2
        ak = a[bisect_left(a, divided_ai)]
        q = cmb(ai, ak)
        if a[1] <= ai//2:
            aj = a[bisect_left(a, divided_ai)-1]
            p = cmb(ai, aj)
            if q < p and max_cmb < p:
                ans = [ai, aj]
                max_cmb = p
            elif max_cmb < q:
                ans = [ai, ak]
                max_cmb = q
            else:
                return str(ans[0]) + ' '+str(ans[1])

        else:
            if max_cmb < q:
                ans = [ai, ak]
                max_cmb = q
            else:
                return str(ans[0]) + ' '+str(ans[1])

    return str(ans[0]) + ' '+str(ans[1])


def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, stdin.readline().split()))
    print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
from bisect import bisect_left


def solve(a):
    a.sort()
    max_cmb = cmb(a[1], a[0])
    ans = [a[1], a[0]]
    for ai in a[n-1:1:-1]:
        divided_ai = ai//2
        ak = a[bisect_left(a, divided_ai)]
        q = cmb(ai, ak)
        if a[1] <= ai//2:
            aj = a[bisect_left(a, divided_ai)-1]
            p = cmb(ai, aj)
            if q < p and max_cmb < p:
                ans = [ai, aj]
                max_cmb = p
            elif max_cmb < q:
                ans = [ai, ak]
                max_cmb = q
            else:
                return str(ans[0]) + ' '+str(ans[1])

        else:
            if max_cmb < q:
                ans = [ai, ak]
                max_cmb = q
            else:
                return str(ans[0]) + ' '+str(ans[1])

    return str(ans[0]) + ' '+str(ans[1])


def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
from bisect import bisect_left


def solve(a):
    a.sort()
    max_cmb = cmb(a[1], a[0])
    ans = [a[1], a[0]]
    for ai in a[2:]:
        divided_ai = ai//2
        ak = a[bisect_left(a, divided_ai)]
        q = cmb(ai, ak)
        if a[1] <= ai//2:
            aj = a[bisect_left(a, divided_ai)-1]
            p = cmb(ai, aj)
            if q < p and max_cmb < p:
                ans = [ai, aj]
                max_cmb = p
            elif max_cmb < q:
                ans = [ai, ak]
                max_cmb = q
            else:
                pass

        else:
            if max_cmb < q:
                ans = [ai, ak]
                max_cmb = q

    return str(ans[0]) + ' '+str(ans[1])


def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
import math
import sys


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

    max_a = max(a_list)
    a_list.remove(max_a)

    max_comb = - sys.maxsize
    answer1 = max_a
    answer2 = -1
    for i in range(n-1):
        comb = math.factorial(max_a)/(math.factorial(a_list[i]) * math.factorial(max_a - a_list[i]))
        if max_comb < comb:
            max_comb = comb
            answer2 = a_list[i]

    print(str(answer1) + " " + str(answer2))

Submission status: Time Limit Exceeded

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

comb1 = max(a)
comb2 = comb1//2
a.sort()
p = 0
keep = float("inf")
for i in range(n):
    if abs(a[i] - comb2) <= keep:
        p = a[i]
        keep = abs(a[i]-comb2)
import math
ans = math.factorial(comb1)//((math.factorial(p))*(math.factorial(comb1-p)))
if comb1 == p:
    print(comb1,0)
else:
    print(comb1,p)

Submission status: Time Limit Exceeded

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

import math
def comb(n,r):
    return math.factorial(n) // (math.factorial(n-r)*math.factorial(r))

temp = 0
for i in range(len(a)):
    for j in range(len(a)):
        if i != j:
            if a[i] >= a[j]:
                if temp < comb(a[i],a[j]):
                    temp = comb(a[i],a[j])
                    ans = (a[i],a[j])

print(ans[0],ans[1])

Submission status: Time Limit Exceeded

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

a = sorted(a)

import numpy as np
def getNearestValue(list, num):
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]
  
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
ans = 0
i = n - 1
tmp0 = a[i] / 2
tmp1 = getNearestValue(a[0:i], tmp0)
tmp2 = cmb(a[i], tmp1)
if tmp2 > ans:
    ans = tmp2
    ans1 = a[i]
    ans2 = tmp1
print(ans1, ans2)

Submission status: Time Limit Exceeded

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

a = sorted(a)

import numpy as np
def getNearestValue(list, num):
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]
  
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
ans = 0
for i in range(1,n):
    tmp0 = a[i] / 2
    tmp1 = getNearestValue(a[0:i], tmp0)
    tmp2 = cmb(a[i], tmp1)
    if tmp2 > ans:
        ans = tmp2
        ans1 = a[i]
        ans2 = tmp1
print(ans1, ans2)

Submission status: Time Limit Exceeded

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

A.sort()

def comb(x, y):
    p = 1
    d = 1
    for i in range(y):
        p *= x - i
        d *= y -i
    return p // d

ans = []
max_val = 0
mid = 0
for i in range(n):
    diff = min(A[mid], A[i]-A[mid])
    is_finished = False
    while mid < i:
        if diff > min(A[mid+1], A[i]-A[mid+1]):
            if max_val < comb(A[i], A[mid]):
                ans = [A[i], A[mid]]
                max_val = comb(*ans)
            is_finished = True
            break
        mid += 1
    if not is_finished and max_val < comb(A[i], A[mid-1]):
        ans = [A[i], A[mid-1]]
        max_val = comb(*ans)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def main():
    n = intin()
    alist = intina()

    alist.sort()

    m = alist[-1]

    ind = binl(alist, m // 2)
    ind1 = ind - 1 if ind > 0 else None
    ind2 = ind

    if ind1 is not None and ind1 != len(alist) - 1:
        c1 = combination(m, alist[ind1])
    else:
        c1 = -float('inf')

    if ind2 != len(alist) - 1:
        c2 = combination(m, alist[ind2])
    else:
        c2 = -float('inf')

    if c1 > c2:
        print('%d %d' % (m, alist[ind1]))
    else:
        print('%d %d' % (m, alist[ind2]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def nCr(n,r):
  if n < 0 or r < 0 or n < r: return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n
  
  # 分子のn*(n-1)*...がr個分続くやつ
  numerator = [n-r+k+1 for k in range(r)]
  # 分母：r!=r*(r-1)*...*3*2の要素
  denominator = [k+1 for k in range(r)]
  
  # 分母の要素で割れる部分を割っていく部分
  for p in range(2, r+1):
    # 分母は1,2,3,...rのようになっており、1は意味がないのでスキップした形か
    pivot = denominator[p-1]
    if pivot > 1:
      # 分子のX番目と分母のX-offset番目が共通の約数を持つということだと思う。piv分ずれているのだから、pivの倍数というところか
      offset = (n-r) % p
      for k in range(p-1, r, p):
        # 約分できる要素について割る
        numerator[k - offset]  /= pivot
        denominator[k] /= pivot
        
  ret = 1
  for k in range(r):
    if numerator[k] > 1: ret *= int(numerator[k])
      
  return ret


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

max_comb = 0
n = 0
r = 0
n = a[0]
for j in range(1, N):
  if max_comb < nCr(a[0],a[j]):
    max_comb = nCr(a[0],a[j])
    r = a[j]
    
ans = str(n) + " " + str(r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nCr(n,r):
  if n < 0 or r < 0 or n < r: return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n
  
  # 分子のn*(n-1)*...がr個分続くやつ
  numerator = [n-r+k+1 for k in range(r)]
  # 分母：r!=r*(r-1)*...*3*2の要素
  denominator = [k+1 for k in range(r)]
  
  # 分母の要素で割れる部分を割っていく部分
  for p in range(2, r+1):
    # 分母は1,2,3,...rのようになっており、1は意味がないのでスキップした形か
    pivot = denominator[p-1]
    if pivot > 1:
      # 分子のX番目と分母のX-offset番目が共通の約数を持つということだと思う。piv分ずれているのだから、pivの倍数というところか
      offset = (n-r) % p
      for k in range(p-1, r, p):
        # 約分できる要素について割る
        numerator[k - offset]  /= pivot
        denominator[k] /= pivot
        
  ret = 1
  for k in range(r):
    if numerator[k] > 1: ret *= int(numerator[k])
      
  return ret


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

max_comb = 0
n = 0
r = 0
for i in range(N):
  for j in range(i+1, N):

    if max_comb < nCr(a[i],a[j]):
      max_comb = nCr(a[i],a[j])
      n = a[i]
      r = a[j]
    
ans = str(n) + " " + str(r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:

import bisect


from functools import reduce
from operator import mul

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under




def main():
    num = int(input())
    data = list(map(int, input().split()))
    data.sort()

    ans = 0
    ans_list = []

    for i in range(num - 1, num):
        left_ind = max(bisect.bisect_left(data, data[i] // 2) - 1, 0)
        if left_ind + 1 >= i:
            right_ind = left_ind
        else:
            right_ind = left_ind + 1
        ans_kari = cmb(data[i], data[left_ind])
        if ans < ans_kari:
            ans = ans_kari
            ans_list = [data[i], data[left_ind]]
        ans_kari = cmb(data[i], data[right_ind])
        if ans < ans_kari:
            ans = ans_kari
            ans_list = [data[i], data[right_ind]]

    print(ans_list[0], ans_list[1])






if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
A.sort()
max_a = A[-1]
A.pop(N-1)
ans = 0
ans_a = 0
for a in A:
    tmp = comb(max_a, a, exact=True)
    if tmp > ans:
        ans = tmp
        ans_a = a
print(max_a, ans_a)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
from operator import mul

import numpy as np
import re
import sys

sys.setrecursionlimit(10000)
INF = float('inf')


def comb(n, r):
    """
    scipy.misc.comb または scipy.special.comb と同じ
    組み合わせの数 nCr
    :param int n:
    :param int r:
    :rtype: int
    """
    assert n >= r
    r = min(n - r, r)
    if r == 0:
        return 1
    return reduce(mul, range(n, n - r, -1)) // reduce(mul, range(r, 0, -1))


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

max_comb = 0
ans = 0, 1
for a in A:
    def test(i):
        global max_comb, ans
        if 0 <= i < N and A[i] < a:
            c = comb(a, A[i])
            if c > max_comb:
                max_comb = c
                ans = a, A[i]

    # a /2 に近いほど大きい
    i = bisect.bisect_left(A, a / 2)
    test(i - 1)
    test(i)
    test(i + 1)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from bisect import bisect

def C(n, r):
	return factorial(n) // factorial(r) // factorial(n-r)

n=int(input())
a=sorted(map(int, input().split()))

z = a.pop()
r = bisect(a, z//2)
l = r - 1

if l < 0:
	print("%d %d" % (z, a[r]))
elif r >= len(a):
	print("%d %d" % (z, a[l]))
elif C(z, a[r]) > C(z, a[l]):
	print("%d %d" % (z, a[r]))
else:
	print("%d %d" % (z, a[l]))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

n=int(input())
a=sorted(map(int, input().split()), reverse=True)

his = {}
def fac(n):
	if n not in his:
		his[n] = factorial(n)
	return his[n]

def C(n, r):
	return fac(n) // fac(r) // fac(n-r)

ix = 1
vx = C(a[0], a[1])

for i in range(1, n):
	if vx < C(a[0], a[i]):
		ix = i
		vx = C(a[0], a[i])

print("%d %d" % (a[0], a[ix]))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

n=int(input())
a=sorted(map(int, input().split()), reverse=True)

def C(n, r):
	return factorial(n) // factorial(r) // factorial(n-r)

ix = 1
vx = C(a[0], a[1])

for i in range(1, n):
	if vx < C(a[0], a[i]):
		ix = i
		vx = C(a[0], a[i])

print("%d %d" % (a[0], a[ix]))

Submission status: Time Limit Exceeded

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

b = sorted(a,reverse=True)

def binary_search(x):
    l,r = 0,n-1
    while l+1<r:
        i = (l+r)//2
        if b[i] > x:
            l = i
        else:
            r = i
    if abs(b[l]-x)>=abs(b[l+1]-x):
        l += 1
    return b[l]

def comb(n,r):
    r = min(r,n-r)
    ret = 1
    for i in range(1,r+1):
        ret *= (n+1-i)
        ret /= i
    return int(ret)

ans = ""
m = 0
for c in b[:1]:
    if m > c**(c//2):
        break
    r = binary_search(c/2)
    comb_c_r = comb(c,r)
    if m < comb_c_r:
        m = comb_c_r
        ans = str(c)+" "+str(r)
print(ans)

Submission status: Time Limit Exceeded

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

b = sorted(a,reverse=True)

def binary_search(x):
    l,r = 0,n-1
    while l+1<r:
        i = (l+r)//2
        if b[i] > x:
            l = i
        else:
            r = i
    if abs(b[l]-x)>=abs(b[l+1]-x):
        l += 1
    return b[l]

def comb(n,r):
    r = min(r,n-r)
    ret = 1
    for i in range(1,r+1):
        ret *= (n+1-i)
        ret /= i
    return int(ret)

ans = ""
m = 0
for c in b[:-1]:
    if m > c**(c//2):
        break
    r = binary_search(c/2)
    comb_c_r = comb(c,r)
    if m < comb_c_r:
        m = comb_c_r
        ans = str(c)+" "+str(r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
A = max(a)
t = -1
tt = 0
for i in range(n):
    if t < a[i]:
        t = a[i]
        tt = i
a.pop(tt)
x = 0
y = A
z = 0
for i in range(1, A):
    x += 1
    y -= 1
    if x >= y:
        z = A - x
        break
d = float('INF')
da = 0
for i in range(n - 1):
    if d > abs(a[i] - z):
        da = a[i]
        d = da - z
print(A, da)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from itertools import combinations

n = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)

def comb(n,r):
    return factorial(n)//(factorial(n-r) * factorial(r))

res = []
for i in A[1:]:
    res.append([comb(A[0],i),A[0],i])

res.sort(reverse=True)

print(res[0][1],res[0][2])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from itertools import combinations

n = int(input())
A = tuple(map(int,input().split())) 

def comb(n,r):
    return factorial(n)//(factorial(n-r) * factorial(r))

res = []
for a,b in combinations(A,2):
    if a < b:
        a,b = b,a
    res.append([comb(a,b),a,b])

res.sort(reverse=True)
print(res[0][1],res[0][2])

Submission status: Time Limit Exceeded

Solution code:
import scipy.misc

N = int(input())
lst = sorted(list(map(int, input().split())), reverse=True)

max_comb = -1
for i in range(N):
    if max_comb > scipy.misc.comb(lst[i], lst[i]//2, exact=1):
        break
    k = lst[i]
    mid = k/2
    high = i
    low = N-1
    dif = abs(mid - lst[low])
    #print(mid)
    while abs(high - low) > 1:
        if dif > abs(mid - lst[(high+low)//2]):
            dif = abs(mid - lst[(high+low)//2])
            low = (high+low)//2
        else:
            high = (high+low)//2
        #print(high, low)
    p = lst[high] if abs(mid - lst[high]) < abs(mid - lst[low]) else lst[low]
    comb = scipy.misc.comb(k, p, exact=1)
    if max_comb < comb:
        ans = (k, p)
        #print(ans, comb)
        max_comb = comb

print(ans[0], ans[1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a_ = sorted(a)
if len(a)==2:
    print(a_[1],a_[0])
    exit()
a = a_[::-1]
import bisect
import math
def combinations_count(n,r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
ret = 0
for i in range(n//2):
    k = bisect.bisect_left(a_,a[i]//2)
    if a[i]//2-a_[k]>a_[k+1]-a[i]//2:
        k += 1
    cmb = combinations_count(a[i],a_[k])
    if ret<cmb:
        ans = [a[i],a_[k]]
        ret = cmb
print(ans[0],ans[1])