-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path6872.cpp
71 lines (60 loc) · 1.54 KB
/
6872.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @file 6872.cpp
* @author Macesuted ([email protected])
* @date 2023-03-05
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
#define maxn 100005
typedef pair<int, int> pii;
pii a[maxn];
int64_t f[maxn];
int vis[maxn];
void solve(void) {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1, [](pii a, pii b) { return (int64_t)a.second * b.first > (int64_t)b.second * a.first; });
int m = a[1].first;
for (int i = 1; i < m; i++) f[i] = INT64_MIN;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < m; j++) vis[j] = 0;
for (int j = 0; j < m; j++) {
if (vis[j]) continue;
int64_t p = j, v = f[p];
while (vis[p] < 3) {
vis[p]++;
v = f[p] = max(f[p], v);
p += a[i].first;
if (v != INT64_MIN) v += a[i].second - p / m * a[1].second;
p %= m;
}
}
}
while (q--) {
int64_t v;
cin >> v;
cout << (f[v % m] == INT64_MIN ? -1 : v / m * a[1].second + f[v % m]) << endl;
}
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}