Problem:
Problem B
Card Game Strategy
Alice and Bob are going to play a card game. There are $n$ cards, each having an integer written on it. The game proceeds as follows:
Alice chooses an integer between $a$ and $b$, inclusive. Call this integer $t$. Alice then tells Bob the value of $t$.
Bob chooses $k$ out of the $n$ cards. Compute the sum of the integers written on the $k$ cards Bob chooses. Call this sum $u$.
Alice's objective is to make $|t - u|$ as large as possible and Bob's is to make $|t - u|$ as small as possible.
Prior to the game, both Alice and Bob know the values of $n$, $k$, $a$, and $b$, and also the integers on the cards. Both Alice and Bob will play optimally. In particular, Alice will make a choice, knowing that Bob will surely minimize $|t - u|$ for told $t$. Additionally, assume that Alice prefers to choose smaller $t$ if she has multiple equally good choices.
Your task is to determine the outcome of the game: the value of $t$ Alice will choose and the $k$ cards Bob will choose for that $t$.
Input
The input consists of two lines representing a single test case. The first line contains four integers $n$, $k$, $a$, and $b$ $(1 \leq k \leq n \leq 600, 0 \leq a \leq b \leq 180,000)$. The second line contains $n$ integers $x_1, ...,  x_n$ $(0 \leq x_i \leq 300)$, denoting that $x_i$ is written on the $i$-th card.
Output
Display two lines: The first line should contain an integer representing the value of $t$ Alice will choose. The second line should contain k distinct integers between 1 and $n$, inclusive, representing the indices of the cards Bob will choose. If Bob has multiple equally good choices, display any one of them.
Sample Input 1
4 2 58 100
10 10 50 80
Output for the Sample Input 1
75
2 3
Sample Input 2
8 3 1300 1800
2 0 1 5 0 4 1 9
Output for the Sample Input 2
1800
4 6 8

Submitted solutions:

code_snippet: // AOJ #2688 Card Game Strategy
// 2025.3.23

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

typedef pair<int, int> pii;
template<typename T, typename U>
inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U>
inline void amax(T &x, U y) { if(x < y) x = y; }

