#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
using namespace std;
const int N = 3e5 + 5,mod = 1e9 + 7;
int a[N];
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
int q;
cin>>q;
while (q--){
multiset <int> st;
for (int i = 1; i <= n; i++){
st.insert(a[i]);
}
int s;
cin>>s;
int ans = 0;
while (st.size()){
multiset <int> :: iterator it = st.lower_bound(s);
int l = -1,r = -1;
if (it != st.end()) {
r = *it;
}
if (it != st.begin()){
--it;
l = *it;
}
if (l == -1 || (l != -1 && r != -1 && (r - s) < (s - l))) {
st.erase(st.find(r));
ans += abs(r - s);
s = r;
}else if (r == -1 || (l != -1 && r != -1 && (s - l) <= (r - s))){
st.erase(st.find(l));
ans += abs(s - l);
s = l;
}
}
cout<<ans<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
404 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
404 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
12420 KB |
Output is correct |
18 |
Correct |
60 ms |
12368 KB |
Output is correct |
19 |
Correct |
60 ms |
12368 KB |
Output is correct |
20 |
Correct |
55 ms |
12372 KB |
Output is correct |
21 |
Correct |
61 ms |
12372 KB |
Output is correct |
22 |
Correct |
81 ms |
12368 KB |
Output is correct |
23 |
Correct |
58 ms |
12380 KB |
Output is correct |
24 |
Correct |
63 ms |
12548 KB |
Output is correct |
25 |
Correct |
64 ms |
12368 KB |
Output is correct |
26 |
Correct |
65 ms |
12352 KB |
Output is correct |
27 |
Correct |
64 ms |
12368 KB |
Output is correct |
28 |
Correct |
60 ms |
12440 KB |
Output is correct |
29 |
Correct |
55 ms |
12500 KB |
Output is correct |
30 |
Correct |
60 ms |
12256 KB |
Output is correct |
31 |
Correct |
56 ms |
12424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
3090 ms |
11876 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
404 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
54 ms |
12420 KB |
Output is correct |
18 |
Correct |
60 ms |
12368 KB |
Output is correct |
19 |
Correct |
60 ms |
12368 KB |
Output is correct |
20 |
Correct |
55 ms |
12372 KB |
Output is correct |
21 |
Correct |
61 ms |
12372 KB |
Output is correct |
22 |
Correct |
81 ms |
12368 KB |
Output is correct |
23 |
Correct |
58 ms |
12380 KB |
Output is correct |
24 |
Correct |
63 ms |
12548 KB |
Output is correct |
25 |
Correct |
64 ms |
12368 KB |
Output is correct |
26 |
Correct |
65 ms |
12352 KB |
Output is correct |
27 |
Correct |
64 ms |
12368 KB |
Output is correct |
28 |
Correct |
60 ms |
12440 KB |
Output is correct |
29 |
Correct |
55 ms |
12500 KB |
Output is correct |
30 |
Correct |
60 ms |
12256 KB |
Output is correct |
31 |
Correct |
56 ms |
12424 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Execution timed out |
3090 ms |
11876 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |