Problem:


Score : 700 points

Problem Statement

Takahashi and Aoki will play a game with a number line and some segments.
Takahashi is standing on the number line and he is initially at coordinate 0.
Aoki has N segments. The i-th segment is [L_i,R_i], that is, a segment consisting of points with coordinates between L_i and R_i (inclusive).

The game has N steps. The i-th step proceeds as follows:

First, Aoki chooses a segment that is still not chosen yet from the N segments and tells it to Takahashi.

Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.

After N steps are performed, Takahashi will return to coordinate 0 and the game ends.

Let K be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that K will be as large as possible, and Takahashi walks along the line so that K will be as small as possible. What will be the value of K in the end?

Constraints

1 ≤ N ≤ 10^5

-10^5 ≤ L_i < R_i ≤ 10^5

L_i and R_i are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
:
L_N R_N

Output

Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above.
It is guaranteed that K is always an integer when L_i,R_i are integers.

Sample Input 1

3
-5 1
3 7
-4 -2

Sample Output 1

10

One possible sequence of actions of Takahashi and Aoki is as follows:

Aoki chooses the first segment. Takahashi moves from coordinate 0 to -4, covering a distance of 4.

Aoki chooses the third segment. Takahashi stays at coordinate -4.

Aoki chooses the second segment. Takahashi moves from coordinate -4 to 3, covering a distance of 7.

Takahashi moves from coordinate 3 to 0, covering a distance of 3.

The distance covered by Takahashi here is 14 (because Takahashi didn't move optimally).
It turns out that if both players move optimally, the distance covered by Takahashi will be 10.

Sample Input 2

3
1 2
3 4
5 6

Sample Output 2

12

Sample Input 3

5
-2 0
-2 0
7 8
9 10
-2 -1

Sample Output 3

34



Sample input:
3
-5 1
3 7
-4 -2


Sample output:
10

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [],[]
for i in range(n):
	l,r = (int(i) for i in input().split())
	L.append(l)
	R.append(r)
L,R,ans = sorted(L)[::-1],sorted(R),0
for i in range((n+1)//2):
	if L[i]-R[i]>=0: ans+=L[i]-R[i]
	else:
		if L[i]>=0: ans+=L[i]
		elif R[i]<=0: ans-=R[i]
		break
print(ans*2)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [],[]
for i in range(n):
	l,r = (int(i) for i in input().split())
	L.append(l)
	R.append(r)
L,R,ans = sorted(L)[::-1],sorted(R),0
for i in range(n):
	if L[i]-R[i]>=0: ans+=L[i]-R[i]
	else:
		if L[i]>=0: ans+=L[i]
		elif R[i]<=0: ans-=R[i]
		break
print(ans*2)

Submission status: Wrong Answer

Solution code:
n = int(input())

arr = []

for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])
    
    
left_arr = sorted(arr, key=lambda x: x[2])
right_arr = sorted(arr, key=lambda x: x[1], reverse=True)

ans = 0

f = 0
ok = [0 for i in range(n)]

if abs(left_arr[0][2]) < abs(right_arr[0][1]):
    f = 1
    
lp = 0
rp = 0
now = 0
for i in range(n):
    if f == 0:
        while True:
            if ok[left_arr[lp][0]] == 0:
                break
            else:
                lp += 1
        if now >= left_arr[lp][1] and now <= left_arr[lp][2]:
            ans += 0
        elif abs(left_arr[lp][1] - now) < abs(left_arr[lp][2] - now):
            ans += abs(left_arr[lp][1] - now)
            now = left_arr[lp][1]
        else:
            ans += abs(left_arr[lp][2] - now)
            now = left_arr[lp][2]
        ok[left_arr[lp][0]] = 1
        lp += 1
        f = 1
    else:
        while True:
            if ok[right_arr[rp][0]] == 0:
                break
            else:
                rp += 1
        if now >= right_arr[rp][1] and now <= right_arr[rp][2]:
            ans += 0
        elif abs(right_arr[rp][1] - now) < abs(right_arr[rp][2] - now):
            ans += abs(right_arr[rp][1] - now)
            now = right_arr[rp][1]
        else:
            ans += abs(right_arr[rp][2] - now)
            now = right_arr[rp][2]
        ok[right_arr[rp][0]] = 1
        rp += 1
        f = 0
        