struct T2T {
    vector<unsigned> v;
    T2T() { }
    T2T(int n): v((n + 2) / 3) { }
    unsigned short get(int i) const {
        int t = i / 3, u = i % 3;
        if(u == 0) return v[t] & 1023;
        else if(u == 1) return (v[t] >> 10) & 1023;
        else return (v[t] >> 20) & 1023;
    }
    void set(int i, unsigned short x) {
        int t = i / 3, u = i % 3;
        if(u == 0) v[t] = (v[t] & ~1023U) | x;
        else if(u == 1) v[t] = (v[t] & ~(1023U << 10)) | (x << 10);
        else v[t] = (v[t] & ~(1023U << 20)) | (x << 20);
    }
};

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

    int N, K, L, R;
    cin >> N >> K >> L >> R;
    vector<pii> aip(N);
    for (int i = 0; i < N; i++){
        int a;
        cin >> a;
        aip[i] = make_pair(a, i);
    }
    sort(aip.begin(), aip.end());
    vector<int> A(N);
    for (int i = 0; i < N; i++) A[i] = aip[i].first;
    int sAll = accumulate(A.begin(), A.end(), 0);
    int sMin = accumulate(A.begin(), A.begin() + K, 0);
    int S = sAll - sMin;
    const short sInf = 0x7fff;
    vector<short> dp(S + 1, sInf);
    vector<short> mj(S + 1, K);
    vector<T2T> prvk(N - K, T2T(S + 1));
    dp[0] = 0;
    int preS = 0;
    for (int i = 0; i < N - K; i++){
        int a = A[K + i];
        for (int s = preS; s >= 0; s--){
            short j = dp[s];
            if(j == sInf) continue;
            int t = s + a;
            short m = mj[t];
            if(m > j){
                for (short k = j; k < m; k++){
                    int u = t - A[k];
                    if(dp[u] > k + 1){
                        dp[u] = k + 1;
                        prvk[i].set(u, k + 1);
                    }
                }
                mj[t] = j;
            }
        }
        if(i < K) preS -= A[i];
        preS += a;
    }
    int RR = R;
    if(sAll > RR) RR = sAll;

    vector<bool> v(RR + 1, false);
    for (int s = 0; s <= S; s++) v[sMin + s] = (dp[s] != sInf);

    const int infI = 0x3f3f3f3f;
    vector<pii> md(RR + 1, make_pair(infI, infI));
    int u = -1;
    for (int t = 0; t <= RR; t++){
        if(v[t]) u = t;
        if(u != -1) amin(md[t], make_pair(t - u, u));
    }
    u = -1;
    for (int t = RR; t >= 0; t--){
        if(v[t]) u = t;
        if(u != -1) amin(md[t], make_pair(u - t, u));
    }
    pair<int, pii> res = make_pair(-1, make_pair(-1, -1));
    for (int t = L; t <= R; t++){
        pair<int, pii> cur = make_pair(md[t].first, make_pair(-t, md[t].second));
        amax(res, cur);
    }

    vector<bool> usd(N, false);
    for (int i = 0; i < K; i++) usd[i] = true;
    int s = res.second.second - sMin;
    for (int i = N - K - 1; i >= 0; i--){
        short k = (short)prvk[i].get(s) - 1;
        if(k != -1){
            usd[K + i] = true;
            usd[k] = false;
            s -= A[K + i] - A[k];
        }
    }
    vector<int> crd;
    for (int i = 0; i < N; i++){
        if(usd[i]) crd.push_back(aip[i].second);
    }
    sort(crd.begin(), crd.end());

    cout << -res.second.first << endl;
    for (int i = 0; i < K; i++){
        if(i > 0) cout << ' ';
        cout << crd[i] + 1;
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 72520
final_rank: 1

code_snippet: bool debug=false;
#include <stdio.h>
#include <vector>
#include <bitset>
#include <utility>
using namespace std;
#define PB push_back
#define F first
#define S second
const int N=6e2+10;
const int S=2e5+10;
int myabs(int n){return n>0?n:-n;}
short min(short a,short b){return a>b?b:a;}
short max(short a,short b){return a>b?a:b;}
short dp[2][N][S],from[2][N][S],x[N];
int main(){
    bitset<N> bs;
    short n,k,nxt;
    int apos=0,pos,ans=0,dis=-1,temp,npos=0,a,b,tot=0;
    pair<short,pair<short,int>> now;
    vector<int> v;
    scanf("%hd%hd%d%d",&n,&k,&a,&b);    
    for(int i=1;i<=n;i++)scanf("%hd",&x[i]);
    for(int i=1;i<=k;i++)tot+=x[i];
    for(int i=0;i<=n;i++)for(int j=0;j<S;j++)dp[0][i][j]=dp[1][i][j]=k+1;
    dp[0][k][tot]=0;
    if(debug)printf("before dp\n");
    for(int i=k;i<=n;i++){
        for(int j=0;j<S;j++)if(dp[1][i][j]<=k)for(int p=dp[1][i][j]+1;p<=min(dp[1][i-1][j],k);p++)if(dp[0][i][j-x[p]]>p){
                from[0][i][j-x[p]]=i+2000;
                dp[0][i][j-x[p]]=p;
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i,j-x[p],p,1,i,j);
        }
        for(int j=0;j<S;j++)if(dp[0][i][j]<=k){
            if(dp[0][i+1][j]>dp[0][i][j]){
                dp[0][i+1][j]=dp[0][i][j];
                from[0][i+1][j]=i+1000;
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j,dp[0][i+1][j],0,i,j);
            }
            if(dp[1][i+1][j+x[i+1]]>dp[0][i][j]){
                dp[1][i+1][j+x[i+1]]=dp[0][i][j];
                from[1][i+1][j+x[i+1]]=i;
                if(debug)printf("dp[1][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j+x[i+1],dp[1][i+1][j+x[i+1]],0,i,j);
            }
        }
    }
    if(debug)printf("after dp\n");
    for(int i=0;i<S;i++)if(dp[0][n][i]<=k)v.PB(i);
    if(debug){
        printf("%d",v[0]);
        for(int i=1;i<v.size();i++)printf(" %d",v[i]);
        printf("\n");
    }
    for(int i=a;i<=b;i++){
        temp=S;
        while(npos<v.size()){
            if(v[npos]<i)npos++;
            else break;
        }
        if(npos>=v.size())pos=v.back();
        else if(npos==0)pos=v[npos];
        else if(v[npos]-i>i-v[npos-1])pos=v[npos-1];
        else pos=v[npos];
        if(dis<myabs(i-pos)){
            dis=myabs(i-pos);
            ans=i;
            apos=pos;
        }
    }
    printf("%d\n",ans);
    now={0,{n,apos}};
    bs.reset();
    for(int i=1;i<=k;i++)bs[i]=true;
    while(now.S.F>k){
        if(debug)printf("now=(%hd,%hd,%d)\n",now.F,now.S.F,now.S.S);
        nxt=from[now.F][now.S.F][now.S.S];
        if(nxt>=2000){
			nxt-=2000;
            bs[dp[now.F][now.S.F][now.S.S]]=false;
            now={1,{nxt,now.S.S+x[dp[now.F][now.S.F][now.S.S]]}};
        }
        else if(nxt>=1000)now={0,{nxt-1000,now.S.S}};
        else {
            bs[now.S.F]=true;
            now={0,{now.S.F-1,now.S.S-x[now.S.F]}};
        }
    }
    v.clear();
    for(int i=1;i<=n;i++)if(bs[i])v.PB(i);
    printf("%d",v[0]);
    for(int i=1;i<k;i++)printf(" %d",v[i]);
    printf("\n");
}
accuracy: 1
time_ms: 430
memory_kb: 516172
final_rank: 3

