Problem:
飴 2 (Candies 2)
問題文
机の上に
N
個の飴が横一列に並んでおり，左から順に
1
から
N
までの番号が付けられている．飴
i
(
1 ≦ i ≦ N
) の美味しさは
A
i
である．
JOI 君は，
N
個の飴のうちいくつかを選んで食べることにした．
ただし，飴を食べ過ぎないために，どの連続する
K
個の飴についても，そのうち高々
2
個しか食べないようにする．すなわち，どの
j
(
1 ≦ j ≦ N - K + 1
) についても，飴
j
から飴
j + K - 1
までの連続する
K
個の飴のうち，食べる飴の個数は
2
個以下でなければならない．
このもとで，JOI 君は食べる飴の美味しさの合計をできるだけ大きくしたい．
N
個の飴の美味しさと
K
が与えられたとき，JOI 君が食べる飴の美味しさの合計の最大値を求めるプログラムを作成せよ．
制約
2 ≦ K ≦ N ≦ 3 000
．
1 ≦ A
i
≦ 10
9
(
1 ≦ i ≦ N
)．
入力される値はすべて整数である．
入力
入力は以下の形式で標準入力から与えられる．
N
K
A
1
A
2
…
A
N
出力
標準出力に，JOI 君が食べる飴の美味しさの合計の最大値を
1
行で出力せよ．
入出力例
入力例 1
5 4
1 3 2 4 3
出力例 1
8
JOI 君が飴
1
，飴
4
， 飴
5
を食べるとき，美味しさの合計は
8
となる．
どの連続する
4
個の飴についても食べる飴の個数が
2
個以下であるような食べ方のうち，美味しさの合計が
9
以上であるようなものは存在しないため，
8
を出力する．
入力例 2
6 3
3 7 1 5 6 4
出力例 2
21
JOI 君が飴
1
，飴
2
， 飴
4
，飴
5
を食べるとき，美味しさの合計は
21
となる．
どの連続する
3
個の飴についても食べる飴の個数が
2
個以下であるような食べ方のうち，美味しさの合計が
22
以上であるようなものは存在しないため，
21
を出力する．
入力例 3
5 2
3 3 2 2 1
出力例 3
11
入力例 4
12 5
864814169 716638377 926889183 891468826 217138351 891972397 504371916 678159995 435478604 181254225 760822841 688502728
出力例 4
4427122428
情報オリンピック日本委員会
作 『第 21 回日本情報オリンピック JOI 2021/2022 二次予選競技課題』

Submitted solutions:

code_snippet: #include <iostream>
using namespace std;
int A[3001];
long table[3001][3001];
int N, K;

long calcM(int pos, int prev){
    if(pos>N){
        return 0;
    }
    if(pos==N){
        return A[N];
    }
    
    if(table[pos][prev]!=-1){
        return table[pos][prev];
    }
    
    long res;
    if(prev==0){
        res= max(A[pos]+calcM(pos+1,1),calcM(pos+1,0));
    }else{
                
        int prevNext=prev+1;
        if(prevNext>=K){
            prevNext=0;
        }
        res= max(A[pos]+calcM(pos-prev+K,K-prev),calcM(pos+1,prevNext));
    }
    table[pos][prev]=res;
    return res;
}