ans += abs(now)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

arr = []

for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])
    
    
left_arr = sorted(arr, key=lambda x: x[1])
right_arr = sorted(arr, key=lambda x: x[2], reverse=True)

ans = 0

f = 0
ok = [0 for i in range(n)]

if abs(left_arr[0][2]) < abs(right_arr[0][1]):
    f = 1
    
lp = 0
rp = 0
now = 0
for i in range(n):
    if f == 0:
        while True:
            if ok[left_arr[lp][0]] == 0:
                break
            else:
                lp += 1
        if now >= left_arr[lp][1] and now <= left_arr[lp][2]:
            ans += 0
        elif abs(left_arr[lp][1] - now) < abs(left_arr[lp][2] - now):
            ans += abs(left_arr[lp][1] - now)
            now = left_arr[lp][1]
        else:
            ans += abs(left_arr[lp][2] - now)
            now = left_arr[lp][2]
        ok[left_arr[lp][0]] = 1
        lp += 1
        f = 1
    else:
        while True:
            if ok[right_arr[rp][0]] == 0:
                break
            else:
                rp += 1
        if now >= right_arr[rp][1] and now <= right_arr[rp][2]:
            ans += 0
        elif abs(right_arr[rp][1] - now) < abs(right_arr[rp][2] - now):
            ans += abs(right_arr[rp][1] - now)
            now = right_arr[rp][1]
        else:
            ans += abs(right_arr[rp][2] - now)
            now = right_arr[rp][2]
        ok[right_arr[rp][0]] = 1
        rp += 1
        f = 0
        
ans += abs(now)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

arr = []

for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])
    
    
left_arr = sorted(arr, key=lambda x: x[1])
right_arr = sorted(arr, key=lambda x: x[2], reverse=True)

ans = 0

f = 0
ok = [0 for i in range(n)]

if abs(left_arr[0][1]) < abs(right_arr[0][2]):
    f = 1
    
lp = 0
rp = 0
now = 0
for i in range(n):
    if f == 0:
        while True:
            if ok[left_arr[lp][0]] == 0:
                break
            else:
                lp += 1
        if now >= left_arr[lp][1] and now <= left_arr[lp][2]:
            ans += 0
        elif abs(left_arr[lp][1] - now) < abs(left_arr[lp][2] - now):
            ans += abs(left_arr[lp][1] - now)
            now = left_arr[lp][1]
        else:
            ans += abs(left_arr[lp][2] - now)
            now = left_arr[lp][2]
        ok[left_arr[lp][0]] = 1
        lp += 1
        f = 1
    else:
        while True:
            if ok[right_arr[rp][0]] == 0:
                break
            else:
                rp += 1
        if now >= right_arr[rp][1] and now <= right_arr[rp][2]:
            ans += 0
        elif abs(right_arr[rp][1] - now) < abs(right_arr[rp][2] - now):
            ans += abs(right_arr[rp][1] - now)
            now = right_arr[rp][1]
        else:
            ans += abs(right_arr[rp][2] - now)
            now = right_arr[rp][2]
        ok[right_arr[rp][0]] = 1
        rp += 1
        f = 0
        
ans += abs(now)
print(ans)

Submission status: Wrong Answer

Solution code:
def run(n, ln, rn):
    dic = {}
    for i in range(n):
        dic[i] = [ln[i], rn[i]]
    sort_ln = sorted(dic.items(), key=lambda x: -x[1][0])
    sort_rn = sorted(dic.items(), key=lambda x: x[1][1])
    if abs(max([l[1][0] for l in sort_ln])) > abs(min([r[1][1] for r in sort_rn])):
        idx = 0
    else:
        idx = 1
    ret = 0
    now = 0
    for i in range(n):
        if idx == 0:
            if sort_ln[0][1][0] < 0:
                if 0 < sort_ln[0][1][1]:
                    return ret + abs(now)
            if sort_ln[0][1][0] == 0:
                return ret + abs(now)
            if sort_ln[0][1][1] == 0:
                return ret + abs(now)
            move = sort_ln[0][1][idx]
            ret += abs(move - now)
            now = move
            idx = 1
            di = sort_ln[0][0]
        else:
            if sort_rn[0][1][0] < 0:
                if 0 < sort_rn[0][1][1]:
                    return ret + abs(now)
            if sort_rn[0][1][0] == 0:
                return ret + abs(now)
            if sort_rn[0][1][1] == 0:
                return ret + abs(now)
            move = sort_rn[0][1][idx]
            ret += abs(move - now)
            now = move
            idx = 0
            di = sort_rn[0][0]
        del dic[di]
        sort_ln = sorted(dic.items(), key=lambda x: -x[1][0])
        sort_rn = sorted(dic.items(), key=lambda x: x[1][1])
    ret += abs(now)
    return ret


