Problem:
Problem G: Fitness
Problem
外出することが少ないツバサくんは、家で筋トレを毎日行うようにしている。
筋トレのレパートリーは $N$ 種類あり、 $i (1 \leqq i \leqq N)$ 種類目のトレーニングを1回行うと、 $t_i$ 分で $p_i$ だけ筋力アップする。
ただし、それぞれのトレーニングは筋力にとても負荷がかかるので、基本的に1回しか行うことができない。
負荷がかかる場所はバラバラなので、複数種類のトレーニングを行うことは可能だ。
また、ツバサくんは休憩も重要だと考えていて、 $K$ 分費やして休憩することができる。
十分に休憩をすることで筋力も回復し、以前既に行ったトレーニング全てを再び行うことができる。
もちろん休憩時に筋力アップは見込めない。休憩は複数回取ることも可能だ。
さて、今日はトレーニングを行う時間が $T$ 分ある。時間内に終わるようなトレーニングメニューを上手く作成したとき、どれだけ筋力アップできるだろうか？
なお、トレーニングや休憩の切り替え時間は無視できるものとする。
Constraints
入力は以下の条件を満たす。
$1 \leqq N,K,T \leqq 2000$
$1 \leqq t_i \leqq 2000$
$1 \leqq p_i \leqq 10^{9}$
入力は全て整数である。
Input
入力は以下の形式で標準入出力から与えられる。
$N$ $K$ $T$
$t_1$ $p_1$
$t_2$ $p_2$
$\vdots$
$t_N$ $p_N$
Output
$T$ 分以内で上昇する筋力の最大値を1行で出力せよ。
末尾の改行を忘れないこと。
Sample Input 1
3 1 10
1 2
2 4
3 3
Sample Output 1
16
例えば、 $1$ $\to$ $2$ $\to$ $3$ $\to$ 休憩 $\to$ $1$ $\to$ $2$ というメニューを選ぶと、 $10$ 分で筋力が $15$ アップする。
$2$ $\to$ 休憩 $\to$ $2$ $\to$ 休憩 $\to$ $2$ $\to$ $1$ とすると $9$ 分で筋力が $14$ アップする。時間を余らせても問題ない点に注意すること。
$1$ $\to$ $2$ $\to$ 休憩 $\to$ $1$ $\to$ $2$ $\to$ 休憩 $\to$ $2$ とすると $10$ 分で筋力が $16$アップし、これが最適である。
Sample Input 2
3 1 10
53 1576
78 2115
89 2006
Sample Output 2
0
そもそも時間内に終わるトレーニングが存在しない。
Sample Input 3
4 8 800
10 49275367
474 100000000
9 2587424
20 99999999
Sample Output 3
3137370110
オーバーフローに注意すること。

Submitted solutions:

code_snippet: #include <iostream>
#include <string.h>
using namespace std;
long long int dp[2002][2002];
long long int ts[2001];
long long int ps[2001];

int t;
void f(int t2,int p2,long long int s2){
	if(t2>t)return ;
	if(dp[t2][p2]<s2)dp[t2][p2]=s2;
}


