Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "TELEPORT"
- #include <iostream>
- #include <cstdio>
- #include <deque>
- using namespace std;
- using ll = long long;
- using ld = long double;
- template <class T>
- void read(T &x)
- {
- x = 0;
- register int c;
- while ((c = getchar()) && (c > '9' || c < '0'))
- ;
- for (; c >= '0' && c <= '9'; c = getchar())
- x = x * 10 + c - '0';
- }
- constexpr int N = 5e3 + 5;
- constexpr ll Inf = 1e18;
- int n, m, k;
- ll l, r, a[N], T, dp[N][2];
- void Read()
- {
- read(n), read(k), read(l), read(r), read(T);
- for (int i = 1; i <= n; ++i)
- read(a[i]);
- a[n + 1] = T;
- }
- /*void Sub_1()
- {
- fill_n(&dp[0][0], N * N, -Inf);
- dp[0][0] = 0;
- for (int i = 1; i <= k; ++i)
- {
- for (int j = 1; j <= n; ++j)
- {
- for (int t = j - 1; ~t; --t)
- if (a[j] - a[t] >= l && a[j] - a[t] <= r)
- dp[i][j] = max(dp[i][j], dp[i - 1][t] + a[j] * (i - 1 - (k - i)));
- //cout << dp[i][j] << (j == n ? "\n" : " ");
- }
- }
- ll ans(-Inf);
- for (int i = 1; i <= n; ++i)
- if (T - a[i] >= l && T - a[i] <= r)
- ans = max(ans, dp[k][i]);
- cout << ans;
- }*/
- void Sub_2()
- {
- fill_n(&dp[0][0], 2 * N, -Inf);
- dp[0][0] = 0;
- ll ans(-Inf);
- for (int i = 1; i <= k; ++i)
- {
- deque<int> s;
- dp[0][i & 1] = -Inf;
- for (int j = 1, t = 0; j <= n; ++j)
- {
- dp[j][i & 1] = -Inf;
- while (t < j && a[j] - a[t] >= l)
- {
- while (!s.empty() && dp[s.back()][!(i & 1)] < dp[t][!(i & 1)])
- s.pop_back();
- s.push_back(t);
- ++t;
- }
- while (!s.empty() && a[j] - a[s.front()] > r)
- s.pop_front();
- if (!s.empty())
- dp[j][i & 1] = dp[s.front()][!(i & 1)] + a[j] * (i - 1 - (k - i));
- if (i == k && T - a[j] >= l && T - a[j] <= r)
- ans = max(ans, dp[j][i & 1]);
- }
- }
- cout << ans;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- //if (1ll * n * n * k <= 2e7)
- // Sub_1();
- //else
- Sub_2();
- }
Advertisement
Add Comment
Please, Sign In to add comment