def read_line():
    n = int(input())
    ln = []
    rn = []
    for i in range(n):
        l, r = list(map(int, input().split()))
        ln.append(l)
        rn.append(r)
    return (n, ln, rn)


def main():
    n, ln, rn = read_line()
    print(run(n, ln, rn))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


def get_checks(lefts, rights, used):
    def left_check(pos):
        while lefts:
            e, i = lefts.pop()
            if not used[i]:
                break
            if pos >= e:
                return None
        else:
            return None

        used[i] = True
        return e

    def right_check(pos):
        while rights:
            e, i = rights.pop()
            if not used[i]:
                break
            if pos <= e:
                return None
        else:
            return None

        used[i] = True
        return e

    return left_check, right_check


def simulate(left_first=True):
    ans = 0
    pos = 0
    _lefts = lefts.copy()
    _rights = rights.copy()
    used = [False] * n
    left_check, right_check = get_checks(_lefts, _rights, used)
    funcs = [left_check, right_check] if left_first else [right_check, left_check]
    dir = 0

    while True:
        e = funcs[dir](pos)
        if e is None:
            break
        ans += abs(e - pos)
        pos = e
        dir ^= 1
    ans += abs(pos)
    return ans


n = int(input())
lefts, rights = [], []
for i, line in enumerate(sys.stdin.readlines()):
    l, r = map(int, line.split())
    lefts.append((l, i))
    rights.append((r, i))

lefts.sort()
rights.sort(reverse=True)

lf = simulate(True)
rf = simulate(False)

print(max(lf, rf))

Submission status: Wrong Answer

Solution code:
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
ls = []
rs = []
for i,(l,r) in enumerate(src):
    ls.append((l,r,i))
    rs.append((r,l,i))
ls.sort()
rs.sort(reverse=True)

used = [0] * N
li = ri = 0
now = ans1 = 0
for i in range(N):
    if i%2 == 1:
        while used[ls[li][2]]:
            li += 1
        l,r,i = ls[li]
    else:
        while used[rs[ri][2]]:
            ri += 1
        r,l,i = rs[ri]
    used[i] = 1
    if l <= now <= r: continue
    if now < l:
        ans1 += l - now
        now = l
    else:
        ans1 += now - r
        now = r
ans1 += abs(now)

used = [0] * N
li = ri = 0
now = ans2 = 0
for i in range(N):
    if i%2 == 0:
        while used[ls[li][2]]:
            li += 1
        l,r,i = ls[li]
    else:
        while used[rs[ri][2]]:
            ri += 1
        r,l,i = rs[ri]
    used[i] = 1
    if l <= now <= r: continue
    if now < l:
        ans2 += l - now
        now = l
    else:
        ans2 += now - r
        now = r
ans2 += abs(now)

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N=int(input())
LR=[None]*N
for i in range(N):
    LR[i]=list(map(int,input().split()))

#if len(LR)%2==1:
#    LR=LR+[[0,0]]

RAN=(len(LR)+1)//2

LR_L=sorted(LR,key=lambda x: x[0],reverse=True)
LR_R=sorted(LR,key=lambda x: x[1])

ANS_k=[0]*(RAN*2+2)

for i in range(RAN):
    ANS_k[2*i+1]=ANS_k[2*i]+LR_L[i][0]
    ANS_k[2*i+2]=ANS_k[2*i+1]-LR_R[i][1]

print(max(ANS_k)*2)

