Problem:


Taro's Shopping

Mammy decided to give Taro his first shopping experience.
Mammy tells him to choose any two items he wants from those
listed in the shopping catalogue,
but Taro cannot decide which two, as all the items look attractive.
Thus he plans to buy the pair of two items with
the highest price sum, not exceeding the amount Mammy allows.
As getting two of the same item is boring, he wants two different items.

You are asked to help Taro select the two items.
The price list for all of the items is given.
Among pairs of two items in the list,
find the pair with the highest price sum
not exceeding the allowed amount,
and report the sum.
Taro is buying two items, not one, nor three, nor more.
Note that, two or more items in the list may be priced equally.

Input

The input consists of multiple datasets, each in the following format.

n m
a1 a2 ... an

A dataset consists of two lines.
In the first line, the number of items n and the maximum
payment allowed m are given.
n is an integer satisfying 2 &#8804; n &#8804; 1000.
m is an integer satisfying 2 &#8804; m &#8804; 2,000,000.
In the second line, prices of n items are given.
ai (1 &#8804; i &#8804; n) is the price
of the i-th item.
This value is an integer greater than or equal to 1 and
less than or equal to 1,000,000.

The end of the input is indicated by a line containing two zeros.
The sum of n's of all the datasets does not exceed 50,000.

Output

For each dataset, find the pair with the highest price sum
not exceeding the allowed amount m
and output the sum in a line.
If the price sum of every pair of items exceeds m,
output NONE instead.

Sample Input

3 45
10 20 30
6 10
1 2 5 8 9 11
7 100
11 34 83 47 59 29 70
4 100
80 70 60 50
4 20
10 5 10 16
0 0

Output for the Sample Input

40
10
99
NONE
20



Sample input:
3 45
10 20 30
6 10
1 2 5 8 9 11
7 100
11 34 83 47 59 29 70
4 100
80 70 60 50
4 20
10 5 10 16
0 0


Sample output:
40
10
99
NONE
20

Submission status: Accepted

Solution code:
def main():
    while True:
        n,m = map(int,input().split())
        if not n and not m:
            return
        aa = list(map(int,input().split()))
        ans = 0
        for i in range(n):
            for j in range(n):
                if i == j:
                    continue
                tmp = aa[i] + aa[j]
                if tmp <= m:
                    ans = max(ans, tmp)
        if ans == 0:
            print("NONE")
        else:
            print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

while True:
    n,m = map(int,input().split())
    if [n,m] == [0,0]:
        break
    
    a = list(map(int,input().split()))
    a.sort()
    res = -1
    
    for i in range(n):
        k = bisect.bisect_right(a,m-a[i])
        if k == i+1:
            k -= 1
        if k != 0:
            res = max(res,a[i]+a[k-1])
    if res == -1:
        print('NONE')
    else:
        print(res)

Submission status: Accepted

Solution code:
ans_list = []

def solve():
    n,m = map(int,input().split())
    if (n,m) == (0,0):
        return "end"
    a = list(map(int,input().split()))
    res = -float("inf")
    n = len(a)
    for i in range(n):
        for j in range(i+1,n):
            cand = a[i] + a[j]
            if res < cand <= m:
                res = cand
    if res == -float("inf"):
        res = "NONE"
    return res

while True:
    ans = solve()
    if ans == "end":
        break
    ans_list.append(ans)

for ans in ans_list:
    print(ans)

Submission status: Accepted

Solution code:
while True:
    n, m = list(map(int, input().split()))
    if n == 0 and m == 0:
        break
    a = [int(i) for i in input().split()]
    total = []
    i = 0
    for i in range(n-1):
        for j in range(i+1, n):
            if a[i] + a[j] <= m:
                total.append(a[i] + a[j])

    if len(total) > 0:
        print(max(total))
    else:
        print("NONE")

Submission status: Accepted

Solution code:
while True:

    items,money=list(map(int,input().split()))

    if money==0 and items==0:
        break

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

    times=int(items*(items-1)/2)

    sum_list=[]

    low=0
    high=1
    sum=0
    i = 0

    while True:
        #sum=prices[low]+prices[high]

        if high>items-1:
            low+=1
            high=low+1
        if low==items-1:
            break
        #print(high,low)
        sum = prices[low] + prices[high]
        #print(sum)
        if sum<=money:
            sum_list.append(sum)
            high+=1
            i+=1
        else:
            high+=1

    if i==0:
        print("NONE")
    else:
        print(max(sum_list))

Submission status: Accepted

Solution code:
while True:
    n, m = map(int,input().split())
    if (n,m) == (0,0):break
    price = list(map(int, input().split()))
    price.sort()
    summ = []
    k = -1
    j = 0
    if price[0]+price[1] > m:
            print('NONE')
            continue
    while True:
        if j == n + k:break
        elif price[j] +  price[k] <= m:
            summ.append(price[j] +  price[k])
            j += 1
        else:
            k -= 1
    summ.sort()
    print(summ[-1])

Submission status: Accepted

Solution code:
while 1:
    n, m = map(int, input().split())
    if n == 0: break
    price = sorted(list(map(int, input().split())))
    maxx = 0
    for i in range(len(price)):
        for j in range(i + 1, len(price)):
            temp = price[i] + price[j]
            if maxx < temp and temp <= m: maxx = price[i] + price[j]
    if maxx == 0: print("NONE")
    else: print(maxx)

Submission status: Accepted

Solution code:
while 1:
  n,m= map(int, input().split())
  if n==0 and m==0:
    break
  A = list(map(int, input().split()))
  B = []
  for i in range(n):
    for j in range(1+i,n):
      b = A[i]+A[j]
      B.append(b)
  #print(B)

  B = sorted(B, reverse=1)
  #print(B)
  B = [x for x in B if x <= m]
  print(B[0] if len(B) > 0 else "NONE")

Submission status: Accepted

Solution code:
while True:
  n,m = map(int,input().split())
  if n+m==0:
    break
  a = list(map(int,input().split()))
  ans = -1
  for i in range(n-1):
    for j in range(i+1,n):
      ans = max(a[i]+a[j],ans) if a[i]+a[j]<=m else ans
  print("NONE" if ans==-1 else ans)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())  # 品物個数n, 最大の金額m
    if n == 0:
        break

    A = list(map(int, input().split()))
    # print(A)
    ans = 0
    for i in range(len(A)):
        for j in range(i+1, len(A)):
            if A[i] + A[j] > m:
                continue
            else:
                ans = max(ans, A[i]+A[j])

    if ans == 0:
        print('NONE')
    else:
        print(ans)

