Repository navigation
Expand file tree
/
Copy pathMax_different.java
More file actions
52 lines (47 loc) · 1.43 KB
/
Copy pathMax_different.java
File metadata and controls
52 lines (47 loc) · 1.43 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
import java.io.*;
import java.util.StringTokenizer;
public class Max_different {
static int[] nums;
static boolean[] visited;
static int[] selected;
static int n;
static int result = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
nums = new int[n];
visited = new boolean[n];
selected = new int[n];
for(int i=0; i<n; i++){
nums[i] = Integer.parseInt(st.nextToken());
}
dfs(0);
bw.write(Integer.toString(result));
bw.flush();
bw.close();
br.close();
}
public static void dfs(int count) {
if(count == n) {
result = Math.max(getResult(), result);
return;
}
for(int i = 0; i < n; i++){
if(!visited[i]){
visited[i] = true;
selected[count] = nums[i];
dfs(count + 1);
visited[i] = false;
}
}
}
public static int getResult(){
int sum = 0;
for(int i = 0; i < n - 1; i++){
sum += Math.abs(selected[i] - selected[i + 1]);
}
return sum;
}
}