Problem:


Score : 2000 points

Problem Statement

Snuke has R red balls and B blue balls.
He distributes them into K boxes, so that no box is empty and no two boxes are identical.
Compute the maximum possible value of K.

Formally speaking, let's number the boxes 1 through K.
If Box i contains r_i red balls and b_i blue balls, the following conditions must be satisfied:

For each i (1 \leq i \leq K), r_i > 0 or b_i > 0.

For each i, j (1 \leq i < j \leq K), r_i \neq r_j or b_i \neq b_j.

\sum r_i = R and \sum b_i = B (no balls can be left outside the boxes).

Constraints

1 \leq R, B \leq 10^{9}

Input

Input is given from Standard Input in the following format:

R B

Output

Print the maximum possible value of K.

Sample Input 1

8 3

Sample Output 1

5

The following picture shows one possible way to achieve K = 5:



Sample input:
8 3


Sample output:
5

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= ((i + 1) * (i + 2)) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            big += j - abs(small)
            break
        ans += 1
        big -= i + 1 - j
        if small == 0:
            break
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    pans = 0
    for j in  range(i + 2):
        bsum = 0
        ssum = 0
        for k in range(j, i + 2):
            ssum += k
            bsum += i + 1 - k
            if ssum == small or bsum == big:
                box = k - j + 1
                break
            if ssum > small or bsum > big:
                k -= 1
                box = k - j + 1
                break
        if pans < box:
            pans = box
    ans += pans

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= ((i + 1) * (i + 2)) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            big += j - abs(small)
            break
        ans += 1
        big -= i + 1 - j
        if small == 0:
            break
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    pans = 0
    for j in  range(i + 2):
        bsum = 0
        ssum = 0
        for k in range(j, i + 2):
            ssum += k
            bsum += i + 1 - k
            if ssum == small or bsum == big:
                box = k - j + 1
                break
            if ssum > small or bsum > big:
                k -= 1
                box = k - j + 1
                break
        if pans < box:
            pans = box
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= ((i + 1) * (i + 2)) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            big += j - abs(small)
            break
        ans += 1
        big -= i + 1 - j
        if small == 0:
            break
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            big += j - abs(small)
            break
        ans += 1
        big -= i + 1 - j
        if small == 0:
            break
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            break
        ans += 1
        big -= i + 1 - j
        if small == 0:
            break
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            break
        ans += 1
        if small == 0:
            break
        big -= i + 1 - j
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            break
        ans += 1
        if small == 0:
            break
        big -= i + 1 - j
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            break
        ans += 1
        if small == 0:
            break
    big -= (i + 1) * (i + 2) / 2
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, b = [int(x) for x in stdin.readline().rstrip().split()]

i = 0
ans = 0
while True:
    i += 1
    n = (i * (i + 1))/2
    r -= n
    b -= n
    if r < 0 or b < 0:
        i -= 1
        r += n
        b += n
        break
    ans += i + 1
    if r == 0 or b == 0:
        break

if r < b:
    big = b
    small = r
else:
    big = r
    small = b

if big >= (i + 1) * (i + 2) / 2:
    for j in range(i + 2):
        small -= j
        if small < 0:
            break
        ans += 1
        if small == 0:
            break
    big -= (i + 1) * (i + 2) / 2
    i += 1
    while True:
        i += 1
        big -= i
        if big < 0:
            break
        ans += 1
        if big == 0:
            break
else:
    ans += (big + small) // (i + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0 and k >= 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
        k -= 1
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""
#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
def f(n):
    return n*(n+1)//2

def f2(n,i):
    return (n+1)*i-f(n)
r,b = LI()
ans = 0
for i in range(1,1500000):
    r -= f(i)
    b -= f(i)
    if r < 0 or b < 0:
        r += f(i)
        b += f(i)
        break
    ans += i+1