Submission status: Accepted

Solution code:
#1行目 品物の個数n 許容額m
#2行目　a[i](1 <= i <= n)
#どの品物の組も価格合計が m を超える場合は，代わりに NONE と出力する
while True :

  n,m = map(int,input().split())
  #もし(n,m) = (0,0)ならbreak
  if n==0:
    break
  else:
    #配列a[i]を読み込む(1<=i<=n)
    a = sorted(list(map(int, input().strip().split())))
    
    a_sum = 0
    #(最大値) + (その他1つ) <=mを探す
    for i in range(0,n-1):
      for j in range(i+1,n):
        if max(a_sum, a[i] + a[j]) <= m :
          a_sum = max(a_sum, a[i] + a[j])
    
    if a_sum ==0 :
      print("NONE")
    else :
      print(a_sum)

Submission status: Accepted

Solution code:
# Taro's Shopping
while True:
  n, m = map(int, input().split())
  if n+m == 0:
    break
  a = sorted(list(map(int, input().split())))
  ans = 0
  pos = n-1
  for i in range(n):
    while pos>i and a[i]+a[pos]>m:
      pos -= 1
    if pos<=i:
      break
    ans = max(ans , a[i]+a[pos])
  print(ans if ans>0 else 'NONE')

Submission status: Accepted

