-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixMaxSwap.java
More file actions
90 lines (83 loc) · 2.49 KB
/
Copy pathPrefixMaxSwap.java
File metadata and controls
90 lines (83 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.IOException;
import java.io.InputStream;
public class PrefixMaxSwap{
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
int t = fs.nextInt();
StringBuilder out = new StringBuilder();
for (int caseIdx=0;caseIdx<t;caseIdx++){
int n=fs.nextInt();
int[] arr=new int[n];
for(int i=0;i<n; i++)arr[i] = fs.nextInt();//input
long best = prefixValue(arr);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++){
swap(arr,i,j);
best=Math.max(best, prefixValue(arr));
swap(arr,i,j);
}
}
if(caseIdx+1<t)out.append(best).append('\n');
else out.append(best);
}
System.out.print(out);
}
private static long prefixValue(int[] arr) {
long sum = 0;
int currentMax = Integer.MIN_VALUE;
for (int value : arr) {
if (value > currentMax) {
currentMax = value;
}
sum += currentMax;
}
return sum;
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
// skeleton for Fast Input
private static final class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0;
private int len = 0;
FastScanner(InputStream in) {
this.in = in;
}
int nextInt() throws IOException {
return (int) nextLong();
}
long nextLong() throws IOException {
int c;
while ((c = read()) <= ' ') {
if (c == -1) {
return Long.MIN_VALUE;
}
}
boolean neg = false;
if (c == '-') {
neg = true;
c = read();
}
long val = 0;
while (c > ' ') {
val = val * 10 + (c - '0');
c = read();
}
return neg ? -val : val;
}
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) {
return -1;
}
}
return buffer[ptr++];
}
}
}