-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBerryPicking.java
More file actions
58 lines (58 loc) · 1.62 KB
/
BerryPicking.java
File metadata and controls
58 lines (58 loc) · 1.62 KB
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
import java.io.*;
import java.util.*;
public class BerryPicking {
static int mod;
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("berries.in"));
String[] temp = br.readLine().split(" ");
int numTrees = Integer.parseInt(temp[0]);
int numBaskets = Integer.parseInt(temp[1]);
temp = br.readLine().split(" ");
ArrayList<Integer> trees = new ArrayList<Integer>(numTrees);
for (int i = 0; i < numTrees; i++)
trees.add(Integer.parseInt(temp[i]));
int count;
int maximum = 0;
int total;
Comparator<Integer> c = new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return (o2%mod) - (o1%mod);
}
};
for (int i = 1; i <= 1000; i++)
{
count = 0;
for (int j = 0; j < numTrees; j++)
{
count += trees.get(j) / i;
}
if (count < numBaskets/2)
break;
mod = i;
Collections.sort(trees, c);
System.out.println(count);
//Bessie takes the max berries that would be left over
//first give her extra baskets of b;
total = 0;
total += i * Math.min(count - numBaskets/2, numBaskets/2);
//then everything less than b
for (int j = 0; j < numBaskets - count && j < numTrees; j++)
{
System.out.println("J: " + j);
total += trees.get(j) % mod;
}
//maximum may not be the last one
if (total > maximum)
maximum = total;
}
//System.out.println(maximum);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("berries.out")));
pw.println(maximum);
pw.close();
br.close();
}
}