Dsa
Graphs Bfs
Medium
Filling a Glass
You have an empty glass, and there are several buckets of water of various capacities. You can fill the glass with water from the buckets until it's full or you run out of buckets. Water may overflow from the glass if you fill it too much (spillover). Write a program to find the minimum time to fill the glass to a given capacity, if using the water in each bucket takes a certain amount of time.
Input format:
- The first line contains the capacity of the glass.
- The second line contains an integer N, the number of buckets.
- The next N lines contain two integers, water_capacity and time_per_fill.
Output format:
- Output the minimum time required to fill the glass, or -1 if it's impossible.
Example:
Input:
5
3
2 1
1 3
3 2
Output:
3
Key concepts
graphsbfswater_fill
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