Secret Lines
Time limit1sMemory limit512 MB
Sum |Xa - Xb| * max(Va, Vb) over all pairs of members with given power and position.
- Level
Medium7 of 10
- Topics
- Sorting, Divide and conquer, Prefix sum
- Solved
- No attempts yet
Problem
The members of a club are connected to each other by secret lines. There are N members, and each member has a nerd power V and a position X on a one dimensional axis. Laying one line needs a special material that withstands the nerd power of both endpoints. The number of units of material for the line between member a and member b is the distance between them multiplied by the larger of their two nerd powers.
All of the members are on good terms, so every pair of two different members needs exactly one direct line. Compute the total number of units of material needed to lay all of the lines.
Input
The first line contains the number of members N ().
Each of the next N lines contains one member's nerd power V and position X, separated by a space ().
Output
Print the total number of units of material on one line. The total can exceed the range of a 32 bit integer.