EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2023
  3. Preliminary Round

Codey and Crimes

https://www.hackerrank.com/contests/codenection-2023-preliminary-round-open-category/challenges/cn-c8

PreviousCodey and Number GridNextFinal Round

Last updated 5 months ago

Question

One night, Codey found itself at a 7-11 convenience store, faced with the desire to purchase n items. Each item took aia_iai​ seconds to process, and they were priced at bib_ibi​ ringgit(s). It was a challenging time for Codey, and its funds were tight, barely enough to afford all n items. Unfortunately, Codey made a regrettable decision that night: it chose to steal.

Here's what it did: For each second the cashier took to process one of their items, Codey managed to steal another item without paying for it. Its goal was to acquire all n items and save as much money as possible.

Now, as Codey reflects on their actions, it wants to know the total amount it ultimately ended up paying that night. Can you help Codey recall the cost of its choices?

Input Format

The first line contains an integer n, where n represents the number of items Codey purchased.

The following n lines describe each item, where each of them contains two integers ai,bia_i, b_iai​,bi​, which represents the time it takes to process item i, and the price of item i, respectively.

Constraints

1≤n≤2∗1031 \le n \le 2 *10^31≤n≤2∗103
1≤ai≤2∗1031 \le a_i \le 2 *10^31≤ai​≤2∗103

Output Format

Output a single integer representing the total amount that Codey ultimately ended up paying at the end of the night.

Sample Inputs:

Input

3
5 50
2 10
1 20

Output

10

Explanation

Codey bought the second item, which cost him 10 ringgits, and it took 2 seconds to process it. During these 2 seconds, Codey stole all the remaining items.

Input

5
0 30
0 123
8 30
2 100
1 100

Output

30

Solution 1 - Greedy Algorithm

Solution 2 - A* Algorithm

1≤bi≤1091 \le b_i \le 10^91≤bi​≤109
🇲🇾