// Arup Guha
// 3/12/2024
// Solution for COP 3503 Exam 2 Question 5

import java.util.*;

public class testcoffee {

	public static void main(String[] args) {
	
		// A basic test.
		coffeecup[] test = new coffeecup[3];
		test[1] = new coffeecup(100,150,200000000);
		test[2] = new coffeecup(3,50,400000000);
		test[0] = new coffeecup(500,501,300000000);
		System.out.println(maxProfit(test, 600000001));
	}

	public static long maxProfit(coffeecup[] list, int totalcups) {

		// Do this first so our greedy will work.
		Arrays.sort(list);
		long res = 0;
		
		// Go through in order from most profit to least.
		for (int i=0; i<list.length; i++) {
			
			// See the most we can sell of this type of coffee.
			int buy = Math.min(totalcups, list[i].numcups);
			
			// Subtract out from how much we can sell.
			totalcups -= buy;
			
			// Update our profit.
			res = res + ((long)buy)*(list[i].sell-list[i].cost);
			
			// Time to get out!
			if (totalcups == 0) break;
		}

		return res;
	}
}

class coffeecup implements Comparable<coffeecup> {
     public int cost;
     public int sell;
     public int numcups;

     // Constructor 
	 public coffeecup(int c, int s, int n) {
		cost = c;
		sell = s;
		numcups = n;
	 }

	// Sorts by profit, largest to smallest.
     public int compareTo(coffeecup other) {

		// Calculate both profits.
		int p1 = sell - cost;
		int p2 = other.sell - other.cost;

		// This prioritizes higher profits first.
		return p2 - p1;
     }
}
