Under construction

Still being built. Things may change

Shortcut

Time limit2sMemory limit512 MB

Summary
Given a main railway line with side branches, add one express edge of length c between two main stations so the network diameter is as small as possible.
Level

Hard8 of 10

Topics
Binary search, Two pointers, Prefix sum
Solved
No attempts yet

Problem

Pavel has a toy railway. It is very simple. The railway has one main line of nn stations, numbered from 0 to n−1n-1 in order along the line. The distance between stations ii and i+1i+1 is lil_i centimeters (0≤i≤n−10 \le i \le n-1).

Apart from the main line, there may be some secondary lines. Each secondary line connects a station on the main line to a new station that is not on the main line. (The new stations are not numbered.) At most one secondary line starts at each station of the main line. The length of the secondary line starting at station ii is did_i centimeters. di=0d_i = 0 means no secondary line starts at station ii.

Pavel now plans to build one shortcut: an express line between two different stations (possibly neighbouring ones) of the main line. The express line has length exactly cc centimeters, regardless of which two stations it connects.

Each segment of the railway, including the new express line, can be used in both directions. The distance between two stations is the length of the shortest route between them along the railways. The diameter of the network is the maximum distance over all pairs of stations, that is, the smallest tt such that the distance between every pair of stations is at most tt.

Pavel wants to build the express line so that the diameter of the resulting network is minimized.

Constraints

In all subtasks, 2≤n≤1,000,0002 \le n \le 1{,}000{,}000, 1≤li≤1091 \le l_i \le 10^9, 0≤di≤1090 \le d_i \le 10^9, and 1≤c≤1091 \le c \le 10^9.

Examples1

  1. Example 1

    Input
    2 1
    1
    0 0
    
    Expected output
    1