|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st = new StringTokenizer(""); |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static String nextToken() throws Exception { |
| 15 | + if(!st.hasMoreTokens()) nextLine(); |
| 16 | + return st.nextToken(); |
| 17 | + } |
| 18 | + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } |
| 19 | + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } |
| 20 | + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } |
| 21 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 22 | + |
| 23 | + // Additional field |
| 24 | + |
| 25 | + static int N, M, K; |
| 26 | + static long[] p; |
| 27 | + static long ans; |
| 28 | + |
| 29 | + public static void main(String[] args) throws Exception { |
| 30 | + |
| 31 | + ready(); |
| 32 | + solve(); |
| 33 | + |
| 34 | + bwEnd(); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + static void ready() throws Exception{ |
| 39 | + |
| 40 | + N = nextInt(); |
| 41 | + M = nextInt(); |
| 42 | + K = nextInt()+1; |
| 43 | + p = new long[N]; |
| 44 | + for(int i=0;i<N;i++) p[i] = nextInt(); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + static void solve() throws Exception{ |
| 49 | + |
| 50 | + ans = M; |
| 51 | + bck(M,0,0,0); |
| 52 | + bw.write(ans + "\n"); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + static void bck(long curMoney, long borrow, long count, int day) { |
| 57 | + if(day == N) { |
| 58 | + ans = Math.max(ans, curMoney); |
| 59 | + return; |
| 60 | + } |
| 61 | + if(count > 0) { |
| 62 | + bck(curMoney, borrow, count, day+1); |
| 63 | + long benefit = count * p[day]; |
| 64 | + curMoney += benefit; |
| 65 | + ans = Math.max(ans, curMoney); |
| 66 | + curMoney -= borrow; |
| 67 | + if(curMoney < 0) return; |
| 68 | + borrow = 0; |
| 69 | + count = 0; |
| 70 | + } |
| 71 | + bck(curMoney, borrow, count, day+1); |
| 72 | + borrow = curMoney * (K-1); |
| 73 | + curMoney *= K; |
| 74 | + count = curMoney / p[day]; |
| 75 | + curMoney -= count * p[day]; |
| 76 | + if(count > 0) bck(curMoney, borrow, count, day+1); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +``` |
0 commit comments