code_snippet: bool debug=false;
#include <stdio.h>
#include <vector>
#include <bitset>
#include <utility>
using namespace std;
#define PB push_back
#define F first
#define S second
const int N=6e2+10;
const int S=18e3+10;
int myabs(int n){return n>0?n:-n;}
short min(short a,short b){return a>b?b:a;}
short max(short a,short b){return a>b?a:b;}
pair<short,short> from[2][N][S];
short dp[2][N][S],x[N];
int main(){
    bitset<N> bs;
    short n,k;
    int apos=0,pos,ans=0,dis=-1,temp,npos=0,a,b,tot=0;
    pair<short,pair<short,int>> now;
    pair<short,int> nxt;
    vector<int> v;
    scanf("%hd%hd%d%d",&n,&k,&a,&b);    
    for(int i=1;i<=n;i++)scanf("%hd",&x[i]);
    for(int i=1;i<=k;i++)tot+=x[i];
    for(int i=0;i<=n;i++)for(int j=0;j<S;j++)dp[0][i][j]=dp[1][i][j]=k+1;
    for(int i=0;i<=n;i++)for(int j=0;j<S;j++)from[0][i][j]=from[1][i][j]={0,0};
    dp[0][k][tot]=0;
    if(debug)printf("before dp\n");
    for(int i=k;i<=n;i++){
        for(int j=0;j<S;j++)if(dp[1][i][j]<=k)for(int p=dp[1][i][j]+1;p<=min(dp[1][i-1][j],k);p++)if(dp[0][i][j-x[p]]>p){
                from[0][i][j-x[p]]={i+2000,x[p]};
                dp[0][i][j-x[p]]=p;
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i,j-x[p],p,1,i,j);
        }
        for(int j=0;j<S;j++)if(dp[0][i][j]<=k){
            if(dp[0][i+1][j]>dp[0][i][j]){
                dp[0][i+1][j]=dp[0][i][j];
                from[0][i+1][j]={i+1000,0};
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j,dp[0][i+1][j],0,i,j);
            }
            if(dp[1][i+1][j+x[i+1]]>dp[0][i][j]){
                dp[1][i+1][j+x[i+1]]=dp[0][i][j];
                from[1][i+1][j+x[i+1]]={i,x[i+1]};
                if(debug)printf("dp[1][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j+x[i+1],dp[1][i+1][j+x[i+1]],0,i,j);
            }
        }
    }
    if(debug)printf("after dp\n");
    for(int i=0;i<S;i++)if(dp[0][n][i]<=k)v.PB(i);
    if(debug){
        printf("%d",v[0]);
        for(int i=1;i<v.size();i++)printf(" %d",v[i]);
        printf("\n");
    }
    for(int i=a;i<=b;i++){
        temp=S;
        while(npos<v.size()){
            if(v[npos]<i)npos++;
            else break;
        }
        if(npos>=v.size())pos=v.back();
        else if(npos==0)pos=v[npos];
        else if(v[npos]-i>i-v[npos-1])pos=v[npos-1];
        else pos=v[npos];
        if(dis<myabs(i-pos)){
            dis=myabs(i-pos);
            ans=i;
            apos=pos;
        }
    }
    printf("%d\n",ans);
    now={0,{n,apos}};
    bs.reset();
    for(int i=1;i<=k;i++)bs[i]=true;
    while(now.S.F>k){
        if(debug)printf("now=(%hd,%hd,%d)\n",now.F,now.S.F,now.S.S);
        nxt=from[now.F][now.S.F][now.S.S];
        if(nxt.F>=2000){
			nxt.F-=2000;
            bs[dp[now.F][now.S.F][now.S.S]]=false;
            now={1,{nxt.F,now.S.S+nxt.S}};
        }
        else if(nxt.F>=1000)now={0,{nxt.F-1000,now.S.S}};
        else {
            bs[now.S.F]=true;
            now={0,{now.S.F-1,now.S.S-nxt.S}};
        }
    }
    v.clear();
    for(int i=1;i<=n;i++)if(bs[i])v.PB(i);
    printf("%d",v[0]);
    for(int i=1;i<k;i++)printf(" %d",v[i]);
    printf("\n");
}
accuracy: 0.21428571428571427
time_ms: 20
memory_kb: 65824
final_rank: 9

