OJ
[BOJ] 27514 1차원 2048 (JAVA)
P3PP4
2023. 3. 16. 10:00
https://www.acmicpc.net/problem/27514
27514번: 1차원 2048
첫 줄에 흐즈로가 정의한 연산을 $0$번 이상 수행해 만들 수 있는 가장 큰 최댓값을 출력하세오. 문제의 답은 $2^{62}$보다 크지 않음이 보장됩니다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static int N;
static long pre, now, max;
public static void main(String[] args) throws Exception {
PriorityQueue<Long> pq = new PriorityQueue<>();
N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < N; i++) {
pq.offer(Long.parseLong(st.nextToken()));
} // end of input
while(!pq.isEmpty()) {
now = pq.poll();
if (now == 0) continue;
max = Math.max(max, now);
if (pre == now) {
pre = 0;
pq.offer(now * 2);
} else pre = now;
}
System.out.println(max);
}
}