#include <bits/stdc++.h>
using namespace std;
#define int ll
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifdef DEBUG
#define debug(args...) _debug(args)
void _debug(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
#else
#define debug(args...)
#endif
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 10
ll x, t;
int n, m, w;
ll s[MAXN];
pll dc[MAXN];
ll cnt[MAXN];
ll dp(int ri, int msk) {
if (ri == n) return 0;
vii v;
ll mn = s[ri] / t * t;
REP (i, 0, m) {
if (!(msk & (1 << i))) continue;
ll cur = (s[ri] - dc[i].FI) / t * t + dc[i].FI;
if (s[ri] - dc[i].FI < 0) cur -= t;
if (cur > mn) {
v.pb(cur, i);
}
}
ll prv = ri == 0 ? 0 : s[ri - 1];
ll k1 = s[ri] / t, k2 = (prv - 1) / t + 1;
if (prv - 1 < 0) k2--;
//debug(" %lld %s: %lld %lld %lld\n", ri, bitset<4>(msk).to_string().c_str(), -1, k1, k2);
ll sum = (ll) (k1 - k2 + 1) * w;
REP (i, 0, m) {
ll k1 = (s[ri] - dc[i].FI) / t, k2 = (prv - dc[i].FI - 1) / t + 1;
if (prv - dc[i].FI - 1 < 0) k2--;
if (s[ri] - dc[i].FI < 0) k1--;
//debug(" %lld %s: %lld %lld %lld\n", ri, bitset<4>(msk).to_string().c_str(), i, k1, k2);
cnt[i] = k1 - k2 + 1;
if (msk & (1 << i)) {
sum += (ll) cnt[i] * w;
//debug(" %lld %s: %lld %lld %lld %lld\n", ri, bitset<4>(msk).to_string().c_str(), i, cnt[i], w, sum);
}
}
sort(ALL(v), greater<ii>());
int tmsk = msk;
ll res = dp(ri + 1, msk) + sum;
debug(" %lld %s: %lld\n", ri, bitset<4>(msk).to_string().c_str(), sum);
REP (_, 0, v.size()) {
sum -= w;
auto [_a, i] = v[_];
tmsk ^= (1 << i);
sum += dc[i].SE;
debug(" %lld %s: %lld\n", ri, bitset<4>(msk).to_string().c_str(), sum);
mnto(res, dp(ri + 1, tmsk) + sum);
}
debug("%lld %s: %lld\n", ri, bitset<4>(msk).to_string().c_str(), res);
return res;
}
main() {
scanf("%lld%lld%lld%lld%lld", &x, &n, &m, &w, &t);
REP (i, 0, n) {
scanf("%lld", &s[i]);
}
sort(s, s + n);
s[n] = x;
n++;
REP (i, 0, m) {
scanf("%lld%lld", &dc[i].FI, &dc[i].SE);
}
sort(dc, dc + m);
printf("%lld\n", dp(0, (1 << m) - 1));
return 0;
}
/*
19 1 4 8 7
5
1 20
2 10
4 5
6 5
105 3 5 9 10
59
68
71
4 71
6 32
7 29
3 62
2 35
1000000000000 1 1 1000000 6
999999259244
1 123456789
*/
Compilation message
coach.cpp: In function 'll dp(ll, ll)':
coach.cpp:10:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
82 | REP (_, 0, v.size()) {
| ~~~~~~~~~~~~~~
coach.cpp:82:2: note: in expansion of macro 'REP'
82 | REP (_, 0, v.size()) {
| ^~~
coach.cpp: At global scope:
coach.cpp:94:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
94 | main() {
| ^~~~
coach.cpp: In function 'int main()':
coach.cpp:95:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%lld%lld%lld%lld%lld", &x, &n, &m, &w, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%lld", &s[i]);
| ~~~~~^~~~~~~~~~~~~~~
coach.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%lld%lld", &dc[i].FI, &dc[i].SE);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |