-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSACO11.txt
71 lines (58 loc) · 1.79 KB
/
USACO11.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
USER: achyuta2
PROB: milk
LANG: JAVA
*/
import java.io.*;
import java.util.StringTokenizer;
public class milk {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("milk.in"));
PrintWriter out = new PrintWriter(new FileWriter("milk.out"));
StringTokenizer k = new StringTokenizer(f.readLine());
int milkNeed = Integer.parseInt(k.nextToken());
int farmNum = Integer.parseInt(k.nextToken());
int[] prices1= new int[farmNum];
int milkCollect=0;
int spent=0;
int[][] prices = new int[farmNum][2];
for(int i=0; i<farmNum; i++){
StringTokenizer z = new StringTokenizer(f.readLine());
prices[i][0]=Integer.parseInt(z.nextToken());
prices1[i]=prices[i][0];
prices[i][1]=Integer.parseInt(z.nextToken());
}
int a;
int b;
int milkLeft;
while(milkCollect<milkNeed){
b=smallest(prices1);
milkLeft=(milkNeed-milkCollect);
if(prices[b][1]<=(milkLeft)){
milkCollect+=prices[b][1];
spent+=(prices[b][1]*prices[b][0]);
prices1[b]=1000000000;
}else{
milkCollect+=milkLeft;
spent+=(milkLeft*prices[b][0]);
prices1[b]=1000000000;
}
}
out.println(spent);
out.close();
}
public static int smallest (int[] ar){
int g=1000000000;
for(int f:ar){
if(g>=f){
g=f;
}
}
for(int i=0;i<ar.length;i++){
if (ar[i]==g){
return i;
}
}
return -1;
}
}