int main() {
	memset(dp,-1,sizeof(dp));
	dp[0][0]=0;
	int n,k;
	cin>>n>>k>>t;
	for(int i=0;i<n;i++){
		cin>>ts[i]>>ps[i];
	}
	long long int ans=0;
	for(int t2=0;t2<=t;t2++){
		for(int p2=0;p2<=n;p2++){
			long long int s2=dp[t2][p2];
			if(s2==-1)continue;
			if(ans<s2)ans=s2;
			if(p2==n){
				f(t2+k,0,s2);
			}else{
				f(t2,p2+1,s2);
				f(t2+ts[p2],p2+1,s2+ps[p2]);
				f(t2+k,0,s2);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 34456
final_rank: 12

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 2005

struct Info{

	ll need_time,value;
};

ll N,K,T;
Info info[SIZE];
ll dp1[SIZE][SIZE],dp2[SIZE];

int main(){

	scanf("%lld %lld %lld",&N,&K,&T);

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

		scanf("%lld %lld",&info[i].need_time,&info[i].value);
	}

	for(ll i = 0; i <= N; i++){
		for(ll k = 0; k <= T; k++){
			dp1[i][k] = -BIG_NUM;
		}
	}

	dp1[0][0] = 0;
	for(ll i = 1; i <= N; i++){
		for(ll k = 0; k <= T; k++){
			if(dp1[i-1][k] == -BIG_NUM)continue;

			//足さない
			dp1[i][k] = max(dp1[i][k],dp1[i-1][k]);

			if(k+info[i].need_time > T)continue;

			//足す
			dp1[i][k+info[i].need_time] = max(dp1[i][k+info[i].need_time],dp1[i-1][k]+info[i].value);
		}
	}

	for(ll k = 0; k <= T; k++){

		dp2[k] = dp1[N][k];
	}
	dp2[0] = 0;

	for(ll t = 1; t <= T; t++){
		for(ll p = 0; p+(t+K) <= T; p++){
			if(dp2[p] == -BIG_NUM)continue;

			dp2[p+(t+K)] = max(dp2[p+(t+K)],dp2[p]+dp1[N][t]);
		}
	}

	ll ans = 0;

	for(ll k = 0; k <= T; k++){

		ans = max(ans,dp2[k]);
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34624
final_rank: 4

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(){
  ll INF = 1000000000000000000;
  ll n, k, t;scanf("%lld %lld %lld", &n, &k, &t);
  vvl DP = VV(n+1, t+1, -INF, ll);
  DP[0][0] = 0;
  vvl dat = VV(n, 2, 0, ll);
  for(ll i=0;i<n;i++) scanf("%lld %lld", &dat[i][0], &dat[i][1]);
  for(ll i=0;i<n;i++){
    for(ll j=0;j<=t;j++){
      DP[i+1][j] = max(DP[i+1][j], DP[i][j]);
      if(j + dat[i][0]<=t) DP[i+1][j+dat[i][0]] = max(DP[i+1][j+dat[i][0]], DP[i][j]+dat[i][1]);
    }
  }
  vll te = DP[n];//時間あたりの効率
  for(ll i=1;i<=t;i++) te[i] = max(te[i], te[i-1]);//メインの周期
  ll ans = 0;

  vvl dp = VV(t+n+2, t+1, -INF, ll);// 物0~T-1は重複可能
  dp[0][0] = 0;

  for(ll i=0;i<t+n+1;i++){
    if(i<=t){
      for(ll j=0;j<=t;j++){
        dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        if(j+i+k<=t) dp[i+1][j+i+k] = max({dp[i+1][j+i+k], dp[i][j]+te[i], dp[i+1][j]+te[i]});
      }
    }else{
      ll idx = i - t - 1;
      ll w = dat[idx][0], v = dat[idx][1];
      for(ll j=0;j<=t;j++){
        dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        if(j+w<=t) dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j] + v);
      }
    }
  }
  for(int i=0;i<=t;i++) ans = max(ans, dp[n+t+1][i]);
  std::cout << ans << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 97316
final_rank: 15

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

template<class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//--------------------------- Solve ---------------------------//

long long dp1[2005][2005];
long long dp2[2005][2005];

void solve() {
    int N, K, T; cin >> N >> K >> T;
    vector<long long> t(N+1), p(N+1);
    for (int i = 1; i <= N; i++) cin >> t[i] >> p[i];

    for (int i = 1; i <= N; i++) {
        for (int j = 0; j <= T; j++) {
            chmax(dp1[i][j], dp1[i-1][j]);
            if (j - t[i] >= 0) chmax(dp1[i][j], dp1[i-1][j-t[i]] + p[i]);
        }
    }

    for (int i = 0; i <= T; i++) {
        dp2[0][i] = dp1[N][i];
    }
    for (int i = 1; i <= T; i++) {
        for (int j = 0; j <= T; j++) {
            chmax(dp2[i][j], dp2[i-1][j]);
            if (j-(i+K) >= 0) chmax(dp2[i][j], dp2[i][j-(i+K)] + dp1[N][i]);
        }
    }

    cout << dp2[T][T] << '\n';
}

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

    solve();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 66220
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

ll n, k, t;
ll dp[2020][2020];
ll dp2[2020][2020];

int main() {
    cin >> n >> k >> t;
    vector<ll> tvec(n, 0);
    vector<ll> pvec(n, 0);
    for(int i = 0; i < n; ++i) cin >> tvec[i] >> pvec[i];

    memset(dp, 0, sizeof(dp));
    memset(dp2, 0, sizeof(dp2));

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j <= t; ++j) {
            chmax(dp[i+1][j], dp[i][j]);
            if(j-tvec[i] >= 0) {
                chmax(dp[i+1][j], dp[i][j-tvec[i]]+pvec[i]);
            }
        }
    }

    for(int i = 0; i <= t; ++i) dp2[0][i] = dp[n][i];

    vector<ll> wvec(t+1, 0);
    for(int i = 0; i <= t; ++i) wvec[i] = i+k;

    for(int i = 1; i <= t; ++i) {
        for(int j = 1; j <= t; ++j) {
            chmax(dp2[i][j], dp2[i-1][j]);
            if(j-wvec[i] >= 0) {
                chmax(dp2[i][j], dp2[i][j-wvec[i]]+dp[n][i]);
            }
        }
    }

    ll ans = 0;
    for(int i = 0; i <= t; ++i) chmax(ans, dp2[t][i]);

    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 66932
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

ll n, k, t;
ll dp[2020][2020];
ll dp2[2020][2020];

int main() {
    cin >> n >> k >> t;
    vector<ll> tvec(n, 0);
    vector<ll> pvec(n, 0);
    for(int i = 0; i < n; ++i) cin >> tvec[i] >> pvec[i];

    memset(dp, 0, sizeof(dp));
    memset(dp2, 0, sizeof(dp2));

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j <= t; ++j) {
            chmax(dp[i+1][j], dp[i][j]);
            if(j-tvec[i] >= 0) {
                chmax(dp[i+1][j], dp[i][j-tvec[i]]+pvec[i]);
            }
        }
    }

    for(int i = 0; i <= t; ++i) dp2[0][i] = dp[n][i];

    vector<ll> wvec(t, 0);
    for(int i = 0; i <= t; ++i) wvec[i] = i+k;

    for(int i = 1; i <= t; ++i) {
        for(int j = 1; j <= t; ++j) {
            chmax(dp2[i][j], dp2[i-1][j]);
            if(j-wvec[i] >= 0) {
                chmax(dp2[i][j], dp2[i][j-wvec[i]]+dp[n][i]);
            }
        }
    }

    ll ans = 0;
    for(int i = 0; i <= t; ++i) chmax(ans, dp2[t][i]);

    cout << ans << endl;
}
accuracy: 0.1
time_ms: 20
memory_kb: 66900
final_rank: 19

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;
//wupc

ll dp1[2005][2005],dp2[2005];

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n,k,T;
    cin>>n>>k>>T;
    V<ll> t(n),p(n);
    rep(i,n) cin>>t[i]>>p[i];
    rep(i,n){
        for(int j=0;j<=T;j++){
            chmax(dp1[i+1][j],dp1[i][j]);
            if(j-t[i]>=0) chmax(dp1[i+1][j],dp1[i][j-t[i]]+p[i]);
        }
    }
    V<ll> tt(T+1),q(T+1);
    for(int i=0;i<=T;i++){
        tt[i]=i+k;
        q[i]=dp1[n][i];
    }
    for(int i=0;i<=T;i++) dp2[i]=dp1[n][i];
    rep(j,T+1){
        rep(i,T+1){
            if(j+tt[i]<=T) chmax(dp2[j+tt[i]],dp2[j]+q[i]);
        }
    }
    ll ans=0;
    for(int i=0;i<=T;i++) chmax(ans,dp2[i]);
    out(ans);
}
accuracy: 1
time_ms: 10
memory_kb: 34596
final_rank: 3

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

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

