-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_10819.java
50 lines (39 loc) · 1.25 KB
/
number_10819.java
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
import java.util.Arrays;
import java.util.Scanner;
public class number_10819 {
static int max=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int list[] = new int[n];
int temparr[] = new int[n];
Boolean visited[] = new Boolean[n];
Arrays.fill(visited, false);
for(int i=0;i<n;i++){
list[i] = sc.nextInt();
}sc.close();
DFS_1(list, temparr, visited, 0);
System.out.println(max);
}
static int calsum12(int arr[],int arr_size){
int temp=0;
for(int i=0;i<arr_size-1;i++){
temp += Math.abs(arr[i]-arr[i+1]);
}
return temp;
}
static void DFS_1(int arr[],int temparr[],Boolean visited[],int current_idx){
if(current_idx==arr.length){
max = Math.max(max,calsum12(temparr, temparr.length));
return;
}
for(int i=0;i<arr.length;i++){
if(!visited[i]){
temparr[current_idx] = arr[i];
visited[i] = true;
DFS_1(arr, temparr, visited, current_idx+1);
visited[i] = false;
}
}
}
}