Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created October 13, 2022 17:02
Show Gist options
  • Save qjatn0120/4e036c071449615beec46b7fdff3f5be to your computer and use it in GitHub Desktop.
Save qjatn0120/4e036c071449615beec46b7fdff3f5be to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int t, n, q, k, a[MX];
long long int sum[MX];
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
a[i] = max(a[i], a[i - 1]);
}
while(q--){
cin >> k;
int lo = 0, hi = n;
while(lo < hi){
int mid = (lo + hi + 1) >> 1;
if(a[mid] <= k) lo = mid;
else hi = mid - 1;
}
cout << sum[lo] << " ";
}
cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment