Under construction

Still being built. Things may change

Repelling Monsters in the Magic Dungeon

Time limit0.5sMemory limit1024 MB

Summary
Bert starts at floor 0 and walks up or down to clear monsters, each costing w per hour until cleared; the task is to minimize total drained power over the walk.
Level

Medium7 of 10

Topics
Greedy, Dynamic programming, Sorting
Solved
No attempts yet

Problem

Bert is a guardian of a magic dungeon. The dungeon has floors 1 to 10810^8 above ground (written as positive integers from 11 to 10810^8) and floors 1 to 10810^8 below ground (written as negative integers from −1-1 down to −108-10^8). Bert's lodge is on floor 0.

Recently, monsters that drain magic power from the dungeon started appearing, so the Magic Association assigned Bert to exterminate them. There are NN kinds of monsters in the dungeon, numbered 1 to NN. The ii-th kind lives on floor pip_i and takes wiw_i units of magic power from the dungeon every hour. Two different kinds of monsters never live on the same floor.

Bert starts on floor 0 and can move up or down one floor per hour. As soon as he arrives on a floor, he uses a powerful spell that instantly removes all monsters on that floor. Bert wants to minimize the total magic power the monsters take.

Input

The first line contains the number of test cases TT.

The first line of each test case contains NN. The next NN lines each contain pip_i and wiw_i, separated by a space.

Output

For each test case, print the answer on its own line.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤N≤1 0001 \le N \le 1\,000
  • For all ii with 1≤i≤N1 \le i \le N:
    • −100 000 000≤pi≤100 000 000-100\,000\,000 \le p_i \le 100\,000\,000
    • pi≠0p_i \neq 0
    • 1≤wi≤1 000 0001 \le w_i \le 1\,000\,000
  • For all i,ji, j with 1≤i<j≤N1 \le i < j \le N: pi≠pjp_i \neq p_j

Examples1

  1. Example 1

    Input
    6
    3
    5 1
    10 2
    15 3
    4
    -4 1
    -13 14
    4 1
    8 1
    4
    -4 1
    -13 1
    4 1
    8 1
    3
    1 3
    10 2
    -100 5
    3
    1 3
    10 2
    -15 50
    1
    10 1
    
    Expected output
    61
    196
    57
    614
    323
    10