Created
April 27, 2013 11:31
-
-
Save Kenji-H/5472781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.*; | |
import static java.util.Arrays.*; | |
import static java.math.BigInteger.*; | |
import java.io.*; | |
import java.util.*; | |
@SuppressWarnings("unused") | |
public class B { | |
Scanner sc = new Scanner(System.in); | |
void solve() { | |
int E = sc.nextInt(); | |
int R = sc.nextInt(); | |
int N = sc.nextInt(); | |
int[] v = new int[N]; | |
for (int i = 0; i < N; i++) | |
v[i] = sc.nextInt(); | |
if (R > E) | |
R = E; | |
long[] dp = new long[N]; | |
dp[0] = E; | |
for (int i = 1; i < N; i++) { | |
dp[i] = R; | |
for (int j = i-1; j >= 0; j--) { | |
if (v[j] >= v[i]) | |
break; | |
if (dp[i] + dp[j] <= E) { | |
dp[i] += dp[j]; | |
dp[j] = 0; | |
} else { | |
dp[j] -= E - dp[i]; | |
dp[i] = E; | |
} | |
} | |
} | |
long ret = 0; | |
for (int i = 0; i < N; i++) | |
ret += dp[i] * v[i]; | |
System.out.println(ret); | |
} | |
void run() { | |
int caseN = sc.nextInt(); | |
for (int caseID = 1; caseID <= caseN; caseID++) { | |
System.out.printf("Case #%d: ", caseID); | |
System.err.printf("Solving Case #%d...\n", caseID); | |
solve(); | |
System.out.flush(); | |
} | |
} | |
void debug(Object... os) { | |
System.err.println(deepToString(os)); | |
} | |
public static void main(String[] args) { | |
new B().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment