알고리즘

백준 24885번 주식 (JAVA)

박카스마시며코딩 2023. 4. 6. 14:45

https://www.acmicpc.net/problem/24885

 

24885번: 주식

첫째 줄에 과거에 머무를 수 있는 기간 $N(2\le N \le 10)$, 숭고한이 가지고 간 돈 $M(0 \le M \le 1\,000)$, 대출할 수 있는 한도 $K(1 \le K \le 4)$가 공백으로 구분되어 주어진다. 둘째 줄에 과거로 돌아간

www.acmicpc.net

 

 

package BOJ.simulation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_24885 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = stoi.apply(st.nextToken());
        long money = stoi.apply(st.nextToken());
        int k = stoi.apply(st.nextToken());
        int[] price = new int[n];
        st = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < n ; i++){
            price[i] = stoi.apply(st.nextToken());
        }
        long result = cal(price,n,money,k);
        System.out.println(result);
    }
    private static long cal(int[] price,int n,long money,int k){
        long rent = 0;
        long result = money;
        for(int i = 1 ; i < n ; i++){
            if(price[i-1] < price[i] && price[i-1] <= (k+1) * money){
                rent = money * k;
                long cnt = (money + rent) / price[i-1];
                money += (price[i] - price[i-1]) * cnt;
                continue;
            }
            if(price[i-1] >= price[i] && price[i-1] <= (k+1) * money){
                long temp = (k+1) * money + (price[i] - price[i-1]) * ((k+1) * money/price[i-1]);
                result = Math.max(result,temp);
            }
        }
        money += rent;
        result = Math.max(result,money);
        return result;
    }
}