Under construction

Still being built. Things may change

Jordan

Time limit1sMemory limit512 MB

Summary
Given N segments and the weight sum inside each, find the minimum and maximum possible total weight of all points, or -1 -1 if the records conflict.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals, Prefix sum
Solved
No attempts yet

Statement

Camille likes to spend time with numbers. This morning, he drew an infinite number line on a whiteboard and marked on it a finite set of distinct points with real coordinates. For each point, he chose a non-negative real value and called it the weight of the point. Then he selected a segment [li;ri][l_i; r_i] on the number line NN times and counted the sum of weights of all points lying inside that segment. A point xx lies inside the segment [l;r][l; r] if l≤x≤rl \le x \le r. He wrote all these calculations in his notebook and left the room for lunch.

Henri likes to tease Camille, and he could not pass up this chance. While Camille was enjoying frog legs and young Bordeaux, Henri took a sponge and erased the whole board. When the prank was revealed, Camille's disappointment was beyond words.

You accidentally witnessed this prank and feel a little ashamed for not stopping it. You also remember the good days you spent with Bernhard and Camille when you were freshmen, so you decided to help him. Given his records, find the minimum and maximum possible sum of weights of all the points. Assume that every marked point belongs to at least one segment selected by Camille.

Input

The first line contains a single integer NN, the number of records in Camille's notebook (1≤N≤10001 \le N \le 1000).

Each of the next NN lines contains three integers lil_i, rir_i, and sis_i. Here lil_i and rir_i are the borders of the segment, and sis_i is the sum of weights of the points lying inside it.

(−109≤li≤ri≤109-10^9 \le l_i \le r_i \le 10^9, 0≤si≤1090 \le s_i \le 10^9)

Output

Print the minimum and the maximum possible total weight of all the points. If the records are inconsistent, print two numbers −1-1 instead.

Notes

In the first case, a single point at x=2x = 2 with weight 1 gives the total weight 1. Two points with weight 1 at x=1.5x = 1.5 and x=2.5x = 2.5 give the total weight 2.

In the second case, the input is obviously inconsistent.

Examples2

  1. Example 1

    Input
    2
    1 2 1
    2 3 1
    
    Expected output
    1 2
    
  2. Example 2

    Input
    2
    1 2 1
    1 2 2
    
    Expected output
    -1 -1