Problem:


Score : 800 points

Problem Statement

Takahashi and Aoki will take N exams numbered 1 to N.
They have decided to compete in these exams.
The winner will be determined as follows:

For each exam i, Takahashi decides its importance c_i, which must be an integer between l_i and u_i (inclusive).

Let A be \sum_{i=1}^{N} c_i \times (Takahashi's score on Exam i), and B be \sum_{i=1}^{N} c_i \times (Aoki's score on Exam i). Takahashi wins if A \geq B, and Aoki wins if A < B.

Takahashi knows that Aoki will score b_i on Exam i, with his supernatural power.

Takahashi himself, on the other hand, will score 0 on all the exams without studying more. For each hour of study, he can increase his score on some exam by 1. (He can only study for an integer number of hours.)
However, he cannot score more than X on an exam, since the perfect score for all the exams is X.

Print the minimum number of study hours required for Takahashi to win.

Constraints

1 \leq N \leq 10^5

1 \leq X \leq 10^5

0 \leq b_i \leq X (1 \leq i \leq N)

1 \leq l_i \leq u_i \leq 10^5 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
b_1 l_1 u_1
b_2 l_2 u_2
:
b_N l_N u_N

Output

Print the minimum number of study hours required for Takahashi to win.

Sample Input 1

2 100
85 2 3
60 1 1

Sample Output 1

115

One optimal strategy is as follows:

Choose c_1 = 3, c_2 = 1.

Study to score 100 on Exam 1 and 15 on Exam 2.

Then, A = 3 \times 100 + 1 \times 15 = 315, B = 3 \times 85 + 1 \times 60 = 315 and Takahashi will win.

Sample Input 2

2 100
85 2 3
60 10 10

Sample Output 2

77

Sample Input 3

1 100000
31415 2718 2818

Sample Output 3

31415

Sample Input 4

10 1000
451 4593 6263
324 310 6991
378 1431 7068
71 1757 9218
204 3676 4328
840 6221 9080
684 1545 8511
709 5467 8674
862 6504 9835
283 4965 9980

Sample Output 4

2540



Sample input:
2 100
85 2 3
60 1 1


Sample output:
115

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
def inpl(): return list(map(int, input().split()))
import sys
input = sys.stdin.readline
N, X = inpl()
B = [0]*N
L = [0]*N
U = [0]*N

S = 0
K = []
for i in range(N):
    B[i], L[i], U[i] = inpl()
    S += B[i] * L[i]
    K.append(((X-B[i])*U[i], i))

K = sorted(K, key=itemgetter(0), reverse=True)

OK = N*X
NG = -1

G = [0]*(N+1)
G[0] += S
for i in range(N):
    g, j = K[i]
    G[i+1] = G[i] - B[j]*L[j] - g

def check(m):
    div, mod = divmod(m, X)
    T = G[div]*1

    for k in range(div, N):
        _, j = K[k]
        if mod <= B[j]:
            if T <= mod * L[j]:
                return True
        else:
            if T <= B[j]*L[j] + (mod - B[j])*U[j]:
                return True
    else:
        return False

while abs(OK-NG) > 1:
    mid = (OK+NG)//2
    if check(mid):
        OK = mid
    else:
        NG = mid

print(OK)

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.readline

N, X = map(int, input().split())
blu = [list(map(int, input().split())) for _ in range(N)]

def mydivup(a, b): return (a + b - 1) // b

blu.sort(key=lambda x:  -(x[2] * (X - x[0]) + x[1] * x[0]) )

loss_tab = [(bi * li) for bi, li, ui in blu]

ans = 0
loss_tmp = sum(loss_tab)
for i, (bi, li, ui) in enumerate(blu):
    tak_best = (X - bi) * ui
    loss_tmp -= loss_tab[i]
    if tak_best >= loss_tmp:
        i_end = i
        loss_tmp += loss_tab[i]
        break
    else:
        ans += X
        loss_tmp -= tak_best
        # print("#", (bi, li, ui), tak_best, X, loss_tmp)

ans_tmp = X
for i, (bi, li, ui) in enumerate(blu):
    if i < i_end: continue
    ans_tmp2 = bi + mydivup(loss_tmp - loss_tab[i], ui)
    # print("##", (bi, li, ui), ans_tmp2)
    if ans_tmp > ans_tmp2:
        ans_tmp = ans_tmp2

ans += ans_tmp
# print("#", sum([bi for bi, li, ui in blu]))
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil

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

tlist = []
for i in range(n):
    b,l,u = map(int,input().split())
    tlist.append([b,l,u,i])

ans = 0

cp = []
for i in tlist:
    cp.append(i[2]*x-(i[2]-i[1])*i[0])

