-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA1470_CF.java
More file actions
46 lines (40 loc) · 1.42 KB
/
Copy pathA1470_CF.java
File metadata and controls
46 lines (40 loc) · 1.42 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
/*
Note:
Scanner is avoided here because it is too slow for large inputs and can cause
Time Limit Exceeded errors. BufferedReader with StringTokenizer is used instead
for faster input handling, along with StringBuilder for efficient output.
*/
import java.io.*;
import java.util.*;
public class A1470_CF {
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder out =new StringBuilder();
int t=Integer.parseInt(br.readLine());
while(t-->0){
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int k[]=new int[n];
st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)k[i]=Integer.parseInt(st.nextToken());
long c[]=new long[m];
st=new StringTokenizer(br.readLine());
for(int i=0;i<m;i++)c[i]=Long.parseLong(st.nextToken());
Arrays.sort(k);
long sum=0;
int j=0;
for(int i=n-1;i>=0;i--){
if(j+1<=k[i]){
sum+=c[j];
j++;
}else{
sum+=c[k[i]-1];
}
}
out.append(sum).append('\n');
}
System.out.print(out.toString());
}
}