-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColoredArray.java
More file actions
70 lines (69 loc) · 1.79 KB
/
ColoredArray.java
File metadata and controls
70 lines (69 loc) · 1.79 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
package codechef;
import java.util.*;
public class ColoredArray {
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
short t = sc.nextShort();
while(t>0){
short N = sc.nextShort();
short M =sc.nextShort();
short K = sc.nextShort();
short input[] = new short[1001];
short B[][] = new short[1001][1001];
short C[][] = new short[1001][1001];
short position[] = new short[1001];
short max[] = new short[1001];
for(int i = 0 ; i<N ; i++){
input[i] = sc.nextShort();
}
for(int i = 0; i<N ; i++){
for(int j = 0; j<M ; j++){
B[i][j] = sc.nextShort();
}
}
for(int i = 0; i<N ; i++){
for(int j = 0; j<M ; j++){
C[i][j] = sc.nextShort();
}
}
short score = 0;
for(int i = 0; i<N ; i++){
score += B[i][input[i]-1];
position[i] = (short)i;
}
for(int i = 0 ; i<N ; i++){
short m = Short.MIN_VALUE;
for(int j = 0 ; j<M ; j++){
short x = (short)(B[i][j]-C[i][j]);
if(x>m) m = x;
}
max[i] = m;
}
sortWithPosition(max,position);
for(int i = 0; i<K ; i++){
if(max[i]>B[position[i]][input[position[i]]-1]){
score += max[i]-B[position[i]][input[position[i]]-1];
}
}
System.out.println(score);
t--;
}
}
public static void sortWithPosition(short input[],short position[]){
for(int i = 1 ; i<input.length ; i++){
short currentElement = input[i];
short backPointer = (short)(i-1);
while(backPointer >= 0 && input[backPointer] < currentElement){
input[backPointer+1]= input[backPointer];
position[backPointer+1] = position[backPointer];
backPointer--;
}
input[backPointer+1] = currentElement;
position[backPointer+1] = (short)i;
}
}
}