#46 ABC131-D
** Thoughts ** Sort by $ B_i $ and calculate the time required to get the job done. Change the flag when the time limit is exceeded.
n = int(input())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key=lambda x: x[1])
t = 0
flag = True
for i in range(n):
t += ab[i][0]
if t > ab[i][1]:
flag = False
if flag:
print('Yes')
else:
print('No')
** Thoughts **
In terms of the size of N
n = int(input())
a = list(map(int,input().split()))
a.insert(0,0)
a.append(0)
d = [abs(a[i]-a[i+1]) for i in range(n+1)]
s = sum(d)
for i in range(1,n+1):
print(s+abs(a[i-1]-a[i+1])-(abs(a[i-1]-a[i])+abs(a[i]-a[i+1])))
I thought about the second question a little. I realized that I had to reduce the amount of calculation because N was large, but I couldn't reduce it well. See you again, good night.
Recommended Posts