Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Greedy

Maximum Units on a Truck

Easy · Greedy · Asked at Amazon, TCS, Infosys, Wipro

There are n types of boxes: type i has b[i] boxes, each holding u[i] units. A truck can carry at most T boxes. Print the maximum total number of units it can carry.

Input

First line: n T. Next n lines: b[i] u[i].

Output

The maximum number of units.

Constraints

Every box takes one place, so take the boxes with the most units first: sort the types by u[i] descending and fill the truck.

Example 1

Input

3 4
1 3
2 2
3 1

Output

8

Example 2

Input

4 10
5 10
2 5
4 7
3 9

Output

91

Hints

  1. Every box uses one slot no matter how many units it holds.
  2. So fill the truck with the richest boxes first.

Approach

Sort the box types by units per box, largest first. Take min(available boxes, remaining space) of each type, adding boxes × units to the total, until the truck is full. Exchanging any box for a richer unused one can only help, so this greedy is optimal. Totals pass 2^31 — use 64-bit integers.

Complexity: O(n log n) time · O(n) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    long long T;
    cin >> n >> T;
    vector<pair<long long, long long>> t(n); // (units, boxes)
    for (auto &p : t) cin >> p.second >> p.first;
    sort(t.rbegin(), t.rend());
    long long total = 0;
    for (auto &x : t) {
        long long u = x.first, b = x.second;
        if (T == 0) break;
        long long take = min(b, T);
        total += take * u;
        T -= take;
    }
    cout << total << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, T = data[0], data[1]
types = sorted(((data[3 + 2 * i], data[2 + 2 * i]) for i in range(n)), reverse=True)
total = 0
for units, boxes in types:
    if T == 0:
        break
    take = min(boxes, T)
    total += take * units
    T -= take
print(total)

Java solution

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        long T = (long) in.nval;
        long[][] t = new long[n][2]; // boxes, units
        for (int i = 0; i < n; i++) {
            in.nextToken();
            t[i][0] = (long) in.nval;
            in.nextToken();
            t[i][1] = (long) in.nval;
        }
        Arrays.sort(t, (a, b) -> Long.compare(b[1], a[1]));
        long total = 0;
        for (long[] x : t) {
            if (T == 0) break;
            long take = Math.min(x[0], T);
            total += take * x[1];
            T -= take;
        }
        System.out.println(total);
    }
}

Solve Maximum Units on a Truck online · All practice problems