Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- #include <deque>
- using namespace std;
- long long d[4501];
- long long dp[301][1501];
- deque <long long> Q;
- struct kkk{
- long long max;
- int poz;
- } P[3];
- int main() {
- int n, k, t;
- cin >> n >> t >> k;
- if(n <= 1500 && t <= 300){
- for(int i = 1; i <= n; ++i)
- cin >> dp[1][i];
- for(int i = 2; i <= t; ++i)
- for(int j = i; j <= n; ++j)
- dp[i][j] = -1000000000000000000;
- for(int i = 2; i <= t; ++i)
- for(int j = i; j <= n; ++j)
- for(int f = j - 1; f >= j - k && f >= i - 1; --f)
- dp[i][j] = max(dp[i][j], dp[i - 1][f] + dp[1][j]);
- long long maxi = -1000000000000000000;
- for(int i = t; i <= n; ++i)
- maxi = max(maxi, dp[t][i]);
- cout << maxi;
- return 0;
- }
- P[1].max = -1000000000000000000;
- for(int i = 1; i <= n; ++i) {
- cin >> d[i];
- if(d[i] > P[2].max){
- P[2].max = d[i];
- P[2].poz = i;
- }
- if(d[i] > P[1].max){
- P[1] = {d[i], i};
- P[2].max = -1000000000000000000;
- }
- else
- if( i - k >= P[1].poz) {
- P[1] = P[2];
- P[2].max = -1000000000000000000;
- }
- Q.push_front(P[1].max);
- }
- for(int i = 2; i <= t; ++i) {
- P[1].max = -1000000000000000000;
- Q.pop_front();
- for(int j = i; j <= n; ++j) {
- long long x = d[j] + Q.back();
- if(x > P[2].max){
- P[2].max = x;
- P[2].poz = j;
- }
- if(x > P[1].max){
- P[1] = {x, j};
- P[2].max = -1000000000000000000;
- }
- else
- if( j - k >= P[1].poz) {
- P[1] = P[2];
- P[2].max = -1000000000000000000;
- }
- Q.pop_back();
- Q.push_front(P[1].max);
- }
- }
- long long maxi = -1000000000000000000;
- while(Q.size()){
- if(Q.back() > maxi)
- maxi = Q.back();
- Q.pop_back();
- }
- cout << maxi;
- return 0;
- }
Add Comment
Please, Sign In to add comment