백준(실버5) - 17521. Byte Coin(실버5)
로직은 간단하다. 다음날에 오르면 사고, 떨어지면 판다.
그런데 두번이나 틀렸다.. 이유는 w와 coin의 값이 커져서 int형의 범위를 넘어갈 수 있기 때문이다..
long으로 선언해주어야 한다!!!!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long w = Integer.parseInt(st.nextToken());
long coin = 0;
int[] chart = new int[n];
for(int i=0; i<n; i++) {
chart[i] = Integer.parseInt(br.readLine());
}
for(int i=0; i<n-1; i++) {
if(chart[i] < chart[i+1]) {//다음날 올랐어
if(w>=chart[i]) {
coin += w/chart[i];
w %= chart[i];
}
}else if(chart[i]> chart[i+1]) {//떨어짐
w += coin * chart[i];
coin = 0;
}
}
w += coin * chart[n-1];
System.out.println(w);
}
}