Under construction

Still being built. Things may change

N-Sumaehwa Sword Technique

Time limit2sMemory limit1024 MB

Summary
Order N weighted line segments so that the total cost is minimized, where each stroke costs its weight times one plus the number of unperformed segments it crosses.
Level

Medium6 of 10

Topics
Greedy, Geometry
Solved
No attempts yet

Problem

Elder Woo Kyung of Hwasan Sect created a new martial art, N-Sumaehwa Sword Technique. It is an advanced version of the Twenty-Four Sumaehwa Sword Technique and consists of NN strokes (cuts made with the sword).

The technique is performed on a two-dimensional plane. Stroke ii cuts in a straight line from point sis_i to point eie_i. The path the sword travels is called the path of stroke ii.

While performing the technique, each stroke costs internal energy according to the following principle.

  • Stroke ii has a weight wiw_i. Performing stroke ii costs (m+1)×wi(m+1)\times w_i units of internal energy, where mm is the number of strokes not yet performed at that moment whose paths intersect the path of stroke ii.

To complete the N-Sumaehwa Sword Technique, each of the NN strokes must be performed exactly once, in any order. Elder Woo Kyung wants to complete the technique using as little internal energy as possible. Find the minimum total internal energy needed to complete the N-Sumaehwa Sword Technique.

Input

The first line contains the number of strokes NN. (1≤N≤2 500)(1 \le N \le 2\,500)

Each of the next NN lines gives, for stroke ii, the coordinates (sxi,syi)(sx_i, sy_i) of sis_i, the coordinates (exi,eyi)(ex_i, ey_i) of eie_i, and the weight wiw_i, separated by spaces. (−109≤sxi,syi,exi,eyi≤109(-10^9 \le sx_i, sy_i, ex_i, ey_i \le 10^9; 1≤wi≤109)1 \le w_i \le 10^9)

The 2N2N points are all distinct, and no three of them lie on the same straight line.

All numbers in the input are integers.

Output

Print the minimum total internal energy needed to complete the N-Sumaehwa Sword Technique.

Examples1

  1. Example 1

    Input
    3
    1 1 3 3 1
    1 3 3 1 2
    6 7 8 9 1
    
    Expected output
    5