Dsa
Greedy
Warm-up
Library Late Fees
You have N books that are all overdue at the library. For each book i, the library charges a daily late fee of F_i. You can only return one book per day, starting today (Day 0). A book's fee is charged at the end of each day for every day it has not yet been returned. You want to determine the order in which to return the books to minimize the total amount of fees you will pay.
Input Format:
A single line of space-separated integers representing the daily late fees for each book.
Output Format:
Print the minimum possible total late fees.
Example:
Input:
1 5 2
Output:
4
(Explanation: Day 0: Return book with fee 5. Day end: 1+2=3 fee. Day 1: Return book with fee 2. Day end: 1 fee. Day 2: Return book with fee 1. Day end: 0 fee. Total = 4.)
Key concepts
sortinggreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now