CCCGuide

Problem S3: Lunch Concert

2021 · S3 · Original problem · DMOJ

Problem Description

It’s lunchtime at your school! Your NN friends are all standing on a long field, as they usually do. The field can be represented as a number line, with the iith friend initially at position PiP_i metres along it. The iith friend is able to walk in either direction along the field at a rate of one metre per WiW_i seconds, and their hearing is good enough to be able to hear music up to and including DiD_i metres away from their position. Multiple students may occupy the same positions on the field, both initially and after walking.

You’re going to hold a little concert at some position cc metres along the field (where cc is any integer of your choice), and text all of your friends about it. Once you do, each of them will walk along the field for the minimum amount of time such that they end up being able to hear your concert (in other words, such that each friend ii ends up within DiD_i units of cc).

You’d like to choose cc such that you minimize the sum of all NN of your friends’ walking times. What is this minimum sum (in seconds)? Please note that the result might not fit within a 32-bit integer.

Input Specification

The first line of input contains NN.

The next NN lines contain three space-separated integers, PiP_i, WiW_i, and DiD_i (1≤i≤N)(1 \le i \le N).

The following table shows how the available 15 marks are distributed.

4 marks1≤N≤20001 \le N \le 20000≤Pi≤20000 \le P_i \le 20001≤Wi≤10001 \le W_i \le 10000≤Di≤20000 \le D_i \le 2000
9 marks1≤N≤200 0001 \le N \leq 200\,0000≤Pi≤1 000 0000 \le P_i \le 1\,000\,0001≤Wi≤10001 \le W_i \le 10000≤Di≤1 000 0000 \le D_i \le 1\,000\,000
2 marks1≤N≤200 0001 \le N \leq 200\,0000≤Pi≤1 000 000 0000 \le P_i \leq 1\,000\,000\ 0001≤Wi≤10001 \le W_i \le 10000≤Di≤1 000 000 0000 \le D_i \le 1\,000\,000\,000

Output Specification

Output one integer which is the minimum possible sum of walking times (in seconds) for all NN of your friends to be able to hear your concert.

Sample Input 1

1
0 1000 0

Output for Sample Input 1

0

Explanation of Output for Sample Input 1

If you choose c=0c = 0, your single friend won’t need to walk at all to hear it.

Sample Input 2

2
10 4 3
20 4 2

Output for Sample Input 2

20

Explanation of Output for Sample Input 2

One possible optimal choice of cc is 1414, which would require your first friend to walk to position 1111 (taking 4×1=44 \times 1 = 4 seconds) and your second friend to walk to position 1616 (taking 4×4=164 \times 4 = 16 seconds), for a total of 2020 seconds.

Sample Input 3

3
6 8 3
1 4 1
14 5 2

Output for Sample Input 3

43