leastline = 0
for i in tlist:
    leastline += i[0]*i[1]

cplist = sorted(tlist, key=lambda x: cp[x[3]])[::-1]

nowleastline = leastline
lenc = len(cplist)
for _ in range(lenc):
    i = cplist[0]
    leastline = nowleastline
    nowleastline = nowleastline + (i[2] - i[1])*i[0] - x*i[2]
    if nowleastline <= 0:
        break
    else:
        ans += x
        del cplist[0]

tmp1 = ceil((leastline+(cplist[0][2] - cplist[0][1])*cplist[0][0])/cplist[0][2])

cplist = sorted(cplist, key=lambda x: x[1])[::-1]
tmp2 = ceil(leastline/cplist[0][1])

cplist = sorted(cplist, key=lambda x: x[0])

tmp3 = x
for i in cplist:
    if i[0] > tmp1:
        break
    tmp3 = min(tmp3,ceil((leastline+(i[2] - i[1])*i[0])/i[2]))

ans += min(tmp1,tmp2,tmp3)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

ans = 0

N, X = map(int,input().split())
inList = []
temp_lost = 0

for i in range(N):
  b, l, u =  map(int,input().split())
  inList.append([b, l, u, (X-b)*u + b*l])
  temp_lost -= b*l

inList.sort(key=lambda x: -x[3])
  
for i in range(N):
  if(temp_lost <0 and (temp_lost+inList[i][3]) > 0 ):
    
    ans_cand = X
    for j in range(i, N):
      if( (-temp_lost) > inList[i][0]*inList[i][1]):
      # win is needed
        ans_cand = min(ans_cand, math.ceil((0-temp_lost)/inList[i][2]))
      else:
      # lost is allowed
        ans_cand = min(ans_cand, math.ceil((0-temp_lost)/inList[i][1]))
    ans += ans_cand

    """
    if( (-temp_lost) > inList[i][0]*inList[i][1]):
      # win is needed
      ans += math.ceil((0-temp_lost)/inList[i][2])
    else:
      # lost is allowed
      ans += math.ceil((0-temp_lost)/inList[i][1])
    """
    break
    
  else:
    ans += X
    temp_lost += inList[i][3]

print(ans)

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
base_score  =0
my_score = 0
ans = 0
b = []
l = []
u = []
sco = []
for i in range(n):
    ba,la,ua = map(int,input().split())
    b.append(ba)
    l.append(la)
    u.append(ua)
    sco.append([ba,la,ua])
    base_score += ba*la

#print(base_score)
for i in range(n):
    sco[i].append((x*sco[i][2])-(sco[i][0]*(sco[i][2]-sco[i][1])))

sco = sorted(sco, key = lambda a: a[3] )

