This page is still under construction.

Parts of this page are still being built. What you see may change.

Toy Shopping

Time limit1sMemory limit128 MB

Summary
Select three of N toys maximizing the sum of joy/price ratios, and output the total price plus the three indices sorted by ratio.
Level

Medium7 of 10

Topics
Sorting, Greedy, Dynamic programming
Solved
No attempts yet

Problem

Bessie wants to buy some toys. She has saved her allowance for years and has a huge stash of cash, but she is frugal and wants the best value for her money. She has decided to buy exactly three different toys out of the NN (3≤N≤25,0003 \le N \le 25{,}000) toys on offer.

Toy ii gives Bessie JiJ_i (0≤Ji≤1,000,0000 \le J_i \le 1{,}000{,}000) microbundles of joy and costs PiP_i (0<Pi≤100,000,0000 < P_i \le 100{,}000{,}000). Bessie has enough money to buy any three toys she likes.

Bessie wants to maximize the total of her happy-frugal metric — defined as Ji/PiJ_i / P_i (joy divided by price) — over the three toys she picks. Help her decide which toys to buy. The answer is guaranteed to be unique.

For example, suppose six toys are on offer, with the following happy-frugal metrics:

  i    Joy    Price    Happy-Frugal Metric
  -    ---    -----    -------------------
  1      0      521    0.00000
  2    442      210    2.10476...
  3    119      100    1.19000
  4    120      108    1.11111...
  5    619      744    0.83198...
  6     48       10    4.80000

Bessie would choose toy 6 (metric 4.80), toy 2 (metric 2.10), and toy 3 (metric 1.19).

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains two space-separated integers JiJ_i and PiP_i.

Output

  • Line 1: the total price Bessie has to pay (the sum of the prices of the three chosen toys).
  • Lines 2 to 4: the 1-based indices of the three toys Bessie should buy, one per line, in descending order of the happy-frugal metric.

Examples1

  1. Example 1

    Input
    6
    0 521
    442 210
    119 100
    120 108
    619 744
    48 10
    
    Expected output
    320
    6
    2
    3