Skip to content

Commit 3429265

Browse files
Add files via upload
1 parent 825467e commit 3429265

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
for _ in range(int(input())):
2+
n, k = map(int, input().split())
3+
a = list(map(int, input().split()))
4+
5+
ans = 0
6+
cur = 0
7+
for i in range(n):
8+
if a[i] >= k:
9+
cur += a[i]
10+
if a[i] == 0 and cur > 0:
11+
cur -= 1
12+
ans += 1
13+
14+
print(ans)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
int n;
11+
vector<ll> arr;
12+
13+
ll checkOut(int takeL, int takeR, int solveL, int solveR){
14+
int i = takeL;
15+
int j = solveR;
16+
17+
ll ans = 0;
18+
while (i <= takeR && j >= solveL && arr[i] < arr[j]){
19+
ans += (arr[i] - arr[j]);
20+
i++;
21+
j--;
22+
}
23+
24+
return ans;
25+
26+
}
27+
int main(){
28+
int tc;
29+
cin >> tc;
30+
while(tc--){
31+
int l, r;
32+
33+
cin >> n >> l >> r;
34+
l--; r--;
35+
arr.resize(n);
36+
for(int i = 0; i < n; i++){
37+
cin >> arr[i];
38+
39+
}
40+
41+
42+
if (l > 0) sort(arr.begin(), arr.begin() + l);
43+
sort(arr.begin() + l, arr.begin() + r + 1);
44+
if (r < n-1) sort(arr.begin() + r + 1, arr.end());
45+
46+
ll originalSum = 0;
47+
for(int i = l; i <= r; i++){
48+
originalSum += arr[i];
49+
}
50+
51+
ll ans = originalSum + min(checkOut(0, l - 1, l, r), checkOut(r + 1, n - 1, l, r));
52+
cout << ans << endl;
53+
}
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)