while my_score<=base_score:
    k = base_score - my_score
    if k <= sco[-1][3]:
        i = -1
        
        while (sco[i][3] >=k and -i <len(sco)):
            i = i-1
        nom = sco[-1][1]
        for s in range(i,-1):
            nom = max(sco[s][1],nom)
        my_score = base_score+1
        ans += ((k-1)//nom)+1

    else:
        my_score += sco[-1][3]
        ans += x
        sco.pop(-1)


print(ans)

Submission status: Wrong Answer

Solution code:
import math
INF = 10**18
n, x = [int(item) for item in input().split()]
blu = [[int(item) for item in input().split()] for _ in range(n)]

manten_point = []
loosing_point = 0 
for i, (b, l, u) in enumerate(blu):
    manten_point.append([(x-b) * u + b * l, i])
    loosing_point += b * l 

manten_point.sort(reverse=True)

manten_set = set() 
for p, index in manten_point:
    if loosing_point < p:
        break
    loosing_point -= p
    manten_set.add(index)

r_time = INF 
# R senryaku
for i, (b, l, u) in enumerate(blu):
    if i in manten_set:
        continue
    if (x-b) * u + b * l >= loosing_point:
        val = int(math.ceil((loosing_point - b*l) / u)) + b
        if val <= b:
            val = INF
    else:
        val = INF
    r_time = min(r_time, val)

# L senryaku
l_rate_set = [0] * (10**5 + 1)
l_time = 0
for i, (b, l, u) in enumerate(blu):
    if i in manten_set:
        continue
    l_rate_set[l] += b
for i in range(10**5, -1, -1):
    if loosing_point <= l_rate_set[i] * i:
        l_time += int(math.ceil(loosing_point / i))
        break
    else:
        loosing_point -= l_rate_set[i] * i
        l_time += l_rate_set[i]

print(min(l_time, r_time) + len(manten_set) * x)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
d=[list(map(int,input().split()))for i in range(n)]
b=0
for i in d:
    b+=i[0]*i[1]
s=sorted(d,key=lambda x:(x[0]-c)*x[2])
g=0
e=0
for i in range(n):
    if b+(s[i][2]-s[i][1])*s[i][0]>g+s[i][2]*c:
        b+=(s[i][2]-s[i][1])*s[i][0]
        g+=s[i][2]*c
        e+=c
    else:
        r,k=e,e
        tom=g
        t=sorted(s[i:],key=lambda x:-x[1])
        for j in range(len(t)):
            if b>g+t[j][0]*t[j][1]:
                g+=t[j][0]*t[j][1]
                r+=t[j][0]
            else:
                r+=(b-g-1)//t[j][1]+1
                print(min(k+(b+(s[i][2]-s[i][1])*s[i][0]-tom-1)//s[i][2]+1,r))
                exit()

Submission status: Wrong Answer

Solution code:
import math
n, x = map(int, input().split())

l = [list(map(int, input().split())) for _ in range(n)]
lose = sum(map(lambda t: t[0] * t[1], l))
needed = 0
for bi, li, ui in sorted(l, key=lambda t: t[1], reverse=True):
#    print('lose =', lose, ', needed =', needed)
#    print((bi, li, ui))
    if bi * li <= lose:
        needed += bi
        lose -= bi * li
        if (x - bi) * ui <= lose:
            needed += (x - bi)
            lose -= (x - bi) * ui
        else:
            needed += math.ceil(lose/ui)
            break
    else:
        needed += math.ceil(lose/li)
        break
    if lose == 0:
        needed += 1
        break
print(needed)

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
#v = list(map(int,input().split()))
tlist = []
for i in range(n):
    b,l,u = map(int,input().split())
    tlist.append([b,l,u,i])

if x == 0:
  	print(0)

Submission status: Wrong Answer

Solution code:
import math
n,x=map(int,input().split())
bs=[]
lu=[]
for i in range(n):
    b,l,u=map(int,input().split())
    bs.append(b)
    lu.append([l,u])
r=[[0,0] for i in range(n)]
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    r[i][0]=lu[i][1]*(x-bs[i])
    r[i][1]=i
r.sort(reverse=True)
s=0
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    s+=bs[i]*lu[i][0]
sub=0
As=0
point=0
l=[]
buf=x+1
for i in range(n):
    As+=r[i][0]
    s-=bs[r[i][1]]*lu[r[i][1]][0]
    point+=x
    if(As>s):
        As-=r[i][0]
        s+=bs[r[i][1]]*lu[r[i][1]][0]
        sub=s-As
        #print(sub)
        point-=x
        for j in range(i,n):
            f=r[j][1]
            y=math.ceil(sub/lu[f][0])
            q=math.ceil((sub+(lu[f][1]-lu[f][0])*bs[f])/lu[f][1])
            #print(y)
            buf=min([buf,y,q]) 
        point+=buf
        break
print(point)

Submission status: Wrong Answer

Solution code:
from heapq import *
n,x=map(int,input().split())
blu=[list(map(int,input().split())) for i in range(n)]
ans=0
s=[]
heapify(s)
t=0
for i,j in enumerate(blu):
    b,l,u=j
    t+=b*l
    heappush(s,(-(u*(x-b)+l*b),i))
while 1:
    i=heappop(s)
    a,b=i
    if(t+a<=0):
        break
    else:
        t+=a
        blu[b]=0
        ans+=x
fa=x
for i in blu:
    if(i!=0):
        b,l,u=i
        if(t>l*b):
            fa=min(fa,b-((l*b-t)//u))
        else:
            fa=min(fa,-(-t//l))
print(fa+ans)

Submission status: Wrong Answer

Solution code:

import bisect
import sys

input = sys.stdin.readline

N,X =[int(i)for i in input().split()]
lis = [[int(i)for i in input().split()]for _ in range(N)]#BLU
wa = 0
lis2 = []
index = []
for (b,l,u) in lis:
    G = (X-b)*u
    L = l*b
    n = bisect.bisect_left(index,G+L)
    lis2.insert(n,[G,L,b,l,u])
    index.insert(n,G+L)
    wa += L
ans = 0
for _ in range(N):
    lisp = lis2.pop(-1)
    if wa > lisp[0]+lisp[1]:
        wa -= lisp[0] +lisp[1]
        ans += X
    else:
        foo = (wa+lisp[3]-1)//lisp[3]
        if foo > lisp[2]:
            foo = X +1
        bar = (wa+(lisp[4]-lisp[3])*lisp[2])//lisp[4]
        ans += min(foo,bar)
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, X = map(int, input().split())
BLU = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
B, L, U = map(list, zip(*BLU))
C = [(u-l)*b for b, l, u in BLU]
D = [(X*u - c, u, c, l) for u, c, l in zip(U, C, L)]
D.sort(reverse = True)
SL = sum(l*b for l, b in zip(L, B))
SS = 0
for i, (d, _, _, _) in enumerate(D):
    SS += d
    if SS >= SL:
        break
SSb = SS - d
lans = i*X
m = 10**10
for _, u, c, l in D[i:]:
    m = min(m, -((SSb - SL)//l))
lans += m

ans = (i+1) * X
m = 0
for _, u, _, _ in D[:i+1]:
    m = max(m, (SS - SL)//u)
print(min(lans, ans - m))

Submission status: Wrong Answer

Solution code:
from heapq import *
n,x=map(int,input().split())
blu=[list(map(int,input().split())) for i in range(n)]
ans=0
s=[]
t=0
for i,j in enumerate(blu):
    b,l,u=j
    t+=b*l
    heappush(s,(-(u*(x-b)+l*b),i))
while 1:
    i=heappop(s)
    a,b=i
    if(t+a<=0):
        break
    else:
        t+=a
        blu[b]=0
        ans+=x
fa=x
for i in blu:
    if(i!=0):
        b,l,u=i
        if(t>l*b):
            fa=min(fa,b-((l*b-t)//u))
        else:
            fa=min(fa,-(-t//l))
print(fa+ans)

Submission status: Wrong Answer

Solution code:
import math
n,x=map(int,input().split())
bs=[]
lu=[]
for i in range(n):
    b,l,u=map(int,input().split())
    bs.append(b)
    lu.append([l,u])
r=[[0,0] for i in range(n)]
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    r[i][0]=lu[i][1]*(x-bs[i])
    r[i][1]=i
r.sort(reverse=True)
s=0
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    s+=bs[i]*lu[i][0]
sub=0
As=0
point=0
l=[]
buf=x+1
for i in range(n):
    As+=r[i][0]
    s-=bs[r[i][1]]*lu[r[i][1]][0]
    point+=x
    if(As>s):
        As-=r[i][0]
        s+=bs[r[i][1]]*lu[r[i][1]][0]
        sub=s-As
        #print(sub)
        point-=x
        for j in range(i,n):
            f=r[j][1]
            y=math.ceil(sub/lu[f][0])
            #print(y)
            if(buf>y):
                buf=y  
        point+=buf
        break
print(point)

Submission status: Wrong Answer

Solution code:
import sys
N, X = map(int, input().split())
BLU = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
B, L, U = map(list, zip(*BLU))
C = [(u-l)*b for b, l, u in BLU]
D = [(X*u - c, u, c, l) for u, c, l in zip(U, C, L)]
D.sort(reverse = True)
SL = sum(l*b for l, b in zip(L, B))
SS = 0
for i, (d, _, _, _) in enumerate(D):
    SS += d
    if SS >= SL:
        break
SSb = SS - d
lans = i*X
m = 10**10
for _, u, c, l in D[i:]:
    m = min(m, -(-(c + SL - SSb)//u), -((SSb - SL)//l))
lans += m

ans = (i+1) * X
m = 0
for _, u, _, _ in D[:i+1]:
    m = max(m, -(-(SS - SL)//u))
print(min(lans, ans - m))

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
base_score  =0
my_score = 0
ans = 0
b = []
l = []
u = []
sco = []
for i in range(n):
    ba,la,ua = map(int,input().split())
    b.append(ba)
    l.append(la)
    u.append(ua)
    sco.append([ba,la,ua])
    base_score += ba*la

#print(base_score)
for i in range(n):
    sco[i].append((x*sco[i][2])-(sco[i][0]*(sco[i][2]-sco[i][1])))

sco = sorted(sco, key = lambda a: a[3] )
#print(sco)

while my_score<=base_score:
    if (base_score - my_score) <= sco[-1][3]:
        k = base_score - my_score
        #print(k)
        my_score = base_score+1
        ans += ((k-1)//sco[-1][1])+1

    else:
        my_score += sco[-1][3]
        ans += x
        sco.pop(-1)
        #print(sco)
        #print(base_score,my_score,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n,x=map(int,input().split())
bs=[]
lu=[]
for i in range(n):
    b,l,u=map(int,input().split())
    bs.append(b)
    lu.append([l,u])
r=[[0,0] for i in range(n)]
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    r[i][0]=lu[i][1]*(x-bs[i])
    r[i][1]=i
r.sort(reverse=True)
s=0
for i in range(n):
    #print(lu[i][1],(x-bs[i]))
    s+=bs[i]*lu[i][0]
sub=0
As=0
point=0
l=[]
buf=x+1
for i in range(n):
    As+=r[i][0]
    s-=bs[r[i][1]]*lu[r[i][1]][0]
    point+=x
    if(As>s):
        As-=r[i][0]
        s+=bs[r[i][1]]*lu[r[i][1]][0]
        sub=s-As
        #print(sub)
        point-=x
        for j in range(i,n):
            f=r[j][1]
            y=math.ceil(sub/lu[f][0])
            print(y)
            if(buf>y):
                buf=y  
        point+=buf
        break
print(point)

Submission status: Wrong Answer

Solution code:
#C問題
import math
N,X = map(int,input().split())
BLU = [list(map(int,input().split())) for i in range(N)]
XU = []
for i in range(N):
    kari = (X-BLU[i][0])*BLU[i][2]
    XU.append([kari,i])
XU.sort(reverse=True)
BL = 0
for i in range(N):
    BL+=(BLU[i][0]*BLU[i][1])

xu = 0
ans = 0
for i in range(N):
    xu+=XU[i][0]
    BL-=BLU[XU[i][1]][0]*BLU[XU[i][1]][1]
    if xu >= BL:
        if BL != 0:
            ans+=math.ceil(BL/BLU[XU[i][1]][2])+BLU[XU[i][1]][0]
        else:
            xu-=XU[i][0]
            BL+=BLU[XU[i][1]][0]*BLU[XU[i][1]][1]
            ans+=math.ceil((BL-xu)/BLU[XU[i][1]][1])
        break
    else:
        ans+=X

print(ans)

Submission status: Wrong Answer

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

test_list = []

aoki_target = 0

for i in range(N):
    b, l, u = map(int, input().split())
    test_list.append([(u * X - u * b), b, l, u])
    aoki_target += b * l

test_list = sorted(test_list, key=lambda x: x[0], reverse=True)

ans = 0
takahashi = 0

for test in test_list:
    b = test[1]
    l = test[2]
    u = test[3]
    aoki_target += (u - l) * b
    if takahashi + u * X < aoki_target:
        ans += X
        takahashi += X * u
    elif takahashi + u * X == aoki_target:
        ans += X
    else:
        ans += (aoki_target - takahashi + u - 1) // u
        break

print(ans)

Submission status: Wrong Answer

Solution code:
N,X = map(int,input().split())
sum_b = 0
rate = []
score = []
delta = []
for i in range(N):
	b,l,u = map(int,input().split())
	rate.append((l,u))
	score.append(b)
	delta.append((u*(X-b) + l*b, i))
	sum_b += b

delta.sort()
delta = list(reversed(delta))
l,r = 0,sum_b
while r - l > 1:
	p = (l+r)//2
	used = [False for i in range(N)]
	A = 0
	B = 0
	for i in range(p//X):
		idx = delta[i][1]
		used[idx] = True
		A += rate[idx][1] * X
		B += rate[idx][1] * score[idx]
	rest = p % X
	if rest > 0:
		d = []
		for i in range(N):
			if not used[i]:
				if rest > score[i]:
					d.append((rate[i][1]*(rest-score[i]) + rate[i][0]*score[i], i, rate[i][1]))
				else:
					d.append((rate[i][0]*(rest-score[i]) + rate[i][0]*score[i], i, rate[i][0]))
		d.sort()
		tmp = d.pop()
		used[tmp[1]] = True
		A += tmp[2] * rest
		B += tmp[2] * score[tmp[1]]
	for i in range(N):
		if not used[i]:
			B += rate[i][0]*score[i]
	if A >= B:
		r = p
	else:
		l = p

print(r)

Submission status: Wrong Answer

Solution code:
import sys
N, X = map(int, input().split())
BLU = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
B, L, U = map(list, zip(*BLU))
C = [(u-l)*b for b, l, u in BLU]
D = [(X*u - c, u, c, l) for u, c, l in zip(U, C, L)]
D.sort(reverse = True)
SL = sum(l*b for l, b in zip(L, B))
SS = 0
for i, (d, _, _, _) in enumerate(D):
    SS += d
    if SS >= SL:
        break
SSb = SS - d
lans = i*X
m = 10**10
for _, u, c, l in D[i:]:
    m = min(m, -(-(c + SL - SSb)//u), -((SSb - SL)//l))
lans += m

ans = (i+1) * X
m = 0
for _, u, _, _ in D[:i+1]:
    m = max(m, -(-(SS - SL))//u)
print(min(lans, ans - m))

Submission status: Wrong Answer

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

pt = []
for i, (b, l, u) in enumerate(map(int, input().split()) for i in range(1, N+1)):
    pt.append([(X - b) * u, l*b, b, u])

pt = sorted(sorted(pt, key=lambda x: x[1], reverse=True), key=lambda x: x[0], reverse=True)

for i in range(len(pt)-2, -1, -1):
    pt[i][1] += pt[i+1][1]

w = 0
t = 0
for i in range(len(pt)):
    w += pt[i][0]
    t += X
    sa = w - (pt[i+1][1] if i+1 < len(pt) else 0)
    if sa > 0:
        break

print(t - sa // pt[i][3])

Submission status: Wrong Answer

Solution code:
import math
n, x = map(int, input().split())

tests = []
for nn in range(n):
  b, l, u = map(int, input().split())
  tests.append({'b':b , 'l':l , 'u':u})

tests = sorted(tests, key=lambda c: (x-c['b'])*c['u'])

#print(tests)
b_total = 0

for test in tests:
  b_total += test['l'] * test['b']

a_total = 0

total_time = 0
while True:
  test = tests.pop()

  # update b score
  b, l, u = test['b'], test['l'], test['u']
  b_total = b_total + (u - l) * b

  a_total += x * u

  if a_total >= b_total:
    a_total -= x * u
    b_total -= (u - l) * b
    tests.append(test)
    break
  total_time += x


r = b_total - a_total
rest_times = []
for test in tests:
  b, l, u = test['b'], test['l'], test['u']
  rest_l = math.ceil(r / l)
  rest_r = math.ceil(((u-l)*b + r) / u)
  rest_times.append( rest_l )
  rest_times.append( rest_r )
#print(rest_times)

total_time += min(rest_times)
print(total_time)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

N, X = map(int, input().split())

b_list = [0]*N
l_list = [0]*N
u_list = [0]*N

for i in range(N):
    b, l, u = map(int, input().split())
    b_list[i] = b
    l_list[i] = l
    u_list[i] = u

# lose weight
lose_weight = sum([b_list[i]*l_list[i] for i in range(N)])

# win weight
win_weight = [(X - b_list[i])*u_list[i]+b_list[i]*l_list[i] for i in range(N)]

h = []
for i in range(N):
    heappush(h, (-win_weight[i], i))

used_flg = [0]*N
res = 0
while True:
    win, i = heappop(h)
    if win + lose_weight < 0:
        break
    elif win + lose_weight == 0:
        res += X
        used_flg[i] = 1
        lose_weight = 0
        break
    else:
        res += X
        used_flg[i] = 1
        lose_weight += win

r_min = X
i_min = -1
for i in range(N):
    if used_flg[i] == 0:
        if lose_weight > b_list[i]*l_list[i]:
            p = lose_weight - b_list[i]*l_list[i]
            q = u_list[i]
            if p % q == 0:
                r = p // q + b_list[i]
            else:
                r = p // q + b_list[i] + 1
            if r < r_min:
                r_min = r
                i_min = i
        else:
            p = lose_weight
            q = l_list[i]
            if p % q == 0:
                r = p // q
            else:
                r = p // q + 1
            if r < r_min:
                r_min = r
                i_min = i

res_temp = res + r_min

# post process
if i_min != -1:
    used_flg[i_min] = 1
    
post_list = [i for i in range(N) if used_flg[i] == 1]
win_total = sum([win_weight[i] for i in post_list])
lose_weight_org = sum([b_list[i]*l_list[i] for i in range(N)])

for i in post_list:
    lose_weight = lose_weight_org - (win_total - win_weight[i])
    
    if lose_weight > b_list[i] * l_list[i]:
        p = lose_weight - b_list[i] * l_list[i]
        q = u_list[i]
        if p % q == 0:
            r = p // q + b_list[i]
        else:
            r = p // q + b_list[i] + 1
        if r < r_min:
            r_min = r
            i_min = i
    else:
        p = lose_weight
        q = l_list[i]
        if p % q == 0:
            r = p // q
        else:
            r = p // q + 1
        if r < r_min:
            r_min = r
            i_min = i

print(res + r_min)

Submission status: Wrong Answer

Solution code:
from math import ceil

n,x = map(int,input().split())
#v = list(map(int,input().split()))
tlist = []
for i in range(n):
    b,l,u = map(int,input().split())
    tlist.append([b,l,u,i])

ans = 0

cp = []
#calc コスパ
for i in tlist:
    cp.append(i[2]*x-(i[2]-i[1])*i[0])

leastline = 0
for i in tlist:
    leastline += i[0]*i[1]

#tlistをコスパで並び替え
#cptlistord = sorted(range(len(cp)) key=lambda x: cp[x])
cplist = sorted(tlist, key=lambda x: cp[x[3]])[::-1]

#print(cplist)
nowleastline = leastline
lenc = len(cplist)
for _ in range(lenc):
    i = cplist[0]
    leastline = nowleastline
    nowleastline = nowleastline + (i[2] - i[1])*i[0] - x*i[2]
    if nowleastline <= 0:
        break
    else:
        ans += x
        del cplist[0]

hokan1 = cplist[0]
tmp1 = ceil((leastline+(hokan1[2] - hokan1[1])*hokan1[0])/hokan1[2])

cplist = sorted(cplist, key=lambda x: x[1])[::-1]
hokan2 = cplist[0]
tmp2 = ceil(leastline/hokan2[1])

cplist = sorted(cplist, key=lambda x: x[0])

tmp3 = x
for i in cplist:
    if i[0] >= tmp1:
        break
    tmp3 = min(tmp3,ceil((leastline+(i[2] - i[1])*i[0])/i[2]))


#print(ceil((leastline+(hokan1[2] - hokan1[1])*hokan1[0])/hokan1[2]))
#print(ceil(leastline/hokan2[1]))

#ans += min(ceil((leastline+(hokan1[2] - hokan1[1])*hokan1[0])/hokan1[2]),ceil(leastline/hokan2[1]))

ans += min(tmp1,tmp2,tmp3)

print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
def inpl(): return list(map(int, input().split()))
import sys
input = sys.stdin.readline
N, X = inpl()
B = [0]*N
L = [0]*N
U = [0]*N

S = 0
K = []
for i in range(N):
    B[i], L[i], U[i] = inpl()
    S += B[i] * L[i]
    K.append(((X-B[i])*U[i], i))

K = sorted(K, key=itemgetter(0), reverse=True)

OK = N*X
NG = -1

G = [0]*(N+1)
G[0] += S
for i in range(N):
    g, j = K[i]
    G[i+1] = G[i] - B[j]*L[j] - g

def check(m):
    i = -1
    div, mod = divmod(m, X)
    T = G[div]*1

    for k in range(div, N):
        _, j = K[k]
        if mod <= B[j]:
            if T <= mod * L[j]:
                return True
        else:
            if T <= B[j]*L[j] + (mod - B[j])*U[j]:
                return True
    else:
        return False

while abs(OK-NG) > 1:
    mid = (OK+NG)//2
    if check(mid):
        OK = mid
    else:
        NG = mid

print(OK)

Submission status: Wrong Answer

Solution code:
import math
N,X = map(int, raw_input().split())
blu = []
summin = 0
count = 0
flag = 0
ss = []
for i in range(N):
	blu2 = map(int, raw_input().split())
	blu2.append(X*blu2[2]-blu2[0]*(blu2[2]-blu2[1]))
	summin += blu2[0]*blu2[1]
	blu.append(blu2)
blu = sorted(blu,key = lambda tup:(-tup[3]))
for temp in blu:
	if flag == 0:
		if(temp[3] < summin):
			summin -= temp[3]
			count += X
		else:
			flag = 1
			ss.append(int(min(math.floor(summin/temp[1]),math.floor((summin+temp[0]*(temp[2]-temp[1]))/temp[2])))+1)
	else:
		ss.append(int(min(math.floor(summin/temp[1]),math.floor((summin+temp[0]*(temp[2]-temp[1]))/temp[2])))+1)
if len(ss) != 0:
	print(count + min(ss))
else:
	print(count)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
N, X = map(int, input().split())
S = [list(map(int, input().split())) for i in range(N)]
A = []
aoki = 0
for i, (b, l, u) in enumerate(S):
    A.append([(X-b)*u + (b)*l, i])
    aoki += b*l
A.sort(reverse=True, key=itemgetter(0))
ans = 0
tak = 0
for iaaaa, (a, i) in enumerate(A):
    if aoki >= a:
        aoki -= a
        ans += X
        if aoki==0:
            print(ans)
            exit()
    else:
        break
mi = float("inf")
aoki_ = aoki
for j in range(iaaaa, N):
    a, i = A[j]
    b, l, u = S[i]
    aoki = aoki_
    an, m = divmod(aoki, l)
    if m!=0:
        an += 1
    mi = min(mi, an)
    aoki += (u-l) * b
    an, m = divmod(aoki, u)
    if m!=0:
        an += 1
    mi = min(mi, an)
ans_ = ans + mi

a, i = A[iaaaa]
aoki_ -= a
aoki_ = -aoki
anss = (iaaaa+1)*X
ma = 0
for a, i in A[:iaaaa]:
    b, l, u = S[i]
    aoki = aoki_

    an, m = divmod(aoki, u)
    ma = max(ma, an)

    an = X-b
    aoki -= (u-l)*an

    an_, m = divmod(aoki, l)
    an += an_
    ma = max(ma, an)


print(min(ans_, anss-ma))

Submission status: Wrong Answer

Solution code:
from heapq import *
n,x=map(int,input().split())
blu=[list(map(int,input().split())) for i in range(n)]
ans=0
s=[]
t=0
for i,j in enumerate(blu):
    b,l,u=j
    t+=b*l
    heappush(s,(-(u*(x-b)+l*b),i))
while 1:
    i=heappop(s)
    a,b=i
    if(t+a<=0):
        break
    else:
        t+=a
        blu[b]=0
        ans+=x
s=[]
fa=x
tmp=ans
for i in blu:
    if(i!=0):
        b,l,u=i
        heappush(s,(-l,b,u))
        if(t>l*b):
            fa=min(fa,b-((l*b-t)//u))
        else:
            fa=min(fa,-(-t//l))
while 1:
    i=heappop(s)
    l,b,u=i
    if(t+l*b>0):
        t+=l*b
        ans+=b
    else:
        ans+=-(-t//-l)
        break
print(min(ans,fa+tmp))

Submission status: Wrong Answer

Solution code:
import math

ans = 0

N, X = map(int,input().split())
inList = []
temp_lost = 0

for i in range(N):
  b, l, u =  map(int,input().split())
  inList.append([b, l, u, (X-b)*u + b*l])
  temp_lost -= b*l

inList.sort(key=lambda x: -x[3])
for i in range(N):
  if(temp_lost <0 and (temp_lost+inList[i][3]) > 0 ):
    if( (-temp_lost) > inList[i][0]*inList[i][1]):
      # win is needed
      ans += math.ceil((0-temp_lost)/inList[i][2])
    else:
      # lost is allowed
      ans += math.ceil((0-temp_lost)/inList[i][1])
    break
  else:
    ans += X
    temp_lost += inList[i][3]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, math, collections, heapq, itertools
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    file = sys.stdin.readline
    N, X = map(int, file().split())
    aoki = [None] * N
    for i in range(N):
        b, l, u = map(int, file().split())
        aoki[i] = [u, (X - b) * (u - l), l, -b]
    aoki.sort()
    low = [0] * (N + 1)
    high = [0] * (N + 1)
    takahashi = [0] * (N + 1)
    for i in range(N):
        low[i+1] = low[i] + aoki[i][3] *  (-1) * aoki[i][2]
        high[i+1] = high[i] + aoki[i][0]  * (-1) * aoki[i][3]
        takahashi[i+1] = takahashi[i] + aoki[i][0] * X

    minhour = X * N
    tmult = 1
    for i in range(1, N+1):
        S = low[i] + high[N] - high[i]
        tpoint = takahashi[N] - takahashi[i]
        tmult = max(tmult, aoki[i-1][2])
        if tpoint >= S:
            over = min(X, (tpoint - S) // aoki[i-1][0])
            minhour = min(minhour, X * (N - i) - over)
        if tpoint < S:
            extra = 0
            if math.ceil((S -tpoint) / tmult) > X: break
            else: extra = math.ceil((S - tpoint) / tmult)
            minhour = min(minhour, X * (N - i) + extra)
    print(minhour)

    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math
INF = 10**18
n, x = [int(item) for item in input().split()]
blu = [[int(item) for item in input().split()] for _ in range(n)]

manten_point = []
loosing_point = 0 
for i, (b, l, u) in enumerate(blu):
    manten_point.append([(x-b) * u + b * l, i])
    loosing_point += b * l 

manten_point.sort(reverse=True)

manten_set = set() 
for p, index in manten_point:
    if loosing_point < p:
        break
    loosing_point -= p
    manten_set.add(index)

r_time = INF 
# R senryaku
for i, (b, l, u) in enumerate(blu):
    if i in manten_set:
        continue
    if (x-b) * u + b * l >= loosing_point:
        val = int(math.ceil((loosing_point - b*l) / u)) + b
    else:
        val = INF
    r_time = min(r_time, val)

# L senryaku
l_rate_set = [0] * (10**5 + 1)
l_time = 0
for i, (b, l, u) in enumerate(blu):
    if i in manten_set:
        continue
    l_rate_set[l] += b
for i in range(10**5, -1, -1):
    if loosing_point < l_rate_set[i] * i:
        l_time += int(math.ceil(loosing_point / i))
        break
    else:
        loosing_point -= l_rate_set[i] * i
        l_time += l_rate_set[i]

print(min(l_time, r_time) + len(manten_set) * x)