Solution code:
"""
a = int(input())
# スペース区切りの整数の入力
b, c = map(int, input().split())
# 文字列の入力
s = input()
# 出力
print(str(a+b+c) + " " + s)
"""


def main():
	while(1):

		N,n = map(int, input().split())
		
		if N == 0 and n == 0:
			break

		a = list(map(int, input().split()))
		ans = 0
		for i in range(N):
			for j in range(i+1,N):
				if a[i]+a[j] <= n and a[i]+a[j] > ans:
					ans = a[i]+a[j]

		if ans == 0:
			print("NONE")
		else:
			print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split(" "))
    if n == 0:
        break

    items = []
    for i in input().split(" "):
        items.append(int(i))


    #全探索
    items2 = items
    sums = []
    for i in range(len(items)):
        for j in range(len(items)):
            if i == j:
                continue
            sums.append(items[i] + items2[j])

    sums.sort(reverse=True)
    for i in range(len(sums)):
        if sums[i] <= m:
            print(sums[i])
            break
        if i == len(sums) - 1:
            print("NONE")

Submission status: Accepted

Solution code:
import itertools

while 1:
    n, m = map(int, input().split())
    if n == 0:
        break

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

    ans = 0
    for v in itertools.combinations(data, 2):
        if sum(v) <= m and sum(v) > ans:
            ans = sum(v)

    if ans == 0:
        print("NONE")
    else:
        print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(n,m):
        a = LI()
        r = None
        for i in range(n):
            for j in range(i+1,n):
                t = a[i] + a[j]
                if m >= t and (r is None or r < t):
                    r = t

        if r is None:
            return 'NONE'
        return r

    while 1:
        n,m = LI()
        if n == 0:
            break
        rr.append(f(n,m))

    return '\n'.join(map(str,rr))


print(main())

Submission status: Accepted

Solution code:
def main():
  while True:
    n, m = map(int, input().split())
    if n == 0:
      break
  
    items = sorted(list(map(int, input().split())))
    ans = 0
    for i in range(n - 1):
      p1 = items[i]
      for j in range(i + 1, n):
        p2 = p1 + items[j]
        if p2 > m:
          break
        if ans < p2:
          ans = p2
    if ans != 0:
      print(ans)
    else:
      print("NONE")
  
main()

Submission status: Accepted

Solution code:
import bisect

while True:
    n, m = map(int, input().split())
    if n == 0:
        break
    a = sorted(map(int, input().split()))
    index = bisect.bisect_left(a, m) - 1
    if index <= 0:
        print("NONE")
        continue
    ans = 0
    for i in range(index, 0, -1):
        for j in range(i-1, -1, -1):
            if a[i] + a[j] <= m:
                ans = max(ans, a[i] + a[j])
    if ans:
        print(ans)
    else:
        print("NONE")

Submission status: Accepted

Solution code:

while True:
    (n,m) = map(int,input().split())
    if n == 0 and m == 0:
        break
    iss = list(map(int, input().split()))
    best = max([ (iss[i] + iss[j] if not ( i == j) and iss[i] + iss[j] <= m else -1) for i in range(0,n) for j in range(0,n) ])
    if(best < 0):
        print("NONE")
    else:
        print(best)

Submission status: Accepted

Solution code:
# coding: utf-8
while 1:
    n,m=map(int,input().split())
    if n==0:
        break
    d=list(map(int,input().split()))
    mx=-1
    for i in range(n):
        for j in range(i+1,n):
            if d[i]+d[j]<=m:
                mx=max(d[i]+d[j],mx)
    print(mx if mx!=-1 else 'NONE')

Submission status: Runtime Error

Solution code:
while(True):
    n,m=map(int,input().split(" "))
    if(n==0):
        break
    things = map(int,input().split(" "))
    _max=0
    for i in range(len(things)):
        for j in range(i+1,len(things)):
            ifthings[ i]+things[j] <= m:
                _max = max(things[i]+things[j],_max):
    if _max != None:
        print(_max)
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:
while True:
    n, m = map(int, input().split())
    if n == 0:
        break
    a = sorted(map(int, input().split()))
    index = bisect.bisect_left(a, m) - 1
    if index <= 0:
        print("NONE")
        continue
    ans = 0
    for i in range(index, 0, -1):
        for j in range(i-1, -1, -1):
            if a[i] + a[j] <= m:
                ans = max(ans, a[i] + a[j])
    if ans:
        print(ans)
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:

