File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed
Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+
7+ // IO field
8+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
9+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
10+ static StringTokenizer st = new StringTokenizer (" " );
11+
12+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
13+ static String nextToken() throws Exception {
14+ while (! st. hasMoreTokens()) nextLine();
15+ return st. nextToken();
16+ }
17+ static int nextInt() throws Exception { return Integer . parseInt(nextToken()); }
18+ static long nextLong() throws Exception { return Long . parseLong(nextToken()); }
19+ static double nextDouble() throws Exception { return Double . parseDouble(nextToken()); }
20+ static void bwEnd() throws Exception {bw. flush();bw. close();}
21+
22+ // Additional field
23+
24+ static int N ;
25+ static long [] A ;
26+ static PriorityQueue<long[]> Q ;
27+
28+ public static void main(String [] args) throws Exception {
29+
30+ ready();
31+ solve();
32+
33+ bwEnd();
34+
35+ }
36+
37+ static void ready() throws Exception {
38+
39+ N = nextInt();
40+ A = new long [N + 1 ];
41+ for (int i= 1 ;i<= N ;i++ ) A [i] = nextInt();
42+ Q = new PriorityQueue<> ((a,b) - > Long . compare(a[0 ]* a[1 ], b[0 ]* b[1 ]));
43+
44+ }
45+
46+ static void solve() throws Exception {
47+
48+ if (N == 1 ){
49+ bw. write(" 0" );
50+ return ;
51+ }
52+
53+ long ans = 0 ;
54+ for (int i= 1 ;i<= N ;i++ ){
55+ ans += A [i];
56+ Q . offer(new long []{A [i], 3 });
57+ }
58+ for (int i= 2 ;i< N ;i++ ){
59+ long [] x = Q . poll();
60+ ans += x[0 ]* x[1 ];
61+ Q . offer(new long []{x[0 ], x[1 ]+ 2 });
62+ }
63+ bw. write(ans + " \n " );
64+
65+ }
66+
67+ }
68+ ```
You can’t perform that action at this time.
0 commit comments