luogu#P8220. [WFOI - 02] I wanna win the race(比赛)

[WFOI - 02] I wanna win the race(比赛)

Background

best is yet to come

Kid accidentally entered online mode. He needs to clear the level faster than his opponent to win.

Problem Description

Kid enters a venue where several players are racing. The venue can be abstracted as a coordinate system.

The players need to run from (1,1)(1, 1) to (n,n)(n, n). If a player is currently at (x,y)(x, y), then in the next step they can move to (x±1,y)(x \pm 1, y) or (x,y±1)(x, y \pm 1). Note that they can only move in the first quadrant, meaning x>0,y>0x > 0, y > 0 at all times.

All points are initially type A\texttt{A}. The organizer chooses a triple (a,b,c)(a, b, c) and changes all points (x,y)(x, y) satisfying axba \le x \le b and ycy \le c into type B\texttt{B}. Each time a player passes a type A\texttt{A} point it takes 11 second, and each time they pass a type B\texttt{B} point it takes 22 seconds. Please note that the starting point and the ending point are also included in the calculation.

Kid wants to win this race, and he wants to know the minimum number of seconds needed to reach the destination.

Please note that there are important constraints in the Constraints section.

Input Format

There are two lines. The first line contains an integer nn. The second line contains three integers a,b,ca, b, c.

Output Format

Output one integer, representing the minimum number of seconds Kid needs to clear the level.

5
2 4 3
9

Hint

Sample Explanation

The figure below shows one feasible plan. Purple points are type A\texttt{A} points, and red points are type B\texttt{B} points:

Constraints

This problem uses bundled Subtask\tt Subtask tests.

For 30%30\% of the testdata, 1<a<b<n103,0<c1031 < a < b < n \le 10^3, 0 < c \le 10^3.

For 100%100\% of the testdata, 1<a<b<n109,0<c1091 < a < b < n \le 10^9, 0 < c \le 10^9.

Translated by ChatGPT 5