Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions khj20006/202506/05 BOJ P5 비 오는 날.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N;
static long[] A;
static PriorityQueue<long[]> Q;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception {

N = nextInt();
A = new long[N+1];
for(int i=1;i<=N;i++) A[i] = nextInt();
Q = new PriorityQueue<>((a,b) -> Long.compare(a[0]*a[1], b[0]*b[1]));

}

static void solve() throws Exception {

if(N == 1){
bw.write("0");
return;
}

long ans = 0;
for(int i=1;i<=N;i++){
ans += A[i];
Q.offer(new long[]{A[i], 3});
}
for(int i=2;i<N;i++){
long[] x = Q.poll();
ans += x[0]*x[1];
Q.offer(new long[]{x[0], x[1]+2});
}
bw.write(ans + "\n");

}

}
```