Submission status: Wrong Answer

Solution code:
N=int(input())
LR=[None]*N
for i in range(N):
    LR[i]=list(map(int,input().split()))

RAN=(len(LR)+1)//2

LR_L=sorted(LR,key=lambda x: x[0],reverse=True)
LR_R=sorted(LR,key=lambda x: x[1])

ANS_k=[0]*(RAN*2+2)

for i in range(RAN):
    ANS_k[2*i+1]=ANS_k[2*i]+LR_L[i][0]
    ANS_k[2*i+2]=ANS_k[2*i+1]-LR_R[i][1]

print(ANS_k)
print(max(ANS_k)*2)

Submission status: Wrong Answer

Solution code:
N=int(input())
LR=[None]*N
for i in range(N):
    LR[i]=list(map(int,input().split()))

if len(LR)%2==1:
    LR=LR+[[0,0]]

RAN=(len(LR)+1)//2

LR_L=sorted(LR,key=lambda x: x[0],reverse=True)
LR_R=sorted(LR,key=lambda x: x[1])

LL=0
RR=0

for i in range(RAN):
    LL+=LR_L[i][0]
    RR-=LR_R[i][1]

print((LL+RR)*2)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [0]*N
R = [0]*N

for i in range(N):
	L[i],R[i] = map(int,input().split(" "))
L.sort(reverse=True)
R.sort()
d = 0
for i in range(N):
	if L[i] > 0:
		d += (L[i]*2)
	if R[i] < 0:
		d += ((-R[i])*2)

print(d)

Submission status: Wrong Answer

Solution code:
n = int(input())
left = []
right = []
for i in range(n):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)