while true:
    (n,m) = map(int,input().split())
    if n == 0 && m == 0:
        break
    is = list(map(int, input().split()))
    best = max [ is[i] + is[j] if not ( i == j) and is[i] + is[j] <= k else -1 for i in range(0:n) for j in range(0:n) ]
    if(best < 0):
        print("NONE")
    else:
        print(best)

Submission status: Runtime Error

Solution code:

while true:
    (n,m) = map(int,input().split())
    if n == 0 and m == 0:
        break
    is = list(map(int, input().split()))
    best = max [ is[i] + is[j] if not ( i == j) and is[i] + is[j] <= k else -1 for i in range(0:n) for j in range(0:n) ]
    if(best < 0):
        print("NONE")
    else:
        print(best)

Submission status: Runtime Error

Solution code:



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

while n!=0 and m!=0:
    ok=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                ok=1
                max=tmp
    if ok==1:
        print(max)
    else:
        print("NONE")
    n,m=map(int,raw_input().split())

Submission status: Runtime Error

Solution code:

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

while n!=0 and m!=0:
    ok=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                ok=1
                max=tmp
    if ok==1:
        print(max)
    else:
        print("NONE")
    n,m=map(int,raw_input().split())

Submission status: Runtime Error

Solution code:


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

while n>0 and m>0:
    ok=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                ok=1
                max=tmp
    if ok==1:
        print(max)
    else:
        print("NONE")
    n,m=map(int,raw_input().split())

Submission status: Runtime Error

Solution code:
import numpy as np
ans=[]
for i in range(50000):
    n,m=map(int,input().split())
    if n==0 and m==0:
        break
    else:
        A=list(map(int,input().split(' ')))
        sum=0
        max=0
        for j in range(len(A)):
            for k in range(len(A)):
                if j!=k:
                    sum=A[j]+A[k]
                    if sum>=max and sum<=m:
                        max=sum
        if max==0:
            max="NONE"
        ans.append(max)
for i in range(len(ans)):
    if ans[i]!=0:
        print(ans[i])
    else:
        break

Submission status: Runtime Error

Solution code:

max1=[]
ok1=[0]
n,m=map(int,raw_input().split())

while n>0 and m>0:
    ie=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                max=tmp
                ok1.insert(ie,1)
    max1.insert(ie,max)
    n,m=map(int,raw_input().split())
    ie+=1

for i in range(len(max1)-1,-1,-1):
    if ok1[i]==1:
        print(max1[i])
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:
#include<stdio.h>

#define MAX 2000000

int select(int S[],int n,int max){
  int ans=0;
  int temp;
  for(int i = 0;i<n;i++){
    for(int j = i+1;j<n;j++){
      temp = S[i]+S[j];
      if(temp <= max && ans <= temp){
	ans = temp;
      }
    }
  }

  return ans;
}

int main(){

  int n;
  int a;
  int m;
  int k =0;
  int ans[100000];
  int S[MAX];

  while(1){
    scanf("%d",&n);
    scanf("%d",&m);
    if(n==0 && m == 0){
      break;
    }
    for(int i = 0;i<n;i++){
      scanf("%d",&a);
      S[i] = a;
    }

    ans[k] = select(S,n,m);
    k++;
  }


  for(int i = 0;i<k;i++){
    if(ans[i]==0){
      printf("NONE\n");
    }else{
      printf("%d\n",ans[i]);
    }
  }
  return 0;
}

Submission status: Runtime Error

Solution code:
max1=[]
ok1=[0]

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

while n>0 and m>0:
    tmp=0
    ie=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                max=tmp
                ok1.insert(ie,1)
    max1.insert(ie,max)
    n,m=map(int,raw_input().split())
    ie+=1