int main(void){
    // Your code here!
    cin >> N >> K;
    for(int i=1;i<N+1;i++){
        cin >> A[i];
        for(int j=0;j<K;j++){
            table[i][j]=-1;
        }
    }
    cout << calcM(1,0) << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 73968
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,K;
    cin>>N>>K;
    vector<ll> A(N);
    for(int i=0;i<N;i++)cin>>A[i];
    vector<vector<ll>> DP(N,vector<ll>(N,-1e18));
    vector<vector<ll>> MDP(N,vector<ll>(N,-1e18));
    ll an=0;
    for(int i=0;i<N;i++){
        for(int j=0;j<i;j++){
            DP[i][j]=max(DP[i][j],A[i]+A[j]);
            if(i-K>=0&&j!=0){
                DP[i][j]=max(DP[i][j],MDP[j][min(i-K,ll(j-1))]+A[i]);
            }
            an=max(an,DP[i][j]);
            MDP[i][j]=DP[i][j];
            if(j!=0)MDP[i][j]=max(MDP[i][j],MDP[i][j-1]);
        }
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 144064
final_rank: 8

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N, K;
  cin >> N >> K;
  vi A(N);
  rep(i, N) cin >> A[i];
  vv(ll, dp, N, N, -inf);
  dp[0][0] = 0;
  ll ans = 0;
  rep(i, N) {
    rep(j, i) {
      dp[i][j] = A[i] + A[j];
      if (j > 0) {
        chmax(dp[i][j], dp[i][j-1]);
        if (0 <= i-K) {
          chmax(dp[i][j], dp[j][min(j-1, i-K)] + A[i]);
        }
      } 
      chmax(ans, dp[i][j]);
    }
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 73572
final_rank: 1

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "a.cpp"

int main(void) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    cin >> a;
    vector dp(n + 1, vector(k, 0L));
    rep (i, n) {
        rep (j, k) {
            chmax(dp[i + 1][max(j - 1, 0)], dp[i][j]);
            if (j == 0) {
                chmax(dp[i + 1][k - 2], dp[i][0] + a[i]);
            } else {
                chmax(dp[min(n, i + 1 + j)][max(0, k - 2 - j)], dp[i][j] + a[i]);
            }
        }
    }

    ll ans = 0;
    rep (i, n + 1) chmax(ans, *max_element(all(dp[i])));
    co(ans);

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 73572
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001




#define SIZE 3005

ll dp[SIZE],maxi[SIZE];
ll dp2[SIZE][SIZE],maxi2[SIZE][SIZE];
ll A[SIZE];


int main(){

	int N,K;
	scanf("%d %d",&N,&K);

	for(int i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}

	for(int i = 1; i <= N; i++){

		//A[i]の手前K-1個に、他に食べられた飴がない
		dp[i] = A[i];
		if(i-K >= 1){

			dp[i] += maxi[i-K];
		}

		//A[i]の手前K-1個に、1つ食べられた飴がある
		ll work = 0;
		for(int a = max(1,i-K+1); a <= i-1; a++){

			ll tmp = A[i]+dp[a];
			ll tmp2 = 0;
			if(i-K >= 1){

				tmp2 = A[i]+maxi2[a][i-K];
			}
			dp2[i][a] = max(tmp,tmp2); //dp2[最後に食べた飴のindex][その1つ前に食べた飴のindex] = 最大値

			chmax(work,dp2[i][a]);
		}

		for(int a = max(1,i-K+1); a <= i-1; a++){

			maxi2[i][a] = dp2[i][a];
			chmax(maxi2[i][a],maxi2[i][a-1]); //自分より左の最大値を前計算しておく
		}
		maxi[i] = max(dp[i],work);
		chmax(maxi[i],maxi[i-1]);
	}

	printf("%lld\n",maxi[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 95520
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001




#define SIZE 3005

ll dp[SIZE],maxi[SIZE];
ll dp2[SIZE][SIZE],maxi2[SIZE][SIZE];
ll A[SIZE];


int main(){

	int N,K;
	scanf("%d %d",&N,&K);

	for(int i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}

	for(int i = 1; i <= N; i++){

		//A[i]の手前K-1個に、他に食べられた飴がない
		dp[i] = A[i];
		if(i-K >= 1){

			dp[i] += maxi[i-K];
		}

		//A[i]の手前K-1個に、1つ食べられた飴がある
		ll work = 0;
		for(int a = max(1,i-K+1); a <= i-1; a++){

			ll tmp = A[i]+dp[a];
			ll tmp2 = 0;
			if(i-K >= 1){

				tmp2 = A[i]+maxi2[a][i-K];
			}
			dp2[i][a] = max(tmp,tmp2);

			chmax(work,dp2[i][a]);
		}

		for(int a = max(1,i-K+1); a <= i-1; a++){

			maxi2[i][a] = dp2[i][a];
			chmax(maxi2[i][a],maxi2[i][a-1]);
		}
		maxi[i] = max(dp[i],work);
		chmax(maxi[i],maxi[i-1]);
	}

	printf("%lld\n",maxi[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 95528
final_rank: 5

code_snippet: #include <bits/extc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) begin(x),end(x)
#define Fixed fixed << setprecision(12)
#define updiv(a, b) (((a) + (b) - 1) / (b))
using pii = pair<int,int>;
constexpr int32_t INF  = 0x3f3f3f3f;
constexpr int_fast64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7;
constexpr int mod2 = 998244353;

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template<class T, class U> istream& operator>>(istream& is, pair<T, U>& p){ is >> p.first >> p.second; return (is); }
template<class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p){ os << p.first << ' ' << p.second; return (os); }
template<class T> istream& operator>>(istream& is, vector<T>& v){ for (auto &&e : v) is >> e; return (is); }
template<class T> ostream& operator<<(ostream& os, vector<T>& v){ for (auto &&e : v) os << e << ' '; return (os); }

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using max_heap = priority_queue<T>;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T> >;
template <class A, class B> using umap = unordered_map<A, B>;
template <class T> using uset = unordered_set<T>;

template <class T> using Max_Heap = __gnu_pbds::priority_queue<T, less<T>, __gnu_pbds::rc_binomial_heap_tag>;
template <class T> using Min_Heap = __gnu_pbds::priority_queue<T, greater<T>, __gnu_pbds::rc_binomial_heap_tag>;
template <class T> using Set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
template <class K, class V> using Umap = __gnu_pbds::gp_hash_table<K, V>;
template <class T> using Rope = __gnu_cxx::rope<T>;
using Trie = __gnu_pbds::trie<string, __gnu_pbds::null_type, __gnu_pbds::trie_string_access_traits<>, __gnu_pbds::pat_trie_tag, __gnu_pbds::trie_prefix_search_node_update>;

template <class T> void bye(T x){ cout << x << '\n'; exit(0); }
inline int square(int a){ return (a * a); }

constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};

signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);

    int n, k;
    cin >> n >> k;

    assert(2 <= k && k <= n && n <= 3000);

    vector<int> a(n);

    cin >> a;

    for (auto e : a) assert(1 <= e && e <= 1000000000);

    vector dp(n + 1, vector(n + 1, 0LL));

    for (int i = 1; i <= n; ++i) {
        dp[i][0] = a[i - 1];
        for (int j = 1; j <= n; ++j) {
            if (j < i) {
                chmax(dp[i][j], dp[j][max(i - k, 0)] + a[i - 1]);
            }
            chmax(dp[i][j], dp[i][j - 1]);
        }
    }

    long long res = 0;

    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            chmax(res, dp[i][j]);
        }
    }

    cout << res << '\n';

    return (0);
}
accuracy: 1
time_ms: 50
memory_kb: 73592
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n,l);
    vector<ll> a(n);
    in(a);
    vector2d(ll,dp,n+1,n+1,-1);
    function<ll(int,int)> rec=[&](int i,int s){
        if(i>=n)return 0ll;
        if(dp[i][s]!=-1)return dp[i][s];
        ll res=rec(i+1,s);
        if(s==0)chmax(res,rec(i+1,i+1)+a[i]);
        else chmax(res,rec(max(i+1,s+l-1),i+1)+a[i]);
        return dp[i][s]=res;
    };
    out(rec(0,0));
}
accuracy: 1
time_ms: 90
memory_kb: 73884
final_rank: 7