+- 2,147,483,647 까지가 int이다.
길이가 50,000인 코스가 100,000개 존재하므로 5,000,000,000을 수용해야 한다.
따라서, long 사용
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[] A;
static long[] S;
static long[] E;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
A = new long[n];
S = new long[n];
E = new long[n];
long result = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
A[i] = Integer.parseInt(st.nextToken());
result += A[i];
}
S[0] = A[0] - 1;
for (int i = 1; i < n; i++) {
S[i] = S[i - 1] + A[i];
}
E[n - 1] = result + A[n - 1] - 1;
for (int i = n - 2; i >= 0; i--) {
E[i] = E[i + 1] + A[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (k == 0) {
ans = 1;
} else if (k <= S[i]) {
ans = i + 1;
} else if (k <= E[i]) {
ans = i + 1;
}
}
System.out.println(ans);
}
}
반례를 찾는데 시간이 오래 걸려서 블로그를 참고해서 풀었다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[] A;
static boolean check;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
A = new long[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
A[i] = Integer.parseInt(st.nextToken());
k -= A[i];
if (k < 0) {
System.out.println(i + 1);
return;
}
}
if (!check) {
for (int i = n - 1; i >= 0; i--) {
k -= A[i];
if (k < 0) {
System.out.println(i + 1);
return;
}
}
}
}
}