for i in range(len(max1)-1,-1,-1):
    if ok1[i]==1:
        print(max1[i])
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:
maxs=[]
while(True):
    max=0
    
    b=list(map(int,input().split()))
    n=b[0]
    m=b[1]
    if(n == 0 and m == 0):
        break
    a=list(map(int,input().split()))
    for i in range(n):
        for j in range(i+1,n):
            sum=a[i]+a[j]
            if(sum>max and sum<m):
                max=sum
    
    maxs.append(max)
for i in maxs:
    if maxs[i]==0:
        print("NONE")
    else:
        print(maxs[i])

Submission status: Runtime Error

Solution code:
s=[]

cnt=0

while 1:
    max=0
    n,m =map(int,input().split())
    l=list(map(int,input().split()))
    if n==0 and m==0:
        break

    for i in range(len(l)):
        for j in range(len(l)):
            if max<l[i]+l[j] and i!=j and m>=l[i]+l[j]:
                max=l[i]+l[j]

    if max==0:
        s+=["NONE"]
    else:
        s+=[max]


for x in s:
    print(x)

Submission status: Runtime Error

Solution code:
maxs=[]
while(True):
    max=-1
    
    b=list(map(int,input().split()))
    n=b[0]
    m=b[1]
    if(n == 0 and m == 0):
        break
    a=list(map(int,input().split()))
    for i in range(n):
        for j in range(i+1,n):
            sum=a[i]+a[j]
            if(sum>max and sum=<m):
                max=sum
    maxs.append(max)

for i in maxs:
    if i==-1:
        print("NONE")
    else:
        print(i)

Submission status: Runtime Error

Solution code:

max1=[]
ok1=[0]

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

while n>0 and m>0:
    ok=0
    ie=0
    list=map(int,raw_input().split())
    if list[0]<m:
        max=list[0]
    for j in range(len(list)-1):
        for k in range(1,len(list)):
            tmp=0
            tmp=list[j]+list[k]
            if tmp>max and tmp<=m:
                max=tmp
                ok=1
    max1.insert(ie,max)
    n,m=map(int,raw_input().split())
    ie+=1
    if ok==1:
        ok1.insert(ie,1)
print(ok1)
for i in range(len(max1)-1,-1,-1):
    if ok1[i]==1:
        print(max1[i])
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:

A=map(int,input().split())
n=int(A[0])
m=int(A[1])


max=[]
k=0
while n>0 and m>0:
    B=[0 for i in range(n)]
    B=input().split()
    for i in range(n):
        B[i]=int(B[i])
    ma=0
    for i in range(n):
        for j in range(i+1,n):  
            x=0 
            x=B[i]+B[j]
            if ma<x and x<=m:
                ma=x
    max.append(ma)
    A=input().split()
    n=int(A[0])
    m=int(A[1])
    k+=1
for i in range(k):
    if max[i]>0:
        print(max[i])
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:
A=[0 for i in range(2)]
A=(int,input().split())
n=A[0]
m=A[1]
max=[]
k=0

while n>0 and m>0:
    B=[0 for i in range(n)]
    B=input().split()
    for i in range(n):
        B[i]=int(B[i])
    ma=0
    for i in range(n):
        for j in range(i+1,n):  
            x=0 
            x=B[i]+B[j]
            if ma<x and x<=m:
                ma=x
    max.append(ma)
    A=input().split()
    n=int(A[0])
    m=int(A[1])
    k+=1
for i in range(k):
    if max[i]>0:
        print(max[i])
    else:
        print("NONE")

Submission status: Runtime Error

Solution code:
while True:
    n, m = map(int, raw_input().split())

    if n == 0 and m == 0:
        break

    a = [int(raw_input()) for _ in range(n)]

    ans = -1

    for i in range(n-1):
        for j in range(i+1, n):
            if a[i] + a[j] <= m:
                ans = max(ans, a[i] + a[j])

    print "NONE" if ans == -1 else ans