This page is still under construction.

Parts of this page are still being built. What you see may change.

Strike

Time limit1sMemory limit128 MB

Summary
Choose one train to hold for k minutes in a DAG rail network so the total delay passed on to all trains is largest.
Level

Hard8 of 10

Topics
Dynamic programming, Topological sort, Graph
Solved
No attempts yet

Problem

Byteland is proud to own the largest lignite (brown coal) mine in the world. Every day, coal from the mine is carried across the railway network to every city in Byteland so that residents have something to burn in their stoves.

Transport works like this: first, some trains set out from the city with the mine toward a few other cities; then from those cities more trains depart to still other cities, and so on. For every city in Byteland there is at least one sequence of trains p1,p2,…,pkp_1, p_2, \dots, p_k such that coal from the mine is loaded onto train p1p_1, then for each i=1,…,k−1i = 1, \dots, k-1 the coal is transferred from train pip_i to train pi+1p_{i+1}, until it finally reaches that city on train pkp_k. Several trains may arrive at any city (except the city with the mine), but there are no cycles: once you board a train in some city, you can never return to that city by rail.

The trains are coordinated: departure times are set so that a train leaving a city departs only after every scheduled coal train has arrived at that city. If a train is delayed, it may in turn delay other trains. The railway workers are planning a strike: they can hold exactly one train for kk minutes. They want to pick the train so that the total delay over all trains is as large as possible.

Compute that maximum total delay.

Input

The first line contains two integers nn and mm (2≤n≤4002 \le n \le 400, 1≤m≤80 0001 \le m \le 80\,000): the number of cities in Byteland and the number of direct rail connections. The second line contains one integer kk (1≤k≤1091 \le k \le 10^9): the number of minutes for which the workers can hold one train. Cities are numbered from 11 to nn; the mine is in city 11.

Each of the next mm lines contains four integers aia_i, bib_i, wiw_i, pip_i (1≤ai,bi≤n1 \le a_i, b_i \le n, 0≤wi,pi≤1090 \le w_i, p_i \le 10^9, 0≤wi+pi≤1090 \le w_i + p_i \le 10^9). They mean that, on schedule, the ii-th train leaves city aia_i exactly wiw_i minutes after sunrise and arrives at city bib_i exactly pip_i minutes later, on the same day (a Byteland day lasts 109+110^9 + 1 minutes). For every city, the departure times of the trains leaving it are not smaller than the largest arrival time of any train arriving at it.

Output

Print a single integer: the maximum total delay of the trains that the workers' strike can cause.

Hint

For example, holding for 33 minutes the train that runs from city 11 to city 33 delays that train together with the two trains departing from city 33.

Examples1

  1. Example 1

    Input
    5 5
    3
    1 2 3 1
    1 3 0 3
    3 2 4 1
    3 4 3 5
    2 5 8 2
    
    Expected output
    8