a = max(r,b)
c = min(r,b)
while a+c >= i:
    r = int((-1+(1+8*c)**(1/2))/2)
    k = f(r)
    a -= i*(r+1)-k
    c -= k
    key = r+1
    if a < 0:
        l = int((2*i-1-((2*i+1)**2+8*a)**(1/2))/2)
        k2 = f2(l,i)
        a += k2
        c += i-k2
        key -= l+1
    r += 1
    while c >= r and a >= i-r:
        c -= r
        a -= i-r
        key += 1
        r += 1
    ans += key
    i += 1
    d = max(a,c)
    e = min(a,c)
    a = d
    c = e
print(ans)

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0 and k >= 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
        k -= 1
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""
#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
def f(n):
    return n*(n+1)//2
r,b = LI()
ans = 0
for i in range(1,1500000):
    r -= f(i)
    b -= f(i)
    if r < 0 or b < 0:
        r += f(i)
        b += f(i)
        break
    ans += i+1
a = max(r,b)
c = min(r,b)
while a+c >= i:
    r = int((-1+(1+8*c)**(1/2))/2)
    k = f(r)
    a -= i*(r+1)-k
    c -= k
    key = r
    if a < 0:
        l = int((2*i-1+((2*i+1)**2+8*a)**(1/2))/2)
        k2 = f(l)
        a += (l+1)*i-k
        c += k
        key -= i-l+2
    r += 1
    while c >= r and a >= i-r:
        c -= r
        a -= i-r
        key += 1
        r += 1
    ans += key
    i += 1
    d = max(a,c)
    e = min(a,c)
    a = d
    c = e
print(ans)

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""

#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
def f(n):
    return n*(n+1)//2
r,b = LI()
ans = 0
for i in range(1,1500000):
    r -= f(i)
    b -= f(i)
    if r < 0 or b < 0:
        r += f(i)
        b += f(i)
        break
    ans += i+1
a = max(r,b)
c = min(r,b)
while a+c >= i:
    for k in range(i+1):
        if a >= i-k and c >= k:
            a -= i-k
            c -= k
            ans += 1
    i += 1
    d = max(a,c)
    e = min(a,c)
    a = d
    c = e
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""

#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
r,b = LI()
ansr = []
ansb = []
flagr = True
flagb = True
for i in range(150000):
    r -= i
    b -= i
    if flagr:
        if r > 0:
            ansr.append(i)
        else:
            flagr = False
            ansr.append(r+i)
    if flagb:
        if b > 0:
            ansb.append(i)
        else:
            flagb = False
            ansb.append(b+i)
if ansr[-1] == len(ansr)-1 and ansb[-1] == len(ansb)-1:
    ans = len(ansr)+len(ansb)-2
else:
    ans = len(ansr)+len(ansb)-3
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""

#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
r,b = LI()
ansr = []
ansb = []
sum = 0
flagr = True
flagb = True
for i in range(100000):
    r -= i
    b -= i
    if flagr:
        if r > 0:
            ansr.append(i)
        else:
            flagr = False
            ansr.append(r+i)
    if flagb:
        if b > 0:
            ansb.append(i)
        else:
            flagb = False
            ansb.append(b+i)

if ansr[-1] == len(ansr)-1 and ansb[-1] == len(ansb)-1:
    ans = len(ansr)+len(ansb)-2
else:
    ans = len(ansr)+len(ansb)-3
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random
def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return list(input())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
n,k = LI()
a = LI()
if min(a) <= k:
    print(-1)
else:
    ans = []
    while max(a) > 0:
        for i in range(n):
            if a[i] > 0:
                a[i] -= 1
                ans.append(i+1)
    print(len(ans))
    for i in range(len(ans)-1):
        print(ans[i],end = " ")
    print(ans[-1])
"""

#B
"""
n = I()
q = I()
"""
#C1

#C2

#D
r,b = LI()
ansr = []
ansb = []
sum = 0
flagr = True
flagb = True
for i in range(100000):
    r -= i
    b -= i
    if flagr:
        if r > 0:
            ansr.append(i)
        else:
            flagr = False
            ansr.append(r+i)
    if flagb:
        if b > 0:
            ansb.append(i)
        else:
            flagb = False
            ansb.append(b+i)
ans = len(ansr)+len(ansb)-2
if ansr[-1] != len(ansr)-1:
    ans -= 1
if ansb[-1] != len(ansb)-1:
    ans -= 1
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T