code_snippet: bool debug=false;
#include <stdio.h>
#include <vector>
#include <bitset>
#include <utility>
using namespace std;
#define PB push_back
#define F first
#define S second
const int N=6e2+10;
const int S=18e3+10;
int myabs(int n){return n>0?n:-n;}
short min(short a,short b){return a>b?b:a;}
short max(short a,short b){return a>b?a:b;}
pair<short,int> from[2][N][S];
short dp[2][N][S],x[N];
int main(){
    bitset<N> bs;
    short n,k;
    int apos=0,pos,ans=0,dis=-1,temp,npos=0,a,b,tot=0;
    pair<short,pair<short,int>> now;
    pair<short,int> nxt;
    vector<int> v;
    scanf("%hd%hd%d%d",&n,&k,&a,&b);    
    for(int i=1;i<=n;i++)scanf("%hd",&x[i]);
    for(int i=1;i<=k;i++)tot+=x[i];
    for(int i=0;i<=n;i++)for(int j=0;j<S;j++)dp[0][i][j]=dp[1][i][j]=k+1;
    for(int i=0;i<=n;i++)for(int j=0;j<S;j++)from[0][i][j]=from[1][i][j]={0,0};
    dp[0][k][tot]=0;
    if(debug)printf("before dp\n");
    for(int i=k;i<=n;i++){
        for(int j=0;j<S;j++)if(dp[1][i][j]<=k)for(int p=dp[1][i][j]+1;p<=min(dp[1][i-1][j],k);p++)if(dp[0][i][j-x[p]]>p){
                from[0][i][j-x[p]]={i+1000,j};
                dp[0][i][j-x[p]]=p;
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i,j-x[p],p,1,i,j);
        }
        for(int j=0;j<S;j++)if(dp[0][i][j]<=k){
            if(dp[0][i+1][j]>dp[0][i][j]){
                dp[0][i+1][j]=dp[0][i][j];
                from[0][i+1][j]={i,j};
                if(debug)printf("dp[0][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j,dp[0][i+1][j],0,i,j);
            }
            if(dp[1][i+1][j+x[i+1]]>dp[0][i][j]){
                dp[1][i+1][j+x[i+1]]=dp[0][i][j];
                from[1][i+1][j+x[i+1]]={i,j};
                if(debug)printf("dp[1][%d][%d]=%d (from dp[%d][%d][%d])\n",i+1,j+x[i+1],dp[1][i+1][j+x[i+1]],0,i,j);
            }
        }
    }
    if(debug)printf("after dp\n");
    for(int i=0;i<S;i++)if(dp[0][n][i]<=k)v.PB(i);
    if(debug){
        printf("%d",v[0]);
        for(int i=1;i<v.size();i++)printf(" %d",v[i]);
        printf("\n");
    }
    for(int i=a;i<=b;i++){
        temp=S;
        while(npos<v.size()){
            if(v[npos]<i)npos++;
            else break;
        }
        if(npos>=v.size())pos=v.back();
        else if(npos==0)pos=v[npos];
        else if(v[npos]-i>i-v[npos-1])pos=v[npos-1];
        else pos=v[npos];
        if(dis<myabs(i-pos)){
            dis=myabs(i-pos);
            ans=i;
            apos=pos;
        }
    }
    printf("%d\n",ans);
    now={0,{n,apos}};
    bs.reset();
    for(int i=1;i<=k;i++)bs[i]=true;
    while(now.S.F>k){
        if(debug)printf("now=(%hd,%hd,%d)\n",now.F,now.S.F,now.S.S);
        nxt=from[now.F][now.S.F][now.S.S];
        if(nxt.F>=1000){
			nxt.F-=1000;
            bs[dp[now.F][now.S.F][now.S.S]]=false;
            now={1,nxt};
        }
        else if(nxt.S==now.S.S)now={0,nxt};
        else {
            bs[now.S.F]=true;
            now={0,nxt};
        }
    }
    v.clear();
    for(int i=1;i<=n;i++)if(bs[i])v.PB(i);
    printf("%d",v[0]);
    for(int i=1;i<k;i++)printf(" %d",v[i]);
    printf("\n");
}
accuracy: 0.21428571428571427
time_ms: 40
memory_kb: 108104
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <climits>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <tuple>
#include <iostream>
#include <deque>
#include <array>
#include <set>
#include <functional>
#include <memory>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
void set_if_renew(int& current_diff, int& current_t, int &current_cards, const int t, const int cards) {
	if (current_diff < std::abs(t - cards)) {
		current_diff = std::abs(t - cards);
		current_t = t;
		current_cards = cards;
	}
	else if (current_diff == std::abs(t - cards) && current_t > t) {
		current_t = t;
		current_cards = cards;
	}
}
struct Card {
	int value, index;
};
int main() {
	int n, k, a, b; std::cin >> n >> k >> a >> b;
	std::vector<Card> cards(n);
	for (auto i = 0; i < n; ++i) {
		std::cin >> cards[i].value;
		cards[i].index = i;
	}
	const int sum = std::accumulate(cards.begin(), cards.end(), 0, [](const int acc, const Card& c) {return acc + c.value; });
	const int max = std::max_element(cards.begin(), cards.end(), [](const Card &a, const Card &b) {return a.value < b.value; })->value;
	std::sort(cards.begin(), cards.end(), [](const Card& a, const Card& b) {return a.value < b.value; });
	std::vector<std::vector<int>> last_index;
	for (auto i = 0; i <= k; ++i) {last_index.emplace_back(std::max(b, max * i) + 1, INT_MAX);}
	std::queue<int> queue; queue.push(0); last_index[0][0] = 0;
	for (auto count = 1; count <= k; ++count) {
		for (int _c = queue.size(); _c > 0; --_c) {
			auto top = queue.front(); queue.pop();
			for (auto i = last_index[count - 1][top]; i < n && cards[i].value + top < last_index[count].size(); ++i) {
				if (last_index[count][cards[i].value + top] == INT_MAX) {
					queue.push(cards[i].value + top);
				}
				if (last_index[count][cards[i].value + top] > i) {
					last_index[count][cards[i].value + top] = i + 1;
				}
			}
		}
	}
	std::vector<int> suspects;
	while (!queue.empty()) {
		suspects.push_back(queue.front()); queue.pop();
	}
	std::sort(suspects.begin(), suspects.end());
	int max_diff = -1;
	int min_t = INT_MAX;
	int min_cards = 0;
	for (int i = std::lower_bound(suspects.begin(), suspects.end(), a) - suspects.begin(); i < suspects.size(); ++i) {
		if (i == 0) {
			max_diff = suspects[0] - a;
			min_t = a;
			min_cards = suspects[0];
		}
		else {
			if (suspects[i - 1] >= b) break;
			int upper = suspects[i];
			int lower = suspects[i - 1];
			int diff = upper - lower;
			if (max_diff < diff / 2) {
				if (lower <= a && a <= upper) {
					if (a - lower < upper - a) set_if_renew(max_diff, min_t, min_cards, a, lower);
					else set_if_renew(max_diff, min_t, min_cards, a, upper);
				}
				if (lower <= b && b <= upper) {
					if (b - lower < upper - b) set_if_renew(max_diff, min_t, min_cards, b, lower);
					else set_if_renew(max_diff, min_t, min_cards, b, upper);
				}
				if (a <= lower + diff / 2 && lower + diff / 2 <= b) {
					set_if_renew(max_diff, min_t, min_cards, lower + diff / 2, lower);
				}
				if (a <= upper - diff / 2 && upper - diff / 2 <= b) {
					set_if_renew(max_diff, min_t, min_cards, upper - diff / 2, upper);
				}
			}
		}
	}
	if (suspects.back() <= b) set_if_renew(max_diff, min_t, min_cards, b, suspects.back());
	std::cout << min_t << "\n";
	std::vector<int> result;
	for (int i = k; i > 0; --i) {
		result.push_back(cards[last_index[i][min_cards] - 1].index + 1);
		min_cards = min_cards - cards[last_index[i][min_cards] - 1].value;
	}
	for (auto i = 0; i < result.size(); ++i) {
		if (i != 0) std::cout << ' ';
		std::cout << result[i];
	}
	std::cout << '\n';
}
accuracy: 1
time_ms: 3930
memory_kb: 425904
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
template<class S,class T> bool chmax(S& x,T y){
	if(x<y){x=y;return 1;}
	return 0;
}
template<class S,class T> bool chmin(S& x,T y){
	if(x>y){x=y;return 1;}
	return 0;
}
const int MAX_N=610,MAX_S=181000;
int N,K,A,B,a[MAX_N];
short inf=1000;
short dp0[MAX_N+1][MAX_S+1],a0[MAX_N+1][MAX_S+1];
short dp1[MAX_N+1][MAX_S+1],a1[MAX_N+1][MAX_S+1];
int l[MAX_S+1],r[MAX_S+1];
bool can[MAX_S+1];
bool sel[MAX_N];
int main(){
	cin>>N>>K>>A>>B;
	rep(i,N) cin>>a[i];
	int S=accumulate(a,a+N,0);
	for(int j=K;j<=N;j++) rep(s,S+1) dp0[j][s]=dp1[j][s]=inf;
	dp0[K][accumulate(a,a+K,0)]=0;
	for(int j=K;j<=N;j++){
		rep(s,S+1){
			if(dp1[j][s]==inf) continue;
			if(chmin(dp1[j+1][s],dp1[j][s])) a1[j+1][s]=-1;
			int l=dp1[j][s],r=K;
			if(j>K) chmin(r,dp1[j-1][s]);
			for(int i=l;i<r;i++) if(chmin(dp0[j][s-a[i]],i+1)) a0[j][s-a[i]]=i;
		}
		rep(s,S+1){
			if(dp0[j][s]==inf) continue;
			if(chmin(dp0[j+1][s],dp0[j][s])) a0[j+1][s]=-1;
			if(chmin(dp1[j+1][s+a[j]],dp0[j][s])) a1[j+1][s+a[j]]=-2;
		}
	}
	rep(s,S+1) can[s]=(dp0[N][s]!=inf);
	rep(i,MAX_S+1){
		l[i]=1e9;
		if(can[i]) l[i]=0;
		if(i>0) chmin(l[i],l[i-1]+1);
	}
	rep(i,MAX_S+1){
		r[MAX_S-i]=1e9;
		if(can[MAX_S-i]) r[MAX_S-i]=0;
		if(i>0) chmin(r[MAX_S-i],r[MAX_S-i+1]+1);
	}
	int ans=-1,x;
	for(int i=A;i<=B;i++){
		if(chmax(ans,min(l[i],r[i]))) x=i;
	}
	cout<<x<<endl;
	int s=x-ans;
	if(s<0||!can[s]) s=x+ans;
//	assert(can[s]);
	rep(i,K) sel[i]=1;
	int t=0,j=N;
	while(!(t==0&&j==K&&s==accumulate(a,a+K,0))){
//		printf("t,j,s=%d,%d,%d\n",t,j,s);
		if(t==0){
			short x=a0[j][s];
			if(x==-1) j--;
			else{
//				assert(x>=0);
				s+=a[x];
				t=1;
				sel[x]=0;
			}
		}else{
			short x=a1[j][s];
			if(x==-1) j--;
			else{
//				assert(x==-2);
				j--;
				s-=a[j];
				t=0;
				sel[j]=1;
			}
		}
	}
	rep(i,N) if(sel[i]) cout<<i+1<<" ";
	puts("");
}
accuracy: 1
time_ms: 160
memory_kb: 234340
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
template<class S,class T> bool chmax(S& x,T y){
	if(x<y){x=y;return 1;}
	return 0;
}
template<class S,class T> bool chmin(S& x,T y){
	if(x>y){x=y;return 1;}
	return 0;
}
const int MAX_N=600,MAX_S=180000;
int N,K,A,B,a[MAX_N];
short inf=1000;
short dp0[MAX_N+1][MAX_S+1],a0[MAX_N+1][MAX_S+1];
short dp1[MAX_N+1][MAX_S+1],a1[MAX_N+1][MAX_S+1];
int l[MAX_S+1],r[MAX_S+1];
bool can[MAX_S+1];
bool sel[MAX_N];
int main(){
	cin>>N>>K>>A>>B;
	rep(i,N) cin>>a[i];
	int S=accumulate(a,a+N,0);
	for(int j=K;j<=N;j++) rep(s,S+1) dp0[j][s]=dp1[j][s]=inf;
	dp0[K][accumulate(a,a+K,0)]=0;
	for(int j=K;j<=N;j++){
		rep(s,S+1){
			if(dp1[j][s]==inf) continue;
			if(chmin(dp1[j+1][s],dp1[j][s])) a1[j+1][s]=-1;
			int l=dp1[j][s],r=K;
			if(j>K) chmin(r,dp1[j-1][s]);
			for(int i=l;i<r;i++) if(chmin(dp0[j][s-a[i]],i+1)) a0[j][s-a[i]]=i;
		}
		rep(s,S+1){
			if(dp0[j][s]==inf) continue;
			if(chmin(dp0[j+1][s],dp0[j][s])) a0[j+1][s]=-1;
			if(chmin(dp1[j+1][s+a[j]],dp0[j][s])) a1[j+1][s+a[j]]=-2;
		}
	}
	rep(s,S+1) can[s]=(dp0[N][s]!=inf);
	rep(i,MAX_S+1){
		l[i]=1e9;
		if(can[i]) l[i]=0;
		if(i>0) chmin(l[i],l[i-1]+1);
	}
	rep(i,MAX_S+1){
		r[MAX_S-i]=1e9;
		if(can[MAX_S-i]) r[MAX_S-i]=0;
		if(i>0) chmin(r[MAX_S-i],r[MAX_S-i+1]+1);
	}
	int ans=-1,x;
	for(int i=A;i<=B;i++){
		if(chmax(ans,min(l[i],r[i]))) x=i;
	}
	cout<<x<<endl;
	int s=x-ans;
	if(s<0||!can[s]) s=x+ans;
//	assert(can[s]);
	rep(i,K) sel[i]=1;
	int t=0,j=N;
	while(!(t==0&&j==K&&s==accumulate(a,a+K,0))){
//		printf("t,j,s=%d,%d,%d\n",t,j,s);
		if(t==0){
			short x=a0[j][s];
			if(x==-1) j--;
			else{
//				assert(x>=0);
				s+=a[x];
				t=1;
				sel[x]=0;
			}
		}else{
			short x=a1[j][s];
			if(x==-1) j--;
			else{
//				assert(x==-2);
				j--;
				s-=a[j];
				t=0;
				sel[j]=1;
			}
		}
	}
	rep(i,N) if(sel[i]) cout<<i+1<<" ";
	puts("");
}
accuracy: 0.8214285714285714
time_ms: 360
memory_kb: 234236
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
template<class S,class T> bool chmax(S& x,T y){
	if(x<y){x=y;return 1;}
	return 0;
}
template<class S,class T> bool chmin(S& x,T y){
	if(x>y){x=y;return 1;}
	return 0;
}
const int MAX_N=600,MAX_S=180000;
int N,K,A,B,a[MAX_N];
short inf=1000;
short dp0[MAX_N][MAX_S+1],a0[MAX_N][MAX_S+1];
short dp1[MAX_N][MAX_S+1],a1[MAX_N][MAX_S+1];
int l[MAX_S+1],r[MAX_S+1];
bool can[MAX_S+1];
bool sel[MAX_N];
int main(){
	cin>>N>>K>>A>>B;
	rep(i,N) cin>>a[i];
	int S=accumulate(a,a+N,0);
	for(int j=K;j<=N;j++) rep(s,S+1) dp0[j][s]=dp1[j][s]=inf;
	dp0[K][accumulate(a,a+K,0)]=0;
	for(int j=K;j<=N;j++){
		rep(s,S+1){
			if(dp1[j][s]==inf) continue;
			if(chmin(dp1[j+1][s],dp1[j][s])) a1[j+1][s]=-1;
			int l=dp1[j][s],r=K;
			if(j>K) chmin(r,dp1[j-1][s]);
			for(int i=l;i<r;i++) if(chmin(dp0[j][s-a[i]],i+1)) a0[j][s-a[i]]=i;
		}
		rep(s,S+1){
			if(dp0[j][s]==inf) continue;
			if(chmin(dp0[j+1][s],dp0[j][s])) a0[j+1][s]=-1;
			if(chmin(dp1[j+1][s+a[j]],dp0[j][s])) a1[j+1][s+a[j]]=-2;
		}
	}
	rep(s,S+1) can[s]=(dp0[N][s]!=inf);
	rep(i,MAX_S+1){
		l[i]=1e9;
		if(can[i]) l[i]=0;
		if(i>0) chmin(l[i],l[i-1]+1);
	}
	rep(i,MAX_S+1){
		r[MAX_S-i]=1e9;
		if(can[MAX_S-i]) r[MAX_S-i]=0;
		if(i>0) chmin(r[MAX_S-i],r[MAX_S-i+1]+1);
	}
	int ans=-1,x;
	for(int i=A;i<=B;i++){
		if(chmax(ans,min(l[i],r[i]))) x=i;
	}
	cout<<x<<endl;
	int s=x-ans;
	if(s<0||!can[s]) s=x+ans;
//	assert(can[s]);
	rep(i,K) sel[i]=1;
	int t=0,j=N;
	while(!(t==0&&j==K&&s==accumulate(a,a+K,0))){
//		printf("t,j,s=%d,%d,%d\n",t,j,s);
		if(t==0){
			short x=a0[j][s];
			if(x==-1) j--;
			else{
//				assert(x>=0);
				s+=a[x];
				t=1;
				sel[x]=0;
			}
		}else{
			short x=a1[j][s];
			if(x==-1) j--;
			else{
//				assert(x==-2);
				j--;
				s-=a[j];
				t=0;
				sel[j]=1;
			}
		}
	}
	rep(i,N) if(sel[i]) cout<<i+1<<" ";
	puts("");
}
accuracy: 0.5357142857142857
time_ms: 510
memory_kb: 232388
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
template<class S,class T> bool chmax(S& x,T y){
	if(x<y){x=y;return 1;}
	return 0;
}
template<class S,class T> bool chmin(S& x,T y){
	if(x>y){x=y;return 1;}
	return 0;
}
const int MAX_N=600,MAX_S=180000;
int N,K,A,B,a[MAX_N];
short inf=1000;
short dp0[MAX_N][MAX_S+1],a0[MAX_N][MAX_S+1];
short dp1[MAX_N][MAX_S+1],a1[MAX_N][MAX_S+1];
int l[MAX_S+1],r[MAX_S+1];
bool can[MAX_S+1];
bool sel[MAX_N];
int main(){
	cin>>N>>K>>A>>B;
	rep(i,N) cin>>a[i];
	int S=accumulate(a,a+N,0);
	for(int j=K;j<=N;j++) rep(s,S+1) dp0[j][s]=dp1[j][s]=inf;
	dp0[K][accumulate(a,a+K,0)]=0;
	for(int j=K;j<=N;j++){
		rep(s,S+1){
			if(dp1[j][s]==inf) continue;
			if(chmin(dp1[j+1][s],dp1[j][s])) a1[j+1][s]=-1;
			int l=dp1[j][s],r=K;
			if(j>K) chmin(r,dp1[j-1][s]);
			for(int i=l;i<r;i++) if(chmin(dp0[j][s-a[i]],i+1)) a0[j][s-a[i]]=i;
		}
		rep(s,S+1){
			if(dp0[j][s]==inf) continue;
			if(chmin(dp0[j+1][s],dp0[j][s])) a0[j+1][s]=-1;
			if(chmin(dp1[j+1][s+a[j]],dp0[j][s])) a1[j+1][s+a[j]]=-2;
		}
	}
	rep(s,S+1) can[s]=(dp0[N][s]!=inf);
	rep(i,MAX_S+1){
		l[i]=1e9;
		if(can[i]) l[i]=0;
		if(i>0) chmin(l[i],l[i-1]+1);
	}
	rep(i,MAX_S+1){
		r[MAX_S-i]=1e9;
		if(can[MAX_S-i]) r[MAX_S-i]=0;
		if(i>0) chmin(r[MAX_S-i],r[MAX_S-i+1]+1);
	}
	int ans=-1,x;
	for(int i=A;i<=B;i++){
		if(chmax(ans,min(l[i],r[i]))) x=i;
	}
	cout<<x<<endl;
	int s=x-ans;
	if(s<0||!can[s]) s=x+ans;
	assert(can[s]);
	rep(i,K) sel[i]=1;
	int t=0,j=N;
	while(!(t==0&&j==K&&s==accumulate(a,a+K,0))){
//		printf("t,j,s=%d,%d,%d\n",t,j,s);
		if(t==0){
			short x=a0[j][s];
			if(x==-1) j--;
			else{
				assert(x>=0);
				s+=a[x];
				t=1;
				sel[x]=0;
			}
		}else{
			short x=a1[j][s];
			if(x==-1) j--;
			else{
				assert(x==-2);
				j--;
				s-=a[j];
				t=0;
				sel[j]=1;
			}
		}
	}
	rep(i,N) if(sel[i]) cout<<i+1<<" ";
	puts("");
}
accuracy: 0.5357142857142857
time_ms: 490
memory_kb: 232292
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int p[610];
short dp[2][610][180010];
short rev[2][610][180010];
int dist[180100];
int main(){
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	for(int i=0;i<a;i++)scanf("%d",p+i);
	for(int i=0;i<2;i++)for(int j=0;j<=a;j++)for(int k=0;k<180010;k++)
		dp[i][j][k]=999;
	int tmp=0;
	for(int i=0;i<b;i++)tmp+=p[i];
	dp[0][b][tmp]=0;
	for(int i=b;i<=a;i++){
		for(int j=1;j>=0;j--){
			for(int k=180000;k>=0;k--){
				if(dp[j][i][k]>900)continue;
				if(a==i&&j==0)continue;
				if(j==0){
					if(dp[0][i+1][k]>dp[j][i][k]){
						dp[0][i+1][k]=dp[j][i][k];
						rev[0][i+1][k]=-1;
					}
					if(dp[1][i+1][k+p[i]]>dp[j][i][k]){
						dp[1][i+1][k+p[i]]=dp[j][i][k];
						rev[1][i+1][k+p[i]]=-2;
					}
				}else{
					for(int l=dp[1][i][k];l<=min(b-1,(int)dp[1][i-1][k]);l++){
						if(dp[0][i][k-p[l]]>l+1){
						//	printf("%d %d %d: %d\n",0,i,k-p[l],l+1);
							dp[0][i][k-p[l]]=l+1;
							rev[0][i][k-p[l]]=l;
						}
					}
				}
			}
		}
	}
	for(int i=0;i<180010;i++)dist[i]=999999999;
	for(int i=0;i<=180000;i++)if(dp[0][a][i]<=b)dist[i]=0;
	for(int i=1;i<=180000;i++)dist[i]=min(dist[i],dist[i-1]+1);
	for(int i=179999;i>=0;i--)dist[i]=min(dist[i],dist[i+1]+1);
	int ret=c;
	int ans=0;
	for(int i=c;i<=d;i++){
		if(dist[i]>ans){
			ans=dist[i];ret=i;
		}
	}
	printf("%d\n",ret);
	vector<int>ind;
	
	int A=0;
	int B=a;
	int C=ret;
	if(ret-ans>=0&&dp[0][a][ret-ans]<=b)C=ret-ans;
	else C=ret+ans;
	
	for(int i=dp[0][a][C];i<b;i++)ind.push_back(i+1);
	while(A!=0||B!=b||C!=tmp){
	
	//	printf("%d %d %d\n",A,B,C);
	//	if(C>180000)break;
		if(rev[A][B][C]==-1)B--;
		else if(rev[A][B][C]==-2){
			ind.push_back(B);
			B--;
			A=0;
			C-=p[B];
		}else{
			int to=dp[A][B][C];
			A=1;
			C+=p[to-1];
			for(int i=dp[A][B][C];i<to-1;i++)ind.push_back(i+1);
		}
	}
	std::sort(ind.begin(),ind.end());
	for(int i=0;i<ind.size();i++){
		if(i)printf(" ");
		printf("%d",ind[i]);
	}
	printf("\n");
}
accuracy: 1
time_ms: 280
memory_kb: 831204
final_rank: 4