#abc340d. D - Super Takahashi Bros.

D - Super Takahashi Bros.

Score: 425425 points

问题描述

Takahashi 正在玩一款游戏。

该游戏包含 NN 个阶段,编号为 1,2,,N1,2,\ldots,N。最初,只有第 11 阶段可以进行游戏。

对于每个可以玩的阶段 ii1iN11\leq i \leq N-1),在阶段 ii 上,你可以执行以下两种操作之一:

  • 花费 AiA_i 秒来通关阶段 ii。这将允许你游玩阶段 i+1i+1
  • 花费 BiB_i 秒来通关阶段 ii。这将允许你游玩阶段 XiX_i

忽略除清除阶段所需时间以外的所有时间,最少需要多少秒才能开始游玩阶段 NN

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Takahashi is playing a game.

The game consists of NN stages numbered 1,2,,N1,2,\ldots,N. Initially, only stage 11 can be played.

For each stage ii ( 1iN11\leq i \leq N-1 ) that can be played, you can perform one of the following two actions at stage ii:

  • Spend AiA_i seconds to clear stage ii. This allows you to play stage i+1i+1.
  • Spend BiB_i seconds to clear stage ii. This allows you to play stage XiX_i.

Ignoring the times other than the time spent to clear the stages, how many seconds will it take at the minimum to be able to play stage NN?

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1Ai,Bi1091 \leq A_i, B_i \leq 10^9
  • 1XiN1 \leq X_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1 X1X_1

A2A_2 B2B_2 X2X_2

\vdots

AN1A_{N-1} BN1B_{N-1} XN1X_{N-1}

Output

Print the answer.

Sample Input 1

5
100 200 3
50 10 1
100 200 5
150 1 2

Sample Output 1

350

By acting as follows, you will be allowed to play stage 55 in 350350 seconds.

  • Spend 100100 seconds to clear stage 11, which allows you to play stage 22.
  • Spend 5050 seconds to clear stage 22, which allows you to play stage 33.
  • Spend 200200 seconds to clear stage 33, which allows you to play stage 55.

Sample Input 2

10
1000 10 9
1000 10 10
1000 10 2
1000 10 3
1000 10 4
1000 10 5
1000 10 6
1000 10 7
1000 10 8

Sample Output 2

90

Sample Input 3

6
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1

Sample Output 3

5000000000

update @ 2024/3/10 01:33:02