Most of us are familiar with google codejam, those who don’t visit https://code.google.com/codejam for more information. This is the first problem from Online Round 1A 2013, top 1000 from this round will be eligible for next online round.
Problem Statement
You’ve got a very busy calendar today, full of important stuff to do. You worked hard to prepare and make sure all the activities don’t overlap. Now it’s morning, and you’re worried that despite all of your enthusiasm, you won’t have the energy to do all of this with full engagement.
You will have to manage your energy carefully. You start the day full of energy – E joulesof energy, to be precise. You know you can’t go below zero joules, or you will drop from exhaustion. You can spend any nonnegative, integer number of joules on each activity (you can spend zero, if you feel lazy), and after each activity you will regain R joules of energy. No matter how lazy you are, however, you cannot have more than E joules of energy at any time; any extra energy you would regain past that point is wasted.
Now, some things (like solving Code Jam problems) are more important than others. For the ith activity, you have a value v_{i} that expresses how important this activity is to you. The gain you get from each activity is the value of the activity, multiplied by the amount of energy you spent on the activity (in joules). You want to manage your energy so that your total gain will be as large as possible.
Note that you cannot reorder the activities in your calendar. You just have to manage your energy as well as you can with the calendar you have.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case is described by two lines. The first contains three integers: E, the maximum (and initial) amount of energy, R, the amount you regain after each activity, and N, the number of activities planned for the day. The second line contains N integers v_{i}, describing the values of the activities you have planned for today.
Output
For each test case, output one line containing “Case #x: y“, where x is the case number (starting from 1) and y is the maximum gain you can achieve by managing your energy that day.
Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ E ≤ 5.
1 ≤ R ≤ 5.
1 ≤ N ≤ 10.
1 ≤ v_{i} ≤ 10.
Large dataset
1 ≤ E ≤ 10^{7}.
1 ≤ R ≤ 10^{7}.
1 ≤ N ≤ 10^{4}.
1 ≤ v_{i} ≤ 10^{7}.
Sample
Input  Output 
3 

In the first case, we can spend all 5 joules of our energy on the first activity (for a gain of 10), regain 2 and spend them on the second activity. In the second case, we spend 2 joules on the first activity, regain them, and spend 5 on the second. In the third case, our regain rate is equal to the maximum energy, meaning we always recover all energy after each activity – so we can spend full 3 joules on each activity.
Solution with Java Code and Explanation
We need to decide the energy we should spend at every task, to decide we will first get the number of tasks after which we can get back the full energy, i.e.n = upper(E/R). Now we see if a task with higher value exists in the next n tasks, if not then we spend all energy in the current task else if we get a task with higher value at pos nextMax, then we should try to maximize the energy for this task, so
The energy we can spend at the current task x = <Current Energy> + <energy we can get while moving to nextMax> – E
Here is the Java code for Manage Your Energy problem in Google codejam Online Round 1A 2013
package googlecodejam2013; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.math.BigInteger; import java.util.ArrayList; public class ManageYourEnergy { public static String getMaxGain(int E, int R, ArrayList<Integer> vArr){ int current = E; if(R > E) R=E; BigInteger totalGain = new BigInteger("0"); int n = (E+R1)/R; for(int i = 0; i < vArr.size(); i++){ int v = vArr.get(i); int nextMax = getNextMaxPos(vArr, v, i, n); if(nextMax != 1){ int energy = (nextMax i)*R; //Energy we can spend on current task int x = current+energyE; if(x < 0){ x = 0; } if(x > current){ x = current; } BigInteger gain = new BigInteger(""+x); gain = gain.multiply(new BigInteger(""+v)); totalGain = totalGain.add(gain); //Enery for next task current = current x + R; if(current > E) { current = E; } }else{ BigInteger gain = new BigInteger(""+current); gain = gain.multiply(new BigInteger(""+v)); totalGain = totalGain.add(gain); current = R; } } return totalGain.toString(); } private static int getNextMaxPos(ArrayList<Integer> vArr, int val, int curPos, int n){ int maxPos = 1; for(int i = curPos+1; i < vArr.size() && i < curPos+1+n; i++){ if(vArr.get(i) >= val){ return i; } } return maxPos; } public static void main(String[] argv) { try { long startTime = System.currentTimeMillis(); Reader reader = new FileReader("sample.in"); BufferedReader bufReader = new BufferedReader(reader); String x = bufReader.readLine(); int numOfTestCases = Integer.parseInt(x); int count = 0; File file = new File("sample.out"); FileWriter writer = new FileWriter(file); for(count =1; count<= numOfTestCases; count++) { ArrayList<Integer> firstLine = getIntegerList(bufReader.readLine()); String output = getMaxGain(firstLine.get(0), firstLine.get(1), getIntegerList(bufReader.readLine())); writer.write("Case #" + count + ": " + output+"\n"); System.out.println("Case #" + count + ": " + output); } writer.close(); System.out.println("Total time = " + (System.currentTimeMillis()  startTime)); } catch (Exception e) { e.printStackTrace(); } } private static ArrayList<Integer> getIntegerList(String s) { ArrayList<Integer> intList = new ArrayList<Integer>(); String[] strArr = s.split(" "); for (String str : strArr) { intList.add(Integer.parseInt(str.trim())); } return intList; } }