left.sort(reverse=True)
right.sort()
p, q = 0, 0
ans = 0
if min(right) < 0 and max(left) >= 1:
    for i in range(n):
        q = p
        if i % 2 == 0:
            p = right[i//2]
        else:
            p = left[i//2]
        ans += abs(p-q)
        if i == n-1:
            ans += abs(p)
else:
    for i in range(n):

        q = p
        if i % 2 == 1:
            p = right[i//2]
        else:
            p = left[i//2]
        ans += abs(p-q)
        if i == n-1:
            ans += abs(p)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = []
for i in range(N):
  LR.append([int(x) for x in input().split()])
#print(LR)
distance = []
for lr in LR:
  if lr[0] > 0:
    distance.append([lr[0],lr[0],lr[1]])
  elif lr[1] < 0:
    distance.append([lr[1],lr[0],lr[1]])
  else:
    distance.append([0,lr[0],lr[1]])
#print(distance)
distance.sort(key=lambda x: x[0])
count = 0
x = 0
dict = 0 if abs(distance[0][0]) >= abs(distance[-1][0]) else -1
while distance:
  if distance[dict][1] > x:
    next = distance[dict][1]
  elif distance[dict][2] < x:
    next = distance[dict][2]
  else:
    next = x
  count += abs(next - x)
  x = next
  if dict == 0:
    distance = distance[1:]
    dict = -1
  else:
    distance = distance[:-1]
    dict = 0
count += abs(x)
print(count)

Submission status: Wrong Answer

Solution code:
def main():
    
    #0 input
    N = int(input())
    LR = []
    for i in range(N):
        LR.append(list(map(int, input().split())))

    
    LR_sorted_tmp = sorted(LR, reverse=False)
    LR_sorted = []
    for part in LR_sorted_tmp:
        if part[0] > 0:
            LR_sorted.append(part)
            

    LR_sorted_R_tmp = sorted(LR, key=lambda x:x[1], reverse=True)
    LR_sorted_R = []
    for part in LR_sorted_R_tmp:
        if part[1] < 0:
            LR_sorted_R.append(part)
            
    
    takahashi = 0
    moved = 0

    while 1:
        
        # finish if no items in the list
        if len(LR_sorted) == 0:
            break
        
        #1 choose maximum L
        next_interval  = LR_sorted[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += next_interval[0] - takahashi
        takahashi = next_interval[0]
        
        # delete the current interval
        del LR_sorted[-1]


        #2 choose minimum R      
        # finish if no items in the list
        if len(LR_sorted_R) == 0:
            break
        
        next_interval = LR_sorted_R[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += takahashi - next_interval[1]
        takahashi = next_interval[1]
        
        # delete the current interval
        del LR_sorted_R[-1]

        
    moved += abs(0-takahashi)
    print(moved)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import repeat
def ng(a):
    return [(-y, -x) for x, y in a]
def go(p, ab=abs):
    p.sort(reverse=True)
    m = [y for x, y in p[(len(p)+1)/2:]]
    m.sort()
    p = p[:(len(p)+1)/2]
    ans = 0
    pr = 0
    for x, y in p:
        if x <= 0 <= y:
            x = y if pr > y else pr
        elif y < 0:
            x = y
        ans += ab(x - pr)
        pr = x
        if m:
            t = m.pop()
            if pr > t:
                ans += ab(pr - t)
                pr = t
    if m and m[-1] < pr:
        ans += ab(pr - m[-1])
        pr = m[-1]
    ans += abs(pr)
    return ans
def main():
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    a = []
    for i in xrange(n):
        a.append(dat[i*2:i*2+2])
    print max(go(a), go(ng(a)))
main()

Submission status: Wrong Answer

Solution code:
def main():
    
    #0 input
    N = int(input())
    LR = []
    for i in range(N):
        LR.append(list(map(int, input().split())))
        
    
    LR_sorted = sorted(LR, reverse=False)
    LR_sorted_R = sorted(LR, key=lambda x:x[1], reverse=True)
    takahashi = 0
    moved = 0

    while 1:
        #1 choose maximum L
        next_interval  = LR_sorted[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += next_interval[0] - takahashi
        takahashi = next_interval[0]
        
        # delete the current interval
        del LR_sorted[-1]

        # finish if no items in the list
        if len(LR_sorted) == 0:
            break


        
        #2 choose minimum R
        while not LR_sorted_R[-1] in LR_sorted :
            del LR_sorted_R[-1]
        
        next_interval = LR_sorted_R[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += takahashi - next_interval[1]
        takahashi = next_interval[1]
        
        # delete the current interval
        LR_sorted.remove(next_interval)


        # finish if no items in the list
        if len(LR_sorted) == 0:
            break
        
        
    moved += abs(0-takahashi)
    print(moved)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[0 for i in range(N)]
R=[0 for i in range(N)]
P=[]
for i in range(N):
    L[i],R[i]=map(int,input().split())
    if L[i]<=0<=R[i]:
        P.append(0)
    elif 0<L[i]:
        P.append(L[i])
    else:
        P.append(R[i])
P.sort()
X=[]
Y=[]
for i in range(N):
    #0<=i<N
    if i<N//2:
        X.append(P[i])
    else:
        Y.append(P[i])
Y.sort(reverse=True)
#len(X)<=len(Y)
if N%2==1:
    X.append(Y[len(Y)-1])
K1=0
K2=0
D1=0
D2=0
M=len(X)
for i in range(M):
    K1+=abs(X[i]-D1)
    K2+=abs(Y[i]-D2)
    D1=X[i]
    D2=Y[i]
    K1+=abs(Y[i]-D1)
    K2+=abs(X[i]-D2)
    D1=Y[i]
    D2=X[i]
K1+=abs(D1)
K2+=abs(D2)
print(max([K1,K2]))

Submission status: Wrong Answer

Solution code:
n = int(input())

field1 = []
field2 = []

max_r = 0
max_l = 0

for i in range(n):
    s = list(map(int, list(input().rstrip().split())))
    if(max_r < s[0]):
        max_r = s[0]
    if(s[1] < max_l):
        max_l = s[1]
    field1.append(s)
    field2.append(s)

if(max_r >= -max_l):  # 最初の進行方向右
    target = 0
    field1.sort(key=lambda x: -x[0])
    field2.sort(key=lambda x: x[1])
else:
    target = 1
    field1.sort(key=lambda x: x[1])
    field2.sort(key=lambda x: -x[0])

move = 0
now = 0
field = 1
m = int(n / 2)

for i in range(m + 1):
    if(field1[i][0] <= now and now <= field1[i][1]):
        break
    last = field1[i][target]
    if(now > 0 and last > 0)or(now < 0 and last < 0):
        move += abs(now - last)
    else:
        move += abs(now) + abs(last)
    now = last
    if(i == m):
        break
    else:
        if(field2[i][0] <= now and now <= field2[i][1]):
            break
        last = field2[i][1 - target]
    if(now > 0 and last > 0)or(now < 0 and last < 0):
        move += abs(now - last)
    else:
        move += abs(now) + abs(last)
    now = last

move += abs(now)

print(move)

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = [[int(i) for i in input().split()] for i in range(N)]
cor = 0
sum = 0
print(arr)
Larr = sorted(arr, key=lambda x:(x[0]), reverse=True)
Rarr = sorted(arr, key=lambda x:(x[1]))
for i in range(N):
    if Larr[0][0]-cor < cor-Rarr[0][1]:
        if Rarr[0][0] > cor or cor > Rarr[0][1]:
             sum += cor-Rarr[0][1]
             cor = Rarr[0][1]

        Larr.remove(Rarr[0])
        Rarr.pop(0)
    else:
        if Larr[0][0] > cor or cor > Larr[0][1]:
             sum += Larr[0][0]-cor
             cor = Larr[0][0]

        Rarr.remove(Larr[0])
        Larr.pop(0)

sum += abs(cor)
print("{}".format(sum))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
R = []
for i in range(N):
    l, r = list(map(int, input().split()))
    L.append((i, l))
    R.append((i, r))
L.sort(key=lambda x:x[1])
R.sort(key=lambda x:x[1])

def choose(id, pos):
    for i in range(len(L)):
        if L[i][0]==id:
            l = L[i][1]
            del L[i]
            break
    for i in range(len(R)):
        if R[i][0]==id:
            r = R[i][1]
            del R[i]
            break
    if l<=pos and pos<=r:
        return pos
    elif r<pos:
        return r
    else:
        return l

pos = 0
ans = 0
while len(L)>0:
    print(len(L),len(R), pos, ans)
    idl,l = L[-1]
    idr,r = R[0]
    if r<pos and pos<l:
        if pos-r>l-pos:
            new_pos = choose(idr, pos)
            ans += abs(pos - new_pos)
            pos = new_pos
        else:
            new_pos = choose(idl, pos)
            ans += abs(pos - new_pos)
            pos = new_pos
    elif r<pos:
        new_pos = choose(idr, pos)
        ans += abs(pos - new_pos)
        pos = new_pos
    else:
        new_pos = choose(idl, pos)
        ans += abs(pos - new_pos)
        pos = new_pos

print(ans+abs(pos))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
S = []
for i in range(N):
    l, r = map(int, input().split())
    S.append((l, r))
S.sort()

kata = 0
if all(l <= 0 for l, r in S):
    kata = 1
    S = [(-r, -l) for l, r in S]
elif all(r >= 0 for l, r in S):
    kata = 1

que = []
P = []
Q = [l for l, r in S]
Q.reverse()
ans = 0
a = b = 0
p = 0
for i in range(N+1):
    if i < N:
        l, r = S[i]
    while que and que[0] <= l:
        P.append(heappop(que))
    cnt = min(len(P), len(Q))
    while p < cnt:
        a += P[p]
        b += Q[p]
        p += 1
    ans = max(ans, abs(a-b)*2)
    if len(P) < len(Q):
        b2 = b + Q[cnt]
        ans = max(ans, abs(a-b2)*2)
    elif len(P) > len(Q):
        a2 = a + P[cnt]
        ans = max(ans, abs(a2-b)*2)
    heappush(que, r)
    if cnt == len(Q) > 0:
        a -= P[cnt-1]
        b -= Q[cnt-1]
    if i < N:
        Q.pop()
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(map(int, raw_input().split()))
j=0
c=0
d=0
e=0
h=0
f=0
for q in range(0,n):
    d=0
    for i in range(0,n-f):
        if a[i][0] >= c:
            c = a[i][0]
            d = i
    h = c
    e = e + abs(h-j)
    if (len(a)>0)&(e!=0):
        del a[d]
        f=f+1
    g=c
    d=0
    for i in range(0,n-f):
        if a[i][1] <= g:
            g = a[i][1]
            d = i

    else:
        if len(a)>0:
            j = g
            e=e+abs(h-j)
            f=f+1
            del a[d]
    c=g
    
e=e+abs(c)
print(e)

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [list(map(int, input().split())) for i in range(N)]
L, R = [], []

for l, r in LR:
    if l > 0:
        L.append([l, r])
    elif r < 0:
        R.append([l, r])

L.sort(reverse=True, key=lambda x: x[0])
R.sort(key=lambda x: x[1])

ans = 0
now = 0
m = min(len(L), len(R))
if len(L) > len(R):
    for i in range(m):
        ans += abs(now-L[i][0])
        now = L[i][0]
        ans += abs(now-R[i][1])
        now = R[i][1]
else:
    for i in range(m):
        ans += abs(now-R[i][1])
        now = R[i][1]
        ans += abs(now-L[i][0])
        now = L[i][0]

if len(L) > len(R):
    Z = L[m:]
    Z = Z[::2] + Z[1::2]
    for z in Z:
        if now < z[0]:
            ans += abs(z[0] - now)
            now = z[0]

        elif z[1] < now:
            ans += abs(z[1] - now)
            now = z[1]

elif len(L) < len(R):
    Z = R[m:]
    Z = Z[::2] + Z[1::2]
    for z in Z:
        if now < z[0]:
            ans += abs(z[0] - now)
            now = z[0]

        elif z[1] < now:
            ans += abs(z[1] - now)
            now = z[1]

print(ans + now)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
S = []
for i in range(N):
    l, r = map(int, input().split())
    S.append((l, r))
S.sort()

kata = 0
if all(l <= 0 for l, r in S):
    kata = 1
    S = [(-r, -l) for l, r in S]
elif all(r >= 0 for l, r in S):
    kata = 1

que = []
P = []
Q = [l for l, r in S]
Q.reverse()
ans = 0
a = b = 0
p = 0
for l, r in S:
    while que and que[0] <= l:
        P.append(heappop(que))
    cnt = min(len(P), len(Q))
    while p < cnt:
        a += P[p]
        b += Q[p]
        p += 1
    ans = max(ans, abs(a-b)*2)
    if 0 < len(P) < len(Q):
        b2 = b + Q[cnt]
        ans = max(ans, abs(a-b2)*2)
    elif len(P) > len(Q) > 0:
        a2 = a + P[cnt]
        ans = max(ans, abs(a2-b)*2)
    heappush(que, r)
    if cnt == len(Q):
        a -= P[cnt-1]
        b -= Q[cnt-1]
    Q.pop()
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq

N = int(input())

ispush = []
L,R = [],[]
LR = []
for i in range(N):
    l,r = map(int,input().split())
    hq.heappush(L,(l*(-1),i)) #取り出す時は-1を掛け直すことに注意
    hq.heappush(R,(r,i))
    LR.append([l,r])
    ispush.append(False)

side = True #Trueで右(Lを追う側に行く)
sl = min(L)[0]*(-1) #実質max
sr = min(R)[0]
if sl*sr>0: #同符号
    if sl>0: side = True
    else: side = False
else: #異符号
    if abs(sr)>abs(sl): side = False

ans = 0
now = 0
for i in range(N):
    #選ぶ
    mov = 0
    if side: #L追う
        while(True):
            if len(L)==0:break
            mov = hq.heappop(L)
            if not ispush[mov[1]]: break
    else:
        while(True):
            if len(R)==0:break
            mov = hq.heappop(R)
            if not ispush[mov[1]]: break
    ispush[mov[1]] = True
    #移動する
    if now < LR[mov[1]][0] or LR[mov[1]][1] < now:
        if side:
            ans += abs((mov[0]*(-1))-now)
            now = mov[0]*(-1)
        else:
            ans += abs(mov[0]-now)
            now = mov[0]
    if side: side=False
    else: side=True
    #print(now)
            
print(ans+abs(now))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(list(map(int,input().split())))
 
ans = 0
b = [0 for i in range(n)]
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
for i in range(n):
    print(b,c,d,e,f,g,h)
    d = 0
    for j in range(n):
        if b[j] == 0:
            if max(abs(c-a[j][0]),abs(c-a[j][1])) > d:
                g = j
                d = max(abs(c-a[j][0]),abs(c-a[j][1]))
                if c < a[j][0] or c > a[j][1]:
                    e = min(abs(c-a[j][0]),abs(c-a[j][1]))
                    if abs(c-a[j][0])>abs(c-a[j][1]):
                        f = a[j][1]
                    else:
                        f = a[j][0]
                else:
                    continue
    c = f
    b[g] = 1
    h += e
 
print(h+abs(c))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(list(map(int,input().split())))

ans = 0
b = [0 for i in range(n)]
c = 0 #現在地
d = 0 #いったん距離をおいておく
e = 0#その時の最短距離
f = 0#一端の距離
g = 0#総距離
h = 0
for i in range(n):
    print(b,c,d,e,f,g,h)
    d = 0
    for j in range(n):
        if b[j] == 0:
            if max(abs(c-a[j][0]),abs(c-a[j][1])) > d:
                g = j
                d = max(abs(c-a[j][0]),abs(c-a[j][1]))
                if c < a[j][0] or c > a[j][1]:
                    e = min(abs(c-a[j][0]),abs(c-a[j][1]))
                    if abs(c-a[j][0])>abs(c-a[j][1]):
                        f = a[j][1]
                    else:
                        f = a[j][0]
                else:
                    continue
    c = f
    b[g] = 1
    h += e

print(h+abs(c))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
now = 0
cost = 0
for i in range(n):
    s.append([i] + list(map(int, input().split())))
reached = set()
low_s = sorted(s, key=lambda x: x[1])
l_ = 0
high_s = sorted(s, key=lambda x: -x[2])
r_ = 0

ans = 0

for i in range(n):
    if not i%2:
        for j in range(r_, n):
            if high_s[j][0] in reached:
                continue
            reached.add(high_s[j][0])
            r_ = j
            if high_s[j][1] <= now <= high_s[j][2]:
                break
            will = high_s[j][1] if now < high_s[j][1] else high_s[j][2]
            cost += abs(will - now)
            now = will
            break
    else:
        for j in range(l_, n):
            if low_s[j][0] in reached:
                continue
            reached.add(low_s[j][0])
            l_ = j
            if low_s[j][1] <= now <= low_s[j][2]:
                break
            will = low_s[j][1] if now < low_s[j][1] else low_s[j][2]
            cost += abs(will - now)
            now = will
            break
cost += abs(now)
print(cost)

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())

kukan=np.zeros((N,2))

for i in range(N):
	kukan[i][0],kukan[i][1]=input().split(' ')
	kukan[i][0]=int(kukan[i][0])
	kukan[i][1]=int(kukan[i][1])

t=0
kyori=0
for i in range(N):
	a=np.min(np.abs(kukan-np.array([t,t])),axis=1)
	index=np.argmax(a)
	kyori=kyori+np.max(a)
	tt=np.argmin(np.abs(kukan[index]-t))
	t=kukan[index][tt]
	kukan=np.delete(kukan,index,axis=0)
print(int(kyori))

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [],[]
for num in range(n):
	l,r = (int(i) for i in input().split())
	L.append(l)
	R.append(r)
L,R,a,b = sorted(L),sorted(R),-1,0
if R and L and R[0]<=0 and L[-1]>=0:
	if abs(R[0])<L[-1]: flag,now,ans,a = True,L[-1],L[-1],-2
	else: flag,now,ans,b = False,R[0],abs(R[0]),1
elif R and L and R[0]<=0 and L[-1]<0: flag,now,ans,b = False,R[0],abs(R[0]),1
elif R and L and R[0]>0 and L[-1]>=0: flag,now,ans,a = True,L[-1],L[-1],-2
else: ans,now = 0,0
while ans!=0 and b-a!=len(L)+1 and ((flag and R[b]<now) or (not flag and now<L[a])) and R[b]<L[a]:
	if flag: flag,now,ans,b = False,R[b],ans+abs(now-R[b]),b+1
	else: flag,now,ans,a = True,L[a],ans+abs(now-L[a]),a-1
print(ans+abs(now))