IOI '16 P3 - Shortcut (Standard I/O)

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type

Pavel has a toy railway. It is very simple. There is a single main line consisting of n stations. These stations are numbered from 0 to n-1 in order along the line. The distance between the stations i and i+1 is l_i centimeters (0 \le i < n-1).

Apart from the main line there may be some secondary lines. Each secondary line is a railway line between a station on the main line and a new station that does not lie on the main line. (These new stations are not numbered.) At most one secondary line can start in each station of the main line. The length of the secondary line starting at station i is d_i centimeters. We use d_i = 0 to denote that there is no secondary line starting at station i.

Pavel is now planning to build one shortcut: an express line between two different (possibly neighbouring) stations of the main line. Express line will have length of exactly c centimeters, regardless of what two stations it will connect.

Each segment of the railway, including the new express line, can be used in both directions. The distance between two stations is the smallest length of a route that goes from one station to the other along the railways. The diameter of the whole railway network is the maximum distance among all pairs of stations. In other words, this is the smallest number t, such that the distance between every pair of stations is at most t.

Pavel wants to build the express line in such a way that the diameter of the resulting network is minimized.

Input Specification

Line 1 of input will contain two space separated integers n and c, the number of stations on the main line and the length of the new express line, respectively.
Line 2 of the input will contain l, an array of length n-1. The space separated integers l_0, l_1, \dots, l_{n-2} give the distances between stations on the main line.
Line 3 of the input will contain d, an array of length n. The space separated integers d_0, d_1, \dots, d_{n-1} give the lengths of secondary lines.

Sample Input 1

4 10
10 20 20
0 40 0 30

Sample Output 1

80

Explanation for Sample Output 1

The optimal solution is the build the express line between stations 1 and 3, as shown below.

The diameter of the new railway network is 80 centimeters, so your program should output 80.

Sample Input 2

9 30
10 10 10 10 10 10 10 10
20 0 30 0 0 40 0 40 0

Sample Output 2

110

Explanation for Sample Output 2

The optimal solution is to connect stations 2 and 7, in which case the diameter is 110.

Sample Input 3

4 1
2 2 2
1 10 10 1

Sample Output 3

21

Explanation for Sample Output 3

The optimal solution is to connect stations 1 and 2, reducing the diameter to 21.

Sample Input 4

3 3
1 1
1 1 1

Sample Output 4

4

Explanation for Sample Output 4

Connection any two stations with the express line of length 3 does not improve the initial diameter of the railway network which is 4.

Subtasks

In all subtasks, 2 \le n \le 1\,000\,000, 1 \le l_i \le 10^9, 0 \le d_i \le 10^9, 1 \le c \le 10^9.

  1. (9 points) 2 \le n \le 10
  2. (14 points) 2 \le n \le 100
  3. (8 points) 2 \le n \le 250
  4. (7 points) 2 \le n \le 500
  5. (33 points) 2 \le n \le 3\,000
  6. (22 points) 2 \le n \le 100\,000
  7. (4 points) 2 \le n \le 300\,000
  8. (3 points) 2 \le n \le 1\,000\,000

Comments

There are no comments at the moment.