PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000021. Đồng xu hồi sinh (Coins Respawn)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đồng xu hồi sinh (Coins Respawn)

Coins Respawn

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You start at vertex 11 with zero coins in a directed graph. Every traversal of edge Ai→BiA_i\to B_i takes one minute and awards CiC_i coins, replenished on every visit. At vertex NN you may stop by pressing a button, or continue. On stopping after TT minutes, pay T⋅PT\cdot P coins (or all your coins if insufficient). The remaining number of coins is your score. Find the maximum possible score, or print -1 if unbounded. Vertex NN is reachable.

Input

First line contains N,M,PN,M,P, followed by MM lines Ai,Bi,CiA_i,B_i,C_i.

Output

Print -1 if unbounded; otherwise print the maximum nonnegative remaining coins.

Subtasks

  • Subtask 1 (20 points): 2≤N≤152\le N\le15; 1≤M≤501\le M\le50; 0≤P≤1000\le P\le100; 1≤Ci≤1001\le C_i\le100; 1≤Ai,Bi≤N1\le A_i,B_i\le N; vertex NN reachable from 11; parallel edges allowed.

  • Subtask 2 (30 points): 2≤N≤1002\le N\le100; 1≤M≤10001\le M\le1000; 0≤P≤10000\le P\le1000; 1≤Ci≤10001\le C_i\le1000; 1≤Ai,Bi≤N1\le A_i,B_i\le N; vertex NN reachable from 11; parallel edges allowed.

  • Subtask 3 (50 points): 2≤N≤25002\le N\le2500; 1≤M≤50001\le M\le5000; 0≤P≤1000000\le P\le100000; 1≤Ci≤1000001\le C_i\le100000; 1≤Ai,Bi≤N1\le A_i,B_i\le N; vertex NN reachable from 11; parallel edges allowed.

Examples

Input

3 3 10
1 2 20
2 3 30
1 3 45

Output

35

Explanation

The two-edge route yields 50−20=3050-20=30; the direct route yields 45−10=3545-10=35, which is optimal.