{"problem":{"name":"Traveler","description":{"content":"There are $N$ balls, called Ball $1$ through $N$, on a number line.   Ball $i$ is at the coordinate $X_i$.   Each ball has a color represented by an integer ID between $1$ and $N$ (inclusive); the ID ","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc197_e"},"statements":[{"statement_type":"Markdown","content":"There are $N$ balls, called Ball $1$ through $N$, on a number line.  \nBall $i$ is at the coordinate $X_i$.  \nEach ball has a color represented by an integer ID between $1$ and $N$ (inclusive); the ID of the color of Ball $i$ is $C_i$.  \nYou are now at the coordinate $0$. You will collect all the balls by moving along the line at the speed of $1$ per second, and then return to the coordinate $0$.  \nHere, you have to collect the balls in a non-descending order of their IDs.  \nWhen collecting a ball, you have to be at the coordinate of that ball, but it is not mandatory to collect it when you are there.  \nFind the minimum time needed to start at the coordinate $0$, collect all the balls, and return to the coordinate $0$.\n\n## Constraints\n\n*   $1 \\le N \\le 2 \\times 10^5$\n*   $|X_i| \\le 10^9$\n*   $X_i \\neq X_j (i \\neq j)$\n*   $X_i \\neq 0$\n*   $1 \\le C_i \\le N$\n*   All values in input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$N$\n$X_1$ $C_1$\n$X_2$ $C_2$\n$X_3$ $C_3$\n$\\hspace{15pt} \\vdots$\n$X_N$ $C_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc197_e","tags":[],"sample_group":[["5\n2 2\n3 1\n1 3\n4 2\n5 3","12\n\nThe optimal strategy is:\n\n*   spend $3$ seconds to reach the coordinate $3$ and collect Ball $2$;\n*   spend $1$ second to reach the coordinate $2$ and collect Ball $1$;\n*   spend $2$ seconds to reach the coordinate $4$ and collect Ball $4$;\n*   spend $1$ second to reach the coordinate $5$ and collect Ball $5$;\n*   spend $4$ seconds to reach the coordinate $1$ and collect Ball $3$;\n*   spend $1$ second to return to the coordinate $0$.\n\nHere, we collected the balls in a non-descending order of their IDs: $1, 2, 2, 3, 3$."],["9\n5 5\n-4 4\n4 3\n6 3\n-5 5\n-3 2\n2 2\n3 3\n1 4","38"]],"created_at":"2026-03-03 11:01:13"}}