fork download
  1. #include <bits/stdc++.h>
  2. #define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
  3. #define fi first
  4. #define se second
  5. #define el "\n"
  6. #define pb push_back
  7. #define sz(a) (int)a.size()
  8. #define FILL(a, x) memset(a, x, sizeof(a))
  9.  
  10. using namespace std;
  11. typedef long long ll;
  12. typedef pair<int, int> ii;
  13. const int N = (int)1e6+3;
  14. int T, S, m, n;
  15. int main()
  16. {
  17. ios_base::sync_with_stdio(false);
  18. cin.tie(NULL); cout.tie(NULL);
  19. freopen("pepsi.inp", "r", stdin);
  20. freopen("pepsi.out", "w", stdout);
  21. cin >> T;
  22. while(T--){
  23. cin >> S >> m >> n;
  24. vector<int> a, c;
  25. FOR(i, 1, m){
  26. int x;
  27. cin >> x;
  28. a.pb(x);
  29. c.pb(x);
  30. }
  31.  
  32. vector<ii> b;
  33. FOR(i, 1, n){
  34. int x, d;
  35. cin >> x >> d;
  36. c.pb(x);
  37. b.pb({x, d});
  38. }
  39. sort(c.begin(), c.end());
  40. c.resize(unique(c.begin(), c.end()) - c.begin());
  41.  
  42. vector<bool> d1(N, 0);
  43. vector<int> d2(N, 0);
  44. for(int x : a){
  45. int p = lower_bound(c.begin(), c.end(), x) - c.begin() + 1;
  46. d1[p] = 1;
  47. }
  48. for(int i = 0; i < sz(b); i++){
  49. int p = lower_bound(c.begin(), c.end(), b[i].fi) - c.begin() + 1;
  50. d2[p] = b[i].se;
  51. }
  52.  
  53. ll ans = 0;
  54. int Tam = S;
  55. FOR(i, 1, sz(c)){
  56. if (Tam >= d2[i]){
  57. ans += d2[i];
  58. Tam -= d2[i];
  59. }
  60. if (d1[i] == 1) Tam = S;
  61. // cout << i << " " << d2[i] << el;
  62. }
  63. cout << ans << el;
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty