QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓

#18911. Bakejoon Online Judge

Statistiques

Bakejoon Online Judge is an online judge where users bake and submit bread that satisfies unusual conditions.

As a user of Bakejoon Online Judge, you must submit $N$ fresh loaves of bread all at once satisfying the following conditions.

  • At most one loaf of bread can be baked at a time, and once you start baking a loaf, you cannot stop it midway.

  • Each loaf of bread has three characteristics: preparation time $P_i$, baking time $T_i$, and freshness duration $F_i$.

    • Since preparation is needed before baking, the $i$-th loaf can be baked starting from time $P_i$.
    • It takes $T_i$ units of time to bake the $i$-th loaf.
    • The $i$-th loaf remains fresh for $F_i$ units of time from the moment it is fully baked.

More precisely, if you start baking the $i$-th loaf at time $t \ge P_i$, then it becomes fully baked at time $t+T_i$, and it remains fresh from time $t+T_i$ to time $t+T_i+F_i$. The loaf is also fresh at the times $t+T_i$ and $t+T_i+F_i$.

Given the characteristics of the $N$ loaves of bread, determine whether it is possible to submit all loaves while they are fresh, and if it is possible, find the earliest time at which they can be submitted.

Input

The first line contains the number of loaves of bread $N$.

$$1 \le N \le 200\,000$$

Each of the next $N$ lines contains three integers $P_i$, $T_i$, and $F_i$, separated by spaces: the preparation time, baking time, and freshness duration of each loaf of bread.

$$0 \le P_i,T_i,F_i \le 10^9$$

Output

Print the earliest time at which all $N$ loaves of bread can be submitted while they are fresh. If it is impossible to submit all $N$ loaves while they are fresh, print -1 instead.

Examples

Example 1

Input

4
2 0 4
4 1 2
1 2 0
0 3 7

Output

7

Example 2

Input

3
0 1 1
1 1 1
2 1 1

Output

-1

Note

In the first example, if the loaves are baked in the following way, all loaves can be submitted while they are fresh at time $7$.

Loaf Baking time interval Fresh time interval
1 instantaneously at time $5$ $[5,9]$
2 $[4,5]$ $[5,7]$
3 $[5,7]$ only at time $7$
4 $[0,3]$ $[3,10]$

Thus, loaf 4 is baked from time $0$ to time $3$, loaf 2 from time $4$ to time $5$, loaf 1 instantaneously at time $5$, and loaf 3 from time $5$ to time $7$; all four loaves are fresh at time $7$.

In the original figure, a gray X means that the loaf is not ready to be baked yet, a green P means that the preparation is complete, a red T means that the loaf is being baked in the oven, a blue F means that the loaf is fresh, and a yellow R means that the loaf is no longer fresh.

In the second example, no matter how the loaves are baked, it is impossible to make all $3$ loaves fresh at the same time.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.