// Google Code Jam Round 1A 2013 B Manage your Energy (small-input)
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Google Code Jam
public class GoogleCode2 {
private static MyScanner in;
private static PrintStream out;
static int caseNum;
private static void solve() throws IOException
{
int C = in.nextInt();
in.nextLine();
for (int i = 0; i < C; i++) {
caseNum = i + 1;
out.print("Case #" + caseNum + ": ");
solveCase();
}
}
static long maxGain;
private static void solveCase() throws IOException {
long E = in.nextInt();
long R = in.nextInt();
if (R > E)
R = E;
int N = in.nextInt();
long[] v = new long[N];
for (int i = 0; i < N; i++) {
v[i] = in.nextLong();
}
maxGain = 0;
calcGain(E, E, R, v, 0, 0);
out.println(maxGain);
}
private static void calcGain(long maxE, long curE, long R, long[] v,
long lastGain, int idxv)
{
if (idxv == v.length - 1) {
long g = curE * v[v.length - 1];
long gain = lastGain + g;
if (gain > maxGain)
maxGain = gain;
return;
}
for (long spent = 0; spent <= curE; spent++) {
long g = spent * v[idxv];
long gain = lastGain + g;
long newE = curE - spent + R;
newE = Math.min(newE, maxE);
calcGain(maxE, newE, R, v, gain, idxv + 1);
}
return;
}
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.txt");
}
else {
in = new MyScanner();
out = System.out;
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
No comments:
Post a Comment