-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSS.java
More file actions
41 lines (41 loc) · 1.35 KB
/
Copy pathMSS.java
File metadata and controls
41 lines (41 loc) · 1.35 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
import java.util.*;
public class MSS{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int []a =new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
long mss=Integer.MIN_VALUE;
for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
List<Integer> inside=new ArrayList<>();
List<Integer> outside=new ArrayList<>();
for(int l=0;l<n;l++){
if(l>=i && l<=j){
inside.add(a[l]);
}else{
outside.add(a[l]);
}
}
Collections.sort(inside);
Collections.sort(outside, Collections.reverseOrder());
long csum=0;
for(int xs: inside){
csum+=xs;
}
int swapp=Math.min(k, Math.min(inside.size(), outside.size()));
for(int l=0;l<swapp;l++){
if(inside.get(l)<outside.get(l)){
csum+=outside.get(l)-inside.get(l);
}else{
break;
}
} mss=Math.max(mss, csum);
}
}
System.out.println(mss);
}
}