int main(){

    ll N, K, T;
    cin >> N >> K >> T;
    V<ll>  t(N), p(N);
    REP(i, N) cin >> t[i] >> p[i];

    V<V<ll>> dp(N+1, V<ll>(T+1, -1));
    dp[0][0] = 0;
    REP(i, N){
        REP(j, T+1){
            if(dp[i][j]!=-1){
                chmax(dp[i+1][j], dp[i][j]);
                if(j+t[i]<T+1)chmax(dp[i+1][j+t[i]], dp[i][j]+p[i]);
            }
        }
    }

    V<ll> pos(T+1, 0);
    REP(i, T+1) pos[i] = dp[N][i];

    V<V<ll>> dq(T+1, V<ll>(2, -INF));
    dq[0][0] = 0;
    REP(i, T){
        REP(j, T+1){
            if(i+j<T+1) chmax(dq[i+j][1], dq[i][0]+pos[j]);
        }
        if(i+K<T+1) chmax(dq[i+K][0], dq[i][1]);
    }

    ll res = 0;
    REP(i, T+1){
        REP(j, 2){
            chmax(res, dq[i][j]);
        }
    }

    cout << res << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34936
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*(b/c)*c);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}




int main(){


	ll N,K,T;
	cin >> N >> K >> T;
	vector<ll> t(N),p(N);
	FOR(i,0,N) cin >> t[i] >> p[i];

	ll dp[N+1][T+1] = {};
	FOR(i,1,N+1){
		FOR(j,0,T+1){
			if(j>=t[i-1]){
				dp[i][j] = max(dp[i-1][j],dp[i-1][j-t[i-1]]+p[i-1]);
			}else{
				dp[i][j] = dp[i-1][j];
			}
		}
	}

	t.resize(T+24);
	p.resize(T+14);
	FOR(i,0,T+1){
		t[i] = i + K;
		p[i] = dp[N][i];
	}

	ll dp2[T+2][T+1]={};
	ll ans = 0;
	FOR(i,0,T+1) dp2[0][i] = dp[N][i];
	FOR(i,1,T+2){
		FOR(j,0,T+1){
			if(j>=t[i-1]){
				dp2[i][j] = max(dp2[i-1][j],dp2[i][j-t[i-1]]+p[i-1]);
			}else{
				dp2[i][j] = dp2[i-1][j];
			}
			ans = max(ans,dp2[i][j]);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 65776
final_rank: 9

code_snippet: /*	author:  Kite_kuma
	created: 2020.09.13 14:31:18 */

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

#pragma region aliases

#define rep(i, n) for(long long i = 0; i < (n); i++)
#define rrep(i, n) for(long long i = (n)-1; i > -1; i--)
#define Rep(i, m, n) for(long long i = (m); i < (n); i++)
#define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--)
#define REP(i, m, n, p) for(long long i = m; i < n; i += p)
#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bcnt(n) __builtin_popcountll(n)
#define endk endl
#define ednl endl
#define enld endl

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using qll = queue<long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
template <class T = ll>
using V = vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
//昇順pq(小さい方から取り出す)
template <class T = ll>
using pqup = priority_queue<T, vector<T>, greater<T>>;
//降順pq(大きい方から取り出す)
template <class T = ll>
using pqdn = priority_queue<T>;

#pragma endregion

#pragma region constants

long long const limLL = 9223372036854775807;  // POW(2,63)-1 ~ 9.22e18
long long const dekai = 3e16;
const long double pi = acos(-1);
const char el = '\n';
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

const int mod = 1000000007;
// const int mod = 998244353;

#pragma endregion

#pragma region basic_procedure

template <class T>
inline bool isin(T x, T lef, T rig) {
	return ((lef <= x) && (x < rig));
}

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

void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << "\n"; }
void No() { cout << "No\n"; }
void YES(bool f = 1) { cout << (f ? "YES" : "NO") << "\n"; }
void NO() { cout << "NO\n"; }
template <class T>
void drop(T answer) {
	cout << answer << "\n";
	exit(0);
}
void err() {
	cout << -1 << "\n";
	exit(0);
}

vector<long long> vin(long long n) {  //整数n個の入力を受け取ってベクトルに突っ込んで返す
	vector<long long> v(n);
	for(long long i = 0; i < n; i++) {
		cin >> v[i];
	}
	return v;
}

//ベクトルの出力(検証済)
// vectorの中身を出力する 答えの出力に利用可能
template <class T>
void vout(vector<T> &v, bool tate = 0) {
	if(v.size() > 0) {
		for(auto it = v.begin(); it < v.end(); it++) {
			cout << *it;
			if(it != v.end() - 1) {
				if(tate)
					cout << endl;
				else
					cout << " ";
			}
		}
	}
	cout << endl;
}

template <class T>
void add(vector<T> &v, T val) {	 //ベクトルの各要素に加算
	for(auto &a : v) a += val;
	return;
}

// vectorの中身を数える map<要素,個数>を返す
template <class T>
map<T, long long> cntv(vector<T> v) {
	map<T, long long> m;
	for(auto &g : v) {
		if(m.count(g))
			m[g]++;
		else
			m[g] = 1;
	}
	return m;
}

//配列圧縮(検証済)
//{1,36,1,3,8,-2,-92}を
//{2, 5,2,3,4, 1,  0}にする
template <class T>
vector<long long> press(vector<T> &v) {
	long long n = v.size();
	vector<long long> w(n);
	map<T, long long> m;
	for(T &p : v) m[p] = 0;
	long long i = 0;
	for(auto &p : m) {
		p.second = i;
		i++;
	}
	for(long long i = 0; i < n; i++) w.at(i) = m[v.at(i)];
	return w;
}

template <class T>
T divup(T a, T b) {
	//端数繰りあがり割り算
	assert(b != 0);
	T x = abs(a);
	T y = abs(b);
	T z = (x + y - 1) / y;
	if((a < 0 && b > 0) || (a > 0 && b < 0))
		return -z;
	else if(a == 0)
		return 0;
	else
		return z;
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

template <class T>
int sgn(T x) {	//符号関数
	if(x < 0) return -1;
	if(x == 0) return 0;
	return 1;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	if(mod == 1) return 0LL;
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a * x % mod == __gcd(a,mod)なるxを返す
// a が modの倍数でないことが条件
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

vvll comb(100, vll(100, -1));
long long com(long long n, long long k) {  //普通の二項計数(overflowに注意)
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(comb[n][k] != -1) return comb[n][k];
	ll res;
	if(n - k < k)
		res = com(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = com(n - 1, k - 1) + com(n - 1, k);
	comb[n][k] = res;
	return res;
}

// nCk modを求める
const ll MAX = 5100000;
// この値は求める二項計数の値に応じて変える
// MAX=3*10^7のとき1900msほど、ほぼ比例
// MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程)
long long fac[MAX], finv[MAX], inv[MAX];

void cominit() {
	// テーブルを作る前処理
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(ll i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
long long commod(ll n, ll k) {	// 二項係数計算
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(ll n, ll k) {  //順列計算
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}
long long hmod(ll n, ll k) {  // nHk計算
	// n個の区別しないoを区別するk個の箱に入れる方法の総数
	//(n+k-1)C(k-1)と等しい
	return commod(n + k - 1, n);
}
#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	INPUT(__VA_ARGS__)

template <class T>
void scan(T &a) {
	cin >> a;
}
template <class T>
void scan(vector<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
template <class T>
inline void print(T x) {
	cout << x << '\n';
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	cerr << ", ";
	view(p.second);
	cerr << ")";
}

template <typename T>
void view(std::set<T> &s) {
	if(s.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		cerr << "\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		cerr << ", ";
		view(c.second);
		cerr << ")\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		cerr << "] : ";
		view(t.second);
		cerr << "\n";
	}
	std::cerr << "}";
}

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                 \
	do {                                        \
		cerr << __LINE__ << " " << #x << " : "; \
		view(x);                                \
		cerr << "\n";                           \
	} while(0)
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	srand((unsigned)time(NULL));

	INT(n, k, timelim);
	timelim += k;
	vll t(n), p(n);
	rep(i, n) cin >> t[i] >> p[i];

	vvll dp(n + 1, vll(timelim + 1, -dekai));

	dp[0][k] = 0;

	rep(i, n) {
		rep(j, timelim + 1) {
			chmax(dp[i + 1][j], dp[i][j]);
			if(j - t[i] >= 0) {
				chmax(dp[i + 1][j], dp[i][j - t[i]] + p[i]);
			}
		}
	}

	vll cospa(timelim + 1);
	rep(i, timelim + 1) { cospa[i] = dp[n][i]; }
	debug(dp, cospa);
	vvll ep(timelim + 2, vll(timelim + 1, -dekai));
	ep[0][0] = 0;
	rep(i, timelim + 1) {
		rep(j, timelim + 1) {
			chmax(ep[i + 1][j], ep[i][j]);
			ll pre = j - i;
			if(pre >= 0) {
				chmax(ep[i + 1][j], ep[i][pre] + cospa[i]);
				chmax(ep[i + 1][j], ep[i + 1][pre] + cospa[i]);
			}
		}
	}
	ll maxi = -1;
	rep(i, timelim + 2) rep(j, timelim + 1) { chmax(maxi, ep[i][j]); }
	drop(maxi);

	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 191452
final_rank: 16

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int n, k, T; cin >> n >> k >> T;
    vector<int> t(n), p(n);
    rep(i, n) {
        cin >> t[i] >> p[i];
    }
    static int dp[2020][2020];
    rep(i, 2020) {
        rep(j, 2020) {
            dp[i][j] = -INF;
        }
    }
    dp[0][0] = 0;
    rep(i, n) {
        for(int j = 0; j <= T; ++ j) {
            if(j + t[i] <= T) chmax(dp[i + 1][j + t[i]], dp[i][j] + p[i]);
            chmax(dp[i + 1][j], dp[i][j]);
        }
    }
    vector<int> sdp(T + 1, 0);
    rep(i, T + 1) {
        sdp[i] = dp[n][i];
    }
    for(int i = 0; i <= T; ++ i) {
        for(int j = 1; j <= T; ++ j) {
            if(i + k + j <= T) chmax(sdp[i + k + j], sdp[i] + dp[n][j]);
        }
    }
    cout << *max_element(all(sdp)) << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 35128
final_rank: 6

code_snippet: /**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.12 21:46:46
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int main() {
    int n,k,t;cin >> n >> k >> t;
    vector<vector<ll>> dp(n+1,vector<ll>(t+1,0));
    for (int i = 0; i < n; i++) {
        int a,p;cin >> a >> p;
        for (int j = 0; j <= t; j++) {
            dp[i+1][j]=dp[i][j];
            if (j-a>=0) dp[i + 1][j] = max(dp[i+1][j],dp[i][j-a] + p);
        }
    }
    
    vector<vector<ll>> dp2(t+1,vector<ll>(t+1,0));
    for (int i = 0; i < t; i++) {
        for (int j = 0; j <= t; j++) {
            dp2[i+1][j] = dp2[i][j];
            if (j-k-i-1>=0) dp2[i + 1][j] = max(dp2[i + 1][j],dp2[i + 1][j-k-i-1] + dp[n][i+1]);
        }
    }
    ll ans = 0;
    for (int i = 0; i < t; i++) {
        ans = max(ans,dp[n][i+1]);
    }
    for (int i = 0; i <= t; i++) {
        ans = max(ans,dp2[t][i] + dp[n][t-i]);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 65672
final_rank: 8

code_snippet: /**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.12 21:46:46
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int main() {
    int n,k,t;cin >> n >> k >> t;
    vector<vector<ll>> dp(n+1,vector<ll>(t+1,0));
    for (int i = 0; i < n; i++) {
        int a,p;cin >> a >> p;
        for (int j = 0; j <= t; j++) {
            dp[i+1][j]=dp[i][j];
            if (j-a>=0) dp[i + 1][j] = max(dp[i+1][j],dp[i][j-a] + p);
        }
    }
    
    vector<vector<ll>> dp2(t+1,vector<ll>(t,0));
    for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
            dp2[i+1][j] = dp2[i][j];
            if (j-k-i-1>=0) dp2[i + 1][j] = max(dp2[i + 1][j],dp2[i + 1][j-k-i-1] + dp[n][i+1]);
        }
    }
    ll ans = 0;
    for (int i = 0; i < t; i++) {
        ans = max(ans,dp[n][i+1]);
    }
    for (int i = 0; i <= t; i++) {
        ans = max(ans,dp2[t][i] + dp[n][t-i]);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.4
time_ms: 20
memory_kb: 65696
final_rank: 18

code_snippet: #pragma region Macros
#pragma GCC optimize("O3")
#pragma GCC target("avx2,avx")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep2(i, a, b) for(ll i = a; i <= b; ++i)
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define rep3(i, a, b) for(ll i = a; i >= b; --i)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#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 fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
string YES[2] = {"NO", "YES"};
string Yes[2] = {"No", "Yes"};
string yes[2] = {"no", "yes"};
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return move(res);
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        cin >> a >> b >> c;
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return move(res);
}

#define i128 __int128_t
#define ull unsigned long long int
#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    for(auto &e : v) cout << e << " ";
    cout << endl;
    return os;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    cout << "(" << p.fi << ", " << p.se << ")";
    return os;
}
template <class S, class T> string to_string(pair<S, T> p) { return "(" + to_string(p.first) + "," + to_string(p.second) + ")"; }
template <class A> string to_string(A v) {
    if(v.empty()) return "{}";
    string ret = "{";
    for(auto &x : v) ret += to_string(x) + ",";
    ret.back() = '}';
    return ret;
}

void dump() { cerr << endl; }
template <class Head, class... Tail> void dump(Head head, Tail... tail) {
    cerr << to_string(head) << " ";
    dump(tail...);
}
#define endl '\n'
#ifdef _LOCAL
#undef endl
#define debug(x)                                                                                                                                               \
    cout << #x << ": ";                                                                                                                                        \
    dump(x)
#else
#define debug(x)
#endif
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); }
// ll rndll(ll n) { return uniform_int_distribution<ll>(0, n - 1)(rng); }

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
#pragma endregion
namespace modular {
constexpr ll MOD = 998244353;
const int MAXN = 1100000;
template <ll Modulus> class modint {
    using u64 = ll;

  public:
    u64 a;

    constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus) + Modulus) % Modulus) {}
    constexpr u64 &value() noexcept { return a; }
    constexpr const u64 &value() const noexcept { return a; }
    constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; }
    constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; }
    constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; }
    template <typename T> constexpr modint operator^(T rhs) const noexcept { return modint(*this) ^= rhs; }
    constexpr modint operator-() const noexcept { return modint() - *this; }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if(a >= Modulus) { a -= Modulus; }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if(a < rhs.a) { a += Modulus; }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr bool operator==(const modint rhs) const noexcept { return a == rhs.a; }
    template <typename T> constexpr modint &operator^=(T n) noexcept {
        modint<Modulus> res = 1;
        modint<Modulus> x = a;
        while(n) {
            if(n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        a = res.a;
        return *this;
    }
};
#define mint modint<MOD>
#define vmint vector<mint>
vmint Inv{0, 1}, Prd{1, 1}, Invprd{1, 1};
mint inv(int n) {
    if(n > MAXN) return mint(n) ^ (MOD - 2);
    if(Inv.size() > n)
        return Inv[n];
    else {
        for(int i = Inv.size(); i <= n; ++i) Inv.emplace_back(Inv[MOD % i] * (-MOD / i));
        return Inv[n];
    }
}
mint inv(mint x) { return inv(x.a); }
mint prd(int n) {
    if(Prd.size() > n)
        return Prd[n];
    else
        for(int i = Prd.size(); i <= n; ++i) Prd.emplace_back(Prd[i - 1] * i);
    return Prd[n];
}
mint invprd(int n) {
    if(Invprd.size() > n)
        return Invprd[n];
    else
        for(int i = Invprd.size(); i <= n; ++i) Invprd.emplace_back(Invprd[i - 1] * inv(i));
    return Invprd[n];
}
mint modpow(ll a, ll n) {
    mint x = a;
    return x ^= n;
}
mint operator/(mint l, mint r) { return l * inv(r); }
mint &operator/=(mint &l, mint r) { return l = l / r; }
mint C(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    return prd(a) * invprd(b) * invprd(a - b);
}
mint P(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    return prd(a) * invprd(a - b);
}
ostream &operator<<(ostream &os, mint a) {
    os << a.a;
    return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> a) {
    for(auto &e : a) os << e << " ";
    return os;
}
mint operator*(ll x, mint y) { return y * x; }
istream &operator>>(istream &is, mint &a) {
    ll x;
    is >> x;
    a = x;
    return is;
}
mint proot = 3;

void FMT(vmint &f, const bool is_inv = false) {
    const int n = f.size();
    const mint root = is_inv ? inv(proot) : proot;
    vmint g(n);
    for(int b = n >> 1; b > 0; b >>= 1) {
        mint a = root ^ ((MOD - 1) / (n / b)), p = 1;
        for(int i = 0; i < n; i += b << 1) {
            rep(j, b) {
                f[i + j + b] *= p;
                g[(i >> 1) + j] = f[i + j] + f[i + b + j];
                g[(n >> 1) + (i >> 1) + j] = f[i + j] - f[i + b + j];
            }
            p *= a;
        }
        swap(f, g);
    }
    if(is_inv) rep(i, n) f[i] *= inv(n);
}

vmint mul(vmint x, const vmint &y) {
    int n = x.size() + y.size() - 1;
    int s = 1;
    while(s < n) s <<= 1;
    x.resize(s);
    FMT(x);
    vmint z(s);
    rep(i, y.size()) z[i] = y[i];
    FMT(z);
    rep(i, s) x[i] *= z[i];
    FMT(x, true);
    x.resize(n);
    return x;
}

using Poly = vmint;
Poly operator-(Poly f) {
    for(auto &&e : f) e = -e;
    return f;
}
Poly &operator+=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] += r[i];
    return l;
}
Poly operator+(Poly l, const Poly &r) { return l += r; }
Poly &operator-=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] -= r[i];
    return l;
}
Poly operator-(Poly l, const Poly &r) { return l -= r; }
Poly &operator<<=(Poly &f, size_t n) { return f.insert(f.begin(), n, 0), f; }
Poly operator<<(Poly f, size_t n) { return f <<= n; }
Poly &operator>>=(Poly &f, size_t n) { return f.erase(f.begin(), f.begin() + min(f.size(), n)), f; }
Poly operator>>(Poly f, size_t n) { return f >>= n; }
Poly operator*(const Poly &l, const Poly &r) { return mul(l, r); }
Poly &operator*=(Poly &l, const Poly &r) { return l = l * r; }
Poly inv(const Poly &f) {
    Poly g{1 / f[0]};
    while(g.size() < f.size()) {
        Poly x(f.begin(), f.begin() + min(f.size(), g.size() << 1)), y = g;
        x.resize(g.size() << 1), FMT(x);
        y.resize(g.size() << 1), FMT(y);
        rep(i, x.size()) x[i] *= y[i];
        FMT(x, true);
        x >>= g.size();
        x.resize(g.size() << 1), FMT(x);
        rep(i, x.size()) x[i] *= -y[i];
        FMT(x, true);
        g.insert(g.end(), x.begin(), x.begin() + g.size());
    }
    return Poly{begin(g), begin(g) + f.size()};
}
Poly integ(const Poly &f) {
    Poly res(f.size() + 1);
    for(int i = 1; i < (int)res.size(); ++i) res[i] = f[i - 1] / i;
    return res;
}
Poly deriv(const Poly &f) {
    if(f.size() == 0) return Poly();
    Poly res(f.size() - 1);
    rep(i, res.size()) res[i] = f[i + 1] * (i + 1);
    return res;
}
Poly log(const Poly &f) {
    Poly g = integ(inv(f) * deriv(f));
    return Poly{g.begin(), g.begin() + f.size()};
}
Poly exp(const Poly &f) {
    Poly g{1};
    while(g.size() < f.size()) {
        Poly x(f.begin(), f.begin() + min(f.size(), g.size() * 2));
        x[0] += 1;
        g.resize(2 * g.size());
        x -= log(g);
        x *= {g.begin(), g.begin() + g.size() / 2};
        rep2(i, g.size() / 2, min<int>(x.size(), g.size()) - 1) g[i] = x[i];
    }
    return {g.begin(), g.begin() + f.size()};
}

} // namespace modular
using namespace modular;
int main() {
    INT(n, k, t);
    constexpr int N = 2001;
    vv(int, v, N);
    rep(i, n) {
        INT(a, b);
        v[a].eb(b);
    }
    vv(pii, w, N);
    rep(i, N) {
        if(empty(v[i])) continue;
        sort(all(v[i]), greater<>());
        rep(j, si(v[i])) { rep(K, t / i / (j + 1)) w[K].eb(i, v[i][j]); }
    }
    vll dp(t + 1);
    ll ans = 0;
    rep(i, t / k + 1) {
        for(auto [W, V] : w[i]) { rep3(j, t, W) chmax(dp[j], dp[j - W] + V); }
        chmax(ans, dp[t - i * k]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3932
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vii = vector<vi>;
using vl = vector<ll>; using vll = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
  ll n,k,t; scanf("%lld %lld %lld", &n, &k, &t);
  vector<ll> a(n), b(n);
  rep(i,n) {
    scanf("%lld %lld", &a[i], &b[i]);	
  }

  vl dp(t+1, 0);
  rep(i,n) {
    vl ndp(t+1, 0);
    rep(j,t+1) {
      if(j - a[i] >= 0) chmax(ndp[j], max(dp[j], dp[j - a[i]] + b[i]));
      else chmax(ndp[j], dp[j]);
    }
    dp = ndp;
  }

  vl dp2(t+k+1, 0);
  rep(i,t+1) {
    rep(j,t+k+1) {
      if(j - (i + k) >= 0) chmax(dp2[j], dp2[j - (i+k)] + dp[i]);
    }
  }
  cout << dp2[t+k] << "\n";
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3544
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

void chmax(int64_t &a, int64_t b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  int n, K, T;
  cin >> n >> K >> T;
  vector<pair<int, int64_t>> tp(n);
  for (auto &e: tp) {
    cin >> e.first >> e.second;
  }
//   sort(tp.begin(), tp.end(), [](const auto &lhs, const auto &rhs) {
//     // return lhs.second / lhs.first > rhs.second / rhs.first;
//     return lhs.second * rhs.first > rhs.second * lhs.first;
//   });
  vector<vector<int64_t>> dp(T + 1, vector<int64_t>(n + 1, -1));
  dp[0][0] = 0;
  rep(t, T) {
    rep(i, n + 1) {
      if (dp[t][i] < 0) continue;
      if (i + 1 <= n) { // i < n
        chmax(dp[t][i + 1], dp[t][i]);
        int ti;
        int64_t pi;
        tie(ti, pi) = tp[i];
        if (t + ti <= T) {
          chmax(dp[t + ti][i + 1], dp[t][i] + pi);
        }
      }
      if (t + K <= T) {
        chmax(dp[t + K][0], dp[t][i]);
      }
    }
  }
  int64_t ans = 0;
  for (int t = 0; t <= T; t++) {
    for (int i = 0; i <= n; i++) {
      chmax(ans, dp[t][i]);
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 34280
final_rank: 14

code_snippet: // I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
signed main(){
  init_io();
  ll n,k,T,ans=0;
  cin >> n >> k >> T;
  vector<ll> t(n),p(n);
  vector<vector<ll>> dp(n+1,vector<ll>(T+1,-1e15));
  vector<vector<ll>> dpx(T+1,vector<ll>(T+1,0));
  for(int i=0;i<n;i++){
    cin >> t[i] >> p[i];
  }
  for(int i=0;i<=T;i++){
    dp[0][i] = 0;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<=T;j++){
      dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
      if(j+t[i]<=T){
        dp[i+1][j+t[i]] = max(dp[i+1][j+t[i]],dp[i][j]+p[i]);
      }
    }
  }
  ans = dp[n][T];
  for(int i=0;i<=T;i++){
    dpx[0][i] = dp[n][i];
  }
  for(int i=1;i<=T;i++){
    for(int j=0;j<=T;j++){
      ll w = i+k;
      ll v = dp[n][i];
      ll x=0;
      if(j-w>=0){
        x = dpx[i][j-w]+v;
      }
      dpx[i][j] = max(dpx[i-1][j],x);
      ans = max(ans,dpx[i][j]);
    }
  }
  for(int i=0;i<=T;i++){
    ans = max(ans,dpx[T][i]);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 65904
final_rank: 13

code_snippet: // I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
signed main(){
  init_io();
  ll n,k,T,ans=0,c=13; cin >> n >> k >> T;
  vector<ll> t(n),p(n);
  vector<vector<ll>> dp(n+1,vector<ll>(T+1,-1e15));
  vector<vector<ll>> dpx(T+1,vector<ll>(T+1,0));
  for(int i=0;i<n;i++){
    cin >> t[i] >> p[i];
  }
  for(int i=0;i<=T;i++){
    dp[0][i] = 0;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<=T;j++){
      dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
      if(j+t[i]<=T){
        dp[i+1][j+t[i]] = max(dp[i+1][j+t[i]],dp[i][j]+p[i]);
      }
    }
  }
  ans = dp[n][T];
  for(int i=0;i<=T;i++){
    dpx[0][i] = dp[n][i];
  }
  for(int i=1;i<=T;i++){
    for(int j=0;j<=T;j++){
      ll w = i+k;
      ll v = dp[n][i];
      if(j-w<0) continue;
      dpx[i][j] = max(dpx[i-1][j],dpx[i][j-w]+v);
      ans = max(ans,dpx[i][j]);
    }
  }
  cout << ans << endl;
}
accuracy: 0.05714285714285714
time_ms: 20
memory_kb: 49160
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

void chmax(int64_t &a, int64_t b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  int n, K, T;
  cin >> n >> K >> T;
  vector<pair<int, int64_t>> tp(n);
  for (auto &e: tp) {
    cin >> e.first >> e.second;
  }
  sort(tp.begin(), tp.end(), [](const auto &lhs, const auto &rhs) {
    // return lhs.second / lhs.first > rhs.second / rhs.first;
    return lhs.second * rhs.first > rhs.second * lhs.first;
  });
  vector<vector<int64_t>> dp(T + 1, vector<int64_t>(n + 1, -1));
  dp[0][0] = 0;
  rep(t, T) {
    rep(i, n + 1) {
      if (dp[t][i] < 0) continue;
      if (i + 1 <= n) { // i < n
        chmax(dp[t][i + 1], dp[t][i]);
        int ti;
        int64_t pi;
        tie(ti, pi) = tp[i];
        if (t + ti <= T) {
          chmax(dp[t + ti][i + 1], dp[t][i] + pi);
        }
      }
      if (t + K <= T) {
        chmax(dp[t + K][0], dp[t][i]);
      }
    }
  }
  int64_t ans = 0;
  for (int t = 0; t <= T; t++) {
    for (int i = 0; i <= n; i++) {
      chmax(ans, dp[t][i]);
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 34280
final_rank: 11

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

//typedef
//------------------------------------------
using LL = int64_t;
using VL = vector<LL>;
using VVL = vector<VL>;
using VS = vector<string>;
using PLL = pair<LL, LL>;

//container util
//------------------------------------------
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define rwhole(f,x,...) ([&](decltype((x)) whole) { return (f)(rbegin(whole), rend(whole), ## __VA_ARGS__); })(x)
#define EACH(i,c) for(decltype((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define ALL(x) ::std::begin(x), ::std::end(x)
#define RALL(x) ::std::rbegin(x), ::std::rend(x)
#define SORT(c) whole(sort, c)
#define RSORT(c) rwhole(sort, c)

//constant
//--------------------------------------------
constexpr double EPS = 1e-10;
constexpr double PI  = 3.14159265358979323846;
constexpr int MOD = 1000000007;

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

//debug
//--------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//IO accelerate
//--------------------------------------------
struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

//template
//--------------------------------------------
// declaretion
template<typename T> istream& operator >>(istream& is, vector<T>& vec);
template<typename T1, typename T2> ostream& operator <<(ostream& os, const pair<T1, T2>& p);
template<typename T> ostream& operator <<(ostream& os, const vector<T>& vec);
template<typename T> ostream& operator <<(ostream& os, const vector<vector<T>>& vv);
template<typename T> vector<T> make_v(size_t a);
template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts);
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v);
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v);

// implementation
template<typename T>
istream& operator >>(istream& is, vector<T>& vec) {
	for(T& x: vec) is >> x;
	return is;
}
template<typename T1, typename T2>
ostream& operator <<(ostream& os, const pair<T1, T2>& p) {
	os << p.first << "," << p.second;
	return os;
}
template<typename T>
ostream& operator <<(ostream& os, const vector<T>& vec) {
	for(int i=0; i<vec.size(); i++){
		os << vec[i] << ( i+1 == vec.size() ? "" : "\t" );
	}
	return os;
}
template<typename T>
ostream& operator <<(ostream& s, const vector<vector<T>>& vv) {
	for (int i = 0; i < vv.size(); ++i) {
		s << vv[i] << endl;
	}
	return s;
}

// 多重vector
// auto dp=make_v<int>(4,h,w) みたいに使える
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 多重vectorのためのfill
// fill_v(dp,0) みたいに使える
template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
	for(auto &e:t) fill_v(e,v);
}

template<class T,class U> void chmax(T&a,U b){if(a<b)a=b;}
template<class T,class U> void chmin(T&a,U b){if(b<a)a=b;}
template<typename T> T gcd(T a, T b) { return b?gcd(b,a%b):a;}
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b;}

LL N,K,T;

//main code
int main(int argc, char *argv[])
{
	cin >> N >> K >> T;
    VL t(N), p(N);
	for (int i = 0; i < N; i++) {
		cin >> t[i] >> p[i];
	}
	VVL dp(N,VL(T+1,0));
	for (int i = t[0]; i < T+1; i++) {
		dp[0][i] = p[0];
	}
	for (int i = 1; i < N; i++) {
		for (int j = 0; j < T+1; j++) {
			if (j < t[i]) {
				dp[i][j] = dp[i-1][j];
			} else {
				dp[i][j] = max(dp[i-1][j-t[i]] + p[i], dp[i-1][j]);
			}
		}
	}

    VVL dp2(5000, VL(5000, 0));
    for(int i = 0; i <= T; i++) {
        for(int j = 0; j <= T; j++) {
            chmax(dp2[i+1][j], dp2[i][j]);
            if(i > j) chmax(dp2[i+K+1][i+K], dp2[i][j] + dp[N-1][i-j]);
        }
    } 
	//dump(dp);
	//LL ans = dp[N-1][T];
    /*
	for (int i = 1; i < T+1; i++) {
		if (dp[N-1][i] > dp[N-1][i-1]) { // iを区切りにやる
			LL tt = (K + i);
			LL d = T / tt;
			for (LL j = 1; j < d+1; j++) { // j回休憩する((i,k)の組み合わせがj回)
				LL tmp = j*dp[N-1][i];
				LL nokori = T - (i+K)*j;
				tmp += dp[N-1][nokori];
				chmax(ans, tmp);
			}
		}
	}
    */
    LL ans = 0;
    for(int i = 0; i < 5000; i++) {
        for(int j = 0; j < 5000; j++)  ans = max(ans, dp2[i][j]);
    }
	cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 230076
final_rank: 17