Problem:
E: Red Black Balloons
Story
Homura-chan's dream comes true. It means ICPC Asia regional contest 20xx will be held in Sapporo! Homura-chan has been working hard for the preparation. And finally, it's the previous day of the contest. Homura-chan started to stock balloons to be delivered to contestants who get accepted. However, she noticed that there were only two colors of balloons: red and black.
Problem Statement
ICPC Asia regional contest in Sapporo plans to provide
N
problems to contestants. Homura-chan is a professional of contest preparation, so she already knows how many contestants would get acceptance for each problem (!!),
a_i
contestants for the
i
-th problem. You can assume the prediction is perfectly accurate. Ideally, Homura-chan would assign a distinct color of a balloon to each problem respectively. But you know, she has only two colors red and black now. Thus, Homura-chan comes up with the idea to differentiate the size of balloons in
K
levels
, that is, each problem has a balloon with a distinct pair of (color, size).
Homura-chan now has
r_i
red balloons with size
i
(
1 \leq i \leq K
) and
b_j
black balloons with size
j
(
1 \leq j \leq K
). Suppose we assign a pair
(c_i, s_i)
of a color
c_i
(red or black) and a size
s_i
to the
i
-th problem, for each
i
. As noted, we have to assign distinct pairs to each problem, more precisely,
(c_i, s_i) \neq (c_j, s_j)
holds for
i \neq j
. Moreover, the number of balloons with
(c_i, s_i)
must be no less than
a_i
. In the case that there are no such assignments, Homura-chan can change the size of balloons by her magic power. Note that Homura-chan doesn't know magic to change the color of balloons, so it's impossible.
Your task is to write a program computing the minimum number of balloons whose size is changed by Homura-chan's magic to realize an assignment satisfying the above-mentioned conditions. If there is no way to achieve such an assignment even if Homura-chan changes the size of balloons infinitely, output
-1
instead.
Input
N
K
a_1 ... a_N
r_1 ... r_K
b_1 ... b_K
Constraints
1 \leq N,K \leq 60
N \leq 2K
1 \leq a_i \leq 50
(1 \leq i \leq N)
1 \leq r_j,b_j \leq 50
(1 \leq j \leq K)
Inputs consist only of integers.
Output
Output the minimum number of balloons whose size is changed to achieve an assignment in a line. If there are no ways to achieve assignments, output
-1
instead.
Sample Input 1
3 2
6 5 4
8 1
7 1
Output for Sample Input 1
3
Homura-chan changes the size of three red balloons from 1 to 2.  Then she can assign (black,1) to the problem 1, (red,1) to the problem 2, and (red,2) to the problem 3.
Sample Input 2
2 1
50 50
2
3
Output for Sample Input 2
-1
Sample Input 3
4 3
3 10 28 43
40 18 2
26 7 11
Output for Sample Input 3
5

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)
#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)
#define vl vector<ll>
#define vvl vector<vl>
#define vvvl vector<vvl>
#define inf 1e18




int main() {
    ll n,k;
	cin>>n>>k;
	vl a(n),r(k),b(k);
	rep(i,n)cin>>a[i];
	rep(i,k)cin>>r[i];
	rep(i,k)cin>>b[i];

	sort(a.rbegin(),a.rend());
	sort(r.rbegin(),r.rend());
	sort(b.rbegin(),b.rend());
	

	vvvl dp(n+1,vvl(k+1,vl(k*50+1,inf)));
	dp[0][0][0]=0;

	ll redsum=0;
	ll blacksum=0;

	rep(i,k){
		redsum+=r[i];
		blacksum+=b[i];
	}
	ll sum=0;
	ll ans=inf;

	rep(i,n){
		rep(j,k+1){
			rep(e,k*50+1){
				if(dp[i][j][e]==inf)continue;
				ll useblack=i-j;
				if(useblack>k)continue;

				//赤の遷移
				if(j!=k){
					ll cost=a[i]-r[j];
					cost=max(0LL,cost);
					dp[i+1][j+1][e+a[i]]=min(dp[i+1][j+1][e+a[i]],dp[i][j][e]+cost);
				}
				//黒の遷移
				if(useblack!=k){
					ll cost=a[i]-b[useblack];
					cost=max(0LL,cost);
					dp[i+1][j][e]=min(dp[i+1][j][e],dp[i][j][e]+cost);
				}
			}
		}
		sum+=a[i];
	}
	rep(j,k+1){
		rep(e,k*50+1){
			ll tmp=sum-e;
			ll useblack=n-j;
			if(useblack>k)continue;

			if(e<=redsum&&tmp<=blacksum){
				ans=min(ans,dp[n][j][e]);
			}
			
		}
	}
	if(ans!=inf)cout<<ans<<endl;
	else cout<<-1<<endl;

	
}
accuracy: 1
time_ms: 40
memory_kb: 91800
final_rank: 11

code_snippet: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<functional>
using namespace std;
typedef long long LL;
inline long long read()
{
	long long kk=0,f=1;
	char cc=getchar();
	while(cc<'0'||cc>'9'){if(cc=='-')f=-1;cc=getchar();}
	while(cc>='0'&&cc<='9'){kk=(kk<<1)+(kk<<3)+cc-'0';cc=getchar();}
	return kk*f;
}
int n,k,asd;
#define INF 0x3f3f3f3f3f3f3f3fll
LL a[166],r[166],b[166];
LL sa[166],sr[166],sb[166];
LL dp[2][166][4110];
bool cmp(LL a,LL b){return a>b;}
int main()
{
	n=read();k=read();asd=0;
	for(int i=1;i<=n;++i){a[i]=read();}
	for(int i=1;i<=k;++i){r[i]=read();}
	for(int i=1;i<=k;++i){b[i]=read();}
	sort(1+a,a+1+n,cmp);
	sort(1+r,r+1+k,cmp);
	sort(1+b,b+1+k,cmp);
	memset(dp,0x3f3f,sizeof(dp));
	for(int i=1;i<=n;++i)sa[i]=sa[i-1]+a[i];
	for(int i=1;i<=k;++i)sr[i]=sr[i-1]+r[i];
	for(int i=1;i<=k;++i)sb[i]=sb[i-1]+b[i];
	
	dp[0][0][0]=0;
	for(int i=1;i<=n;++i)
	{
		int now=i&1,per=now^1;
		memset(dp[now],0x3f3f,sizeof(dp[now]));
		for(int j=0;j<=min(k,i);++j)
		{
			
			for(int kc=0;kc<=3010;++kc)
			{
				if(kc+a[i]<=sr[k])
				{
					dp[now][j+1][kc+a[i]]=min(dp[now][j+1][kc+a[i]],dp[per][j][kc]+max(0ll,a[i]-r[j+1]));
				}
				if(sa[i-1]-kc+a[i]<=sb[k])
				{
					dp[now][j][kc]=min(dp[now][j][kc],dp[per][j][kc]+max(0ll,a[i]-b[i-j]));
				}
			}
		}
	}
		
		
	LL ans=INF;
	int now=n&1;
	for(int i=0;i<=k;++i)
	{
		for(int kc=0;kc<=3010;++kc)
		{
			ans=min(ans,dp[now][i][kc]);
		}
	}
	if(ans==INF)printf("-1\n");
	else printf("%d\n",ans);
}
accuracy: 1
time_ms: 10
memory_kb: 14236
final_rank: 3

code_snippet: #line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
    int n, k; cin >> n >> k;
    vector a(n, 0);
    for(int i = 0; i < n; i++) cin >> a[i];
    ranges::sort(a | views::reverse);
    vector r(k, 0), b(k, 0);
    for(int i = 0; i < k; i++) cin >> r[i];
    for(int i = 0; i < k; i++) cin >> b[i];
    ranges::sort(r | views::reverse);
    ranges::sort(b | views::reverse);
    constexpr int MAX_SUMR = 60*50;
    constexpr int INF = 1<<30;
    vector dp(k+1, vector(MAX_SUMR+1, INF));
    dp[0][0] = 0;
    auto chmin = [&](int& a, int b) { return a > b && (a = b, true); };
    for(int i = 0; i < n; i++) {
        vector ndp(k+1, vector(MAX_SUMR+1, INF));
        for(int j = 0; j <= k; j++) for(int sumr = 0; sumr <= MAX_SUMR; sumr++) {
            if(dp[j][sumr] == INF) continue;
            if(j+1 <= k) chmin(ndp[j+1][sumr+a[i]], dp[j][sumr] + max(0, a[i]-r[j]));
            if((i-j)+1 <= k) chmin(ndp[j][sumr], dp[j][sumr] + max(0, a[i]-b[i-j]));
        }
        dp.swap(ndp);
    }
    int ans = INF;
    for(int j = 0; j <= k; j++) {
        for(int sumr = 0; sumr <= MAX_SUMR; sumr++) {
            if(dp[j][sumr] == INF) continue;
            int sumb = accumulate(a.begin(), a.end(), 0) - sumr;
            if(sumr <= accumulate(r.begin(), r.end(), 0) && sumb <= accumulate(b.begin(), b.end(), 0)) {
                chmin(ans, dp[j][sumr]);
            }
        }
    }
    cout << (ans == INF ? -1 : ans) << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 4840
final_rank: 2

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<int> X(N), A(K), B(K);
    rep(i,0,N) cin >> X[i];
    rep(i,0,K) cin >> A[i];
    rep(i,0,K) cin >> B[i];
    sort(ALL(X)), reverse(ALL(X));
    sort(ALL(A)), reverse(ALL(A));
    sort(ALL(B)), reverse(ALL(B));
    int ASUM = 0, BSUM = 0;
    rep(i,0,K) ASUM += A[i], BSUM += B[i];
    int MAXS = 3000;
    vector DP(K+1, vector<int>(MAXS+1, inf));
    DP[0][0] = 0;
    int CurSUM = 0;
    rep(i,0,N) {
        vector NewDP(K+1, vector<int>(MAXS+1, inf));
        rep(j,0,K+1) {
            rep(k,0,MAXS+1) {
                if (DP[j][k] == inf) continue;
                int nj = i-j, nk = CurSUM - k;
                if (j + 1 <= K && nj <= K && k + X[i] <= ASUM && nk <= BSUM) chmin(NewDP[j+1][k+X[i]], DP[j][k] + max(0, X[i]-A[j]));
                if (j <= K && nj + 1 <= K && k <= ASUM && nk + X[i] <= BSUM) chmin(NewDP[j][k], DP[j][k] + max(0, X[i]-B[nj]));
            }
        }
        swap(DP, NewDP);
        CurSUM += X[i];
    }
    int ANS = inf;
    rep(i,0,K+1) rep(j,0,MAXS+1) chmin(ANS, DP[i][j]);
    cout << (ANS == inf ? -1 : ANS) << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 4672
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(A) A.begin(),A.end()

int main() {
    
    ll N,K,RS=0,BS=0,AS=0;
    cin>>N>>K;
    vll A(N),R(K),B(K);
    for(auto &a:A)cin>>a;
    for(auto &a:R){
        cin>>a;
        RS+=a;
    }
    for(auto &a:B){
        cin>>a;
        BS+=a;
    }
    sort(all(A));
    reverse(all(A));
    sort(all(R));
    reverse(all(R));
    sort(all(B));
    reverse(all(B));
    vector<vector<vector<ll>>> DP(N+1,vector<vector<ll>>(K+1,vector<ll>(RS+1,1e18)));
    DP[0][0][0]=0;
    rep(n,N){
        AS+=A[n];
        rep(k,K+1){
            rep(m,RS+1){
                if(k<K&&m+A[n]<=RS){
                    ll cost=max(0ll,A[n]-R[k]);
                    DP[n+1][k+1][m+A[n]]=min(DP[n+1][k+1][m+A[n]],DP[n][k][m]+cost);
                }
                if((n-k)<K&&AS-m<=BS){
                    ll cost=max(0ll,A[n]-B[n-k]);
                    DP[n+1][k][m]=min(DP[n+1][k][m],DP[n][k][m]+cost);
                }
            }
        }
    }
    ll an=1e18;
    rep(k,K+1)rep(m,RS+1)an=min(an,DP[N][k][m]);
    cout<<(an<1e17?an:-1)<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 83164
final_rank: 10

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

template <class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

using ll = long long;

ll N, K;

int main() {
  cin >> N >> K;
  vector<ll> A(N), R(K), B(K);
  for (auto &a : A) cin >> a;
  for (auto &r : R) cin >> r;
  for (auto &b : B) cin >> b;
  sort(A.begin(), A.end(), greater<ll>{});
  sort(R.begin(), R.end(), greater<ll>{});
  sort(B.begin(), B.end(), greater<ll>{});
  
  vector dp(K+1, vector(K+1, map<array<ll, 2>, ll>()));
  // dp[n1][n2][s1][s2] : min cost
  dp[0][0][{0, 0}] = 0;
  for (int n1 = 0; n1 <= K; ++n1) {
    for (int n2 = 0; n2 <= K; ++n2) {
      int i = n1 + n2;
      if (i >= N) continue;
      
      for (auto [key, value] : dp[n1][n2]) {
        auto [s1, s2] = key;      
        // A[i] to red
        if (n1 < K) {
          ll diff = R[n1] - A[i];
          ll nv = value + max(-diff, 0LL);
          if (dp[n1 + 1][n2].find({s1 + diff, s2}) == dp[n1 + 1][n2].end()) {
            dp[n1 + 1][n2][{s1 + diff, s2}] = nv;
          }
          else {
            chmin(dp[n1 + 1][n2][{s1 + diff, s2}], nv);
          }
        }
        // A[i] to black
        if (n2 < K) {
          ll diff = B[n2] - A[i];
          ll nv = value + max(-diff, 0LL);
          if (dp[n1][n2 + 1].find({s1, s2 + diff}) == dp[n1][n2 + 1].end()) {
            dp[n1][n2 + 1][{s1, s2 + diff}] = nv;
          }
          else {
            chmin(dp[n1][n2 + 1][{s1, s2 + diff}], nv);
          }
        }
        
      }
      
    }
  }
  
  const ll INF = 1e18;
  ll res = INF;
  for (int n1 = 0; n1 <= K; ++n1) {
    int n2 = N - n1;
    if (n2 < 0 || n2 > K) continue;
    
    for (auto [key, value] : dp[n1][n2]) {
      auto [s1, s2] = key;
      
      ll t1 = 0, t2 = 0;
      for (int n = n1; n < K; ++n) t1 += R[n];
      for (int n = n2; n < K; ++n) t2 += B[n];
      
      if (s1 + t1 >= 0 && s2 + t2 >= 0) chmin(res, value);
    }
  }
  
  if (res == INF) res = -1;
  cout << res << endl;
  
  
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 35072
final_rank: 12

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

template <class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

using ll = long long;

ll N, K;

int main() {
  cin >> N >> K;
  vector<ll> A(N), R(K), B(K);
  for (auto &a : A) cin >> a;
  for (auto &r : R) cin >> r;
  for (auto &b : B) cin >> b;
  sort(A.begin(), A.end(), greater<ll>{});
  sort(R.begin(), R.end(), greater<ll>{});
  sort(B.begin(), B.end(), greater<ll>{});
  
  vector dp(K+1, vector(K+1, map<array<ll, 2>, ll>()));
  // dp[n1][n2][s1][s2] : min cost
  dp[0][0][{0, 0}] = 0;
  for (int n1 = 0; n1 <= K; ++n1) {
    for (int n2 = 0; n2 <= K; ++n2) {
      int i = n1 + n2;
      if (i >= N) continue;
      
      for (auto [key, value] : dp[n1][n2]) {
        auto [s1, s2] = key;      
        // A[i] to red
        if (n1 < K) {
          ll diff = R[n1] - A[i];
          ll nv = value + max(-diff, 0LL);
          if (dp[n1 + 1][n2].find({s1 + diff, s2}) == dp[n1 + 1][n2].end()) {
            dp[n1 + 1][n2][{s1 + diff, s2}] = nv;
          }
          else {
            chmin(dp[n1 + 1][n2][{s1 + diff, s2}], nv);
          }
        }
        // A[i] to black
        if (n2 < K) {
          ll diff = B[n2] - A[i];
          ll nv = value + max(-diff, 0LL);
          if (dp[n1][n2 + 1].find({s1, s2 + diff}) == dp[n1][n2 + 1].end()) {
            dp[n1][n2 + 1][{s1, s2 + diff}] = nv;
          }
          else {
            chmin(dp[n1][n2 + 1][{s1, s2 + diff}], nv);
          }
        }
        
      }
      
    }
  }
  
  const ll INF = 1e18;
  ll res = INF;
  for (int n1 = 0; n1 <= K; ++n1) {
    int n2 = N - n1;
    if (n2 < 0 || n2 > K) continue;
    
    for (auto [key, value] : dp[n1][n2]) {
      auto [s1, s2] = key;
      if (s1 >= 0 && s2 >= 0) chmin(res, value);
    }
  }
  
  if (res == INF) res = -1;
  cout << res << endl;
  
  
  return 0;
}
accuracy: 0.72
time_ms: 80
memory_kb: 35072
final_rank: 15

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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
#define INFS (1LL<<28)
#define DEKAI 1000000007
//#define INF 1000000007
//#define MOD 1000000007
#define lp(i,n) for(int i=0;i<(long long)n;i++)
#define lps(i,n) for(int i=1;i<=n;i++)
#define all(c) begin(c), end(c)

//#define int long long

namespace {
#define __DECLARE__(C)    \
	    template <typename T> \
	std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
	    template <typename T, typename U> \
	std::ostream &operator<<(std::ostream &, const C<T, U> &);

    __DECLARE__(std::vector)
    __DECLARE__(std::deque)
    __DECLARE__(std::set)
    __DECLARE__(std::stack)
    __DECLARE__(std::queue)
    __DECLARE__(std::priority_queue)
    __DECLARE__(std::unordered_set)
    __DECLAREM__(std::map)
    __DECLAREM__(std::unordered_map)

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
    template <typename... T>
    std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

    template <typename Tuple, std::size_t N>
    struct __TuplePrinter__ {
        static void print(std::ostream &os, const Tuple &t) {
            __TuplePrinter__<Tuple, N - 1>::print(os, t);
            os << ", " << std::get<N - 1>(t);
        }
    };

    template <typename Tuple>
    struct __TuplePrinter__<Tuple, 1> {
        static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
    };

    template <typename... T>
    std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
        os << '(';
        __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
        os << ')';
        return os;
    }

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
        return os << '(' << v.first << ", " << v.second << ')';
    }

#define __INNER__                             \
	os << '[';                                \
	for (auto it = begin(c); it != end(c);) { \
		os << *it;                            \
		os << (++it != end(c) ? ", " : "");   \
	}                                         \
	return os << ']';

    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
        __INNER__
    }

#define __DEFINE__(C)                                           \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		__INNER__                                               \
	}

#define __DEFINEM__(C)                                             \
	    template <typename T, typename U>                              \
	std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
		__INNER__                                                  \
	}

#define __DEFINEW__(C, M1, M2)                                  \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		std::deque<T> v;                                        \
		for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
			return os << v;                                         \
	}

    __DEFINE__(std::vector)
    __DEFINE__(std::deque)
    __DEFINE__(std::set)
    __DEFINEW__(std::stack, push_front, top)
    __DEFINEW__(std::queue, push_back, front)
    __DEFINEW__(std::priority_queue, push_front, top)
    __DEFINE__(std::unordered_set)
    __DEFINEM__(std::map)
    __DEFINEM__(std::unordered_map)
}

#define pii pair<int,int>
#define ll long long
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// modint
template <signed M, unsigned T>
struct mod_int {
    constexpr static signed MODULO = M;
    constexpr static unsigned TABLE_SIZE = T;

    signed x;

    mod_int() : x(0) {}

    mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}

    mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}

    mod_int &operator+=(const mod_int &rhs) {
        if ((x += rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator-=(const mod_int &rhs) {
        if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator*=(const mod_int &rhs) {
        x = static_cast<signed>(1LL * x * rhs.x % MODULO);
        return *this;
    }

    mod_int &operator/=(const mod_int &rhs) {
        x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
        return *this;
    }

    mod_int operator-() const { return mod_int(-x); }

    mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }

    mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }

    mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }

    mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }

    bool operator<(const mod_int &rhs) const { return x < rhs.x; }

    mod_int inv() const {
        assert(x != 0);
        if (x <= static_cast<signed>(TABLE_SIZE)) {
            if (_inv[1].x == 0) prepare();
            return _inv[x];
        } else {
            signed a = x, b = MODULO, u = 1, v = 0, t;
            while (b) {
                t = a / b;
                a -= t * b;
                std::swap(a, b);
                u -= t * v;
                std::swap(u, v);
            }
            return mod_int(u);
        }
    }

    mod_int pow(long long t) const {
        assert(!(x == 0 && t == 0));
        mod_int e = *this, res = mod_int(1);
        for (; t; e *= e, t >>= 1)
            if (t & 1) res *= e;
        return res;
    }

    mod_int fact() {
        if (_fact[0].x == 0) prepare();
        return _fact[x];
    }

    mod_int inv_fact() {
        if (_fact[0].x == 0) prepare();
        return _inv_fact[x];
    }

    mod_int choose(mod_int y) {
        assert(y.x <= x);
        return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
    }

    static mod_int _inv[TABLE_SIZE + 1];

    static mod_int _fact[TABLE_SIZE + 1];

    static mod_int _inv_fact[TABLE_SIZE + 1];

    static void prepare() {
        _inv[1] = 1;
        for (int i = 2; i <= (int)TABLE_SIZE; ++i) {
            _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
        }
        _fact[0] = 1;
        for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
            _fact[i] = _fact[i - 1] * int(i);
        }
        _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
        for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) {
            _inv_fact[i] = _inv_fact[i + 1] * (i + 1);
        }
    }
};

template <int M, unsigned F>
std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) {
    return os << rhs.x;
}

template <int M, unsigned F>
std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) {
    long long s;
    is >> s;
    rhs = mod_int<M, F>(s);
    return is;
}

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return lhs.x == rhs.x;
}

template <int M, unsigned F>
bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return !(lhs == rhs);
}

const int MF = 1000010;
const int MOD = 998244353;

using mint = mod_int<MOD, MF>;

mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); }

mint fact(int n) { return mint(n).fact(); }

mint inv_fact(int n) { return mint(n).inv_fact(); }
const ll mod = 1000000007;
// const int MAX_N = 1024; // 4MB
// nCr % mod

inline ll gcds(ll a, ll b) { return b ? gcds(b, a % b) : a; }
inline ll lcms(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename Int, Int MOD, int N>
struct comb_util {
    std::array<Int, N + 1> fc, ifc;

    comb_util() {
        fc[0] = 1;
        for (int i = 1; i <= N; i++) fc[i] = fc[i - 1] * i % MOD;
        ifc[N] = inv(fc[N]);
        for (int i = N - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % MOD;
    }

    Int fact(Int n) { return fc[n]; }

    Int inv_fact(Int n) { return ifc[n]; }

    Int inv(Int n) { return pow(n, MOD - 2); }

    Int pow(Int n, Int a) {
        Int res = 1, exp = n;
        for (; a; a /= 2) {
            if (a & 1) res = res * exp % MOD;
            exp = exp * exp % MOD;
        }
        return res;
    }

    Int perm(Int n, Int r) {
        if (r < 0 || n < r)
            return 0;
        else
            return fc[n] * ifc[n - r] % MOD;
    }

    Int binom(Int n, Int r) {
        if (n < 0 || r < 0 || n < r) return 0;
        return fc[n] * ifc[r] % MOD * ifc[n - r] % MOD;
    }

    Int homo(Int n, Int r) {
        if (n < 0 || r < 0) return 0;
        return r == 0 ? 1 : binom(n + r - 1, r);
    }
};

using comb = comb_util<long long, 1000000007, 3000000>;

#define RK 200000000000
#define LK 300000000000
#define PL 400000000000
#define MI 500000000000
#define KA 600000000000
#define PI acos(-1)
#define int long long

int dp[70][70][5000];

signed main(){
    int n,kk;
    cin>>n>>kk;
    vector<int> v(n);
    int total=0;
    lp(i,n){
        cin>>v[i];
        total+=v[i];
    }
    vector<int> r(kk), b(kk);
    int rsum=0,bsum=0;
    lp(i,kk){
        cin>>r[i];
        rsum+=r[i];
    }
    lp(i,kk){
        cin>>b[i];
        bsum+=b[i];
    }
    sort(all(r));
    sort(all(b));
    reverse(all(r));
    reverse(all(b));
    sort(all(v));
    reverse(all(v));

    //dp[a問目まで][Rをb種類割り振って][RにC人振った]
    lp(i,70)lp(j,70)lp(k,5000)dp[i][j][k]=INFS;
    dp[0][0][0]=0;
    lp(i,n){
        lp(j,70){
            lp(k,5000){
                if(dp[i][j][k]==INFS)continue;
                int rcnt=j;
                int bcnt=i-j;
                if(rcnt!=kk){
                    int rover=max(0ll, v[i]-r[rcnt]);
                    dp[i+1][j+1][k+v[i]]=min(dp[i+1][j+1][k+v[i]], dp[i][j][k]+rover);
                }
                if(bcnt!=kk){
                    int bover=max(0ll, v[i]-b[bcnt]);
                    dp[i+1][j][k]=min(dp[i+1][j][k], dp[i][j][k]+bover);          
                }
            }
        }
    }
    int ans = INFS;
    lp(i,70){
        lp(j,5000){
            if(dp[n][i][j]!=INFS){
                int rtotal=j;
                int btotal=total-j;
                if(rsum<rtotal||bsum<btotal)continue;
                ans=min(ans,dp[n][i][j]);
            }
        }
    }
    if(ans==INFS)ans=-1;
    cout<<ans<<endl;

    
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 206632
final_rank: 13

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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
#define INFS (1LL<<28)
#define DEKAI 1000000007
//#define INF 1000000007
//#define MOD 1000000007
#define lp(i,n) for(int i=0;i<(long long)n;i++)
#define lps(i,n) for(int i=1;i<=n;i++)
#define all(c) begin(c), end(c)

//#define int long long

namespace {
#define __DECLARE__(C)    \
	    template <typename T> \
	std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
	    template <typename T, typename U> \
	std::ostream &operator<<(std::ostream &, const C<T, U> &);

    __DECLARE__(std::vector)
    __DECLARE__(std::deque)
    __DECLARE__(std::set)
    __DECLARE__(std::stack)
    __DECLARE__(std::queue)
    __DECLARE__(std::priority_queue)
    __DECLARE__(std::unordered_set)
    __DECLAREM__(std::map)
    __DECLAREM__(std::unordered_map)

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
    template <typename... T>
    std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

    template <typename Tuple, std::size_t N>
    struct __TuplePrinter__ {
        static void print(std::ostream &os, const Tuple &t) {
            __TuplePrinter__<Tuple, N - 1>::print(os, t);
            os << ", " << std::get<N - 1>(t);
        }
    };

    template <typename Tuple>
    struct __TuplePrinter__<Tuple, 1> {
        static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
    };

    template <typename... T>
    std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
        os << '(';
        __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
        os << ')';
        return os;
    }

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
        return os << '(' << v.first << ", " << v.second << ')';
    }

#define __INNER__                             \
	os << '[';                                \
	for (auto it = begin(c); it != end(c);) { \
		os << *it;                            \
		os << (++it != end(c) ? ", " : "");   \
	}                                         \
	return os << ']';

    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
        __INNER__
    }

#define __DEFINE__(C)                                           \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		__INNER__                                               \
	}

#define __DEFINEM__(C)                                             \
	    template <typename T, typename U>                              \
	std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
		__INNER__                                                  \
	}

#define __DEFINEW__(C, M1, M2)                                  \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		std::deque<T> v;                                        \
		for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
			return os << v;                                         \
	}

    __DEFINE__(std::vector)
    __DEFINE__(std::deque)
    __DEFINE__(std::set)
    __DEFINEW__(std::stack, push_front, top)
    __DEFINEW__(std::queue, push_back, front)
    __DEFINEW__(std::priority_queue, push_front, top)
    __DEFINE__(std::unordered_set)
    __DEFINEM__(std::map)
    __DEFINEM__(std::unordered_map)
}

#define pii pair<int,int>
#define ll long long
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// modint
template <signed M, unsigned T>
struct mod_int {
    constexpr static signed MODULO = M;
    constexpr static unsigned TABLE_SIZE = T;

    signed x;

    mod_int() : x(0) {}

    mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}

    mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}

    mod_int &operator+=(const mod_int &rhs) {
        if ((x += rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator-=(const mod_int &rhs) {
        if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator*=(const mod_int &rhs) {
        x = static_cast<signed>(1LL * x * rhs.x % MODULO);
        return *this;
    }

    mod_int &operator/=(const mod_int &rhs) {
        x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
        return *this;
    }

    mod_int operator-() const { return mod_int(-x); }

    mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }

    mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }

    mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }

    mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }

    bool operator<(const mod_int &rhs) const { return x < rhs.x; }

    mod_int inv() const {
        assert(x != 0);
        if (x <= static_cast<signed>(TABLE_SIZE)) {
            if (_inv[1].x == 0) prepare();
            return _inv[x];
        } else {
            signed a = x, b = MODULO, u = 1, v = 0, t;
            while (b) {
                t = a / b;
                a -= t * b;
                std::swap(a, b);
                u -= t * v;
                std::swap(u, v);
            }
            return mod_int(u);
        }
    }

    mod_int pow(long long t) const {
        assert(!(x == 0 && t == 0));
        mod_int e = *this, res = mod_int(1);
        for (; t; e *= e, t >>= 1)
            if (t & 1) res *= e;
        return res;
    }

    mod_int fact() {
        if (_fact[0].x == 0) prepare();
        return _fact[x];
    }

    mod_int inv_fact() {
        if (_fact[0].x == 0) prepare();
        return _inv_fact[x];
    }

    mod_int choose(mod_int y) {
        assert(y.x <= x);
        return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
    }

    static mod_int _inv[TABLE_SIZE + 1];

    static mod_int _fact[TABLE_SIZE + 1];

    static mod_int _inv_fact[TABLE_SIZE + 1];

    static void prepare() {
        _inv[1] = 1;
        for (int i = 2; i <= (int)TABLE_SIZE; ++i) {
            _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
        }
        _fact[0] = 1;
        for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
            _fact[i] = _fact[i - 1] * int(i);
        }
        _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
        for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) {
            _inv_fact[i] = _inv_fact[i + 1] * (i + 1);
        }
    }
};

template <int M, unsigned F>
std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) {
    return os << rhs.x;
}

template <int M, unsigned F>
std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) {
    long long s;
    is >> s;
    rhs = mod_int<M, F>(s);
    return is;
}

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return lhs.x == rhs.x;
}

template <int M, unsigned F>
bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return !(lhs == rhs);
}

const int MF = 1000010;
const int MOD = 998244353;

using mint = mod_int<MOD, MF>;

mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); }

mint fact(int n) { return mint(n).fact(); }

mint inv_fact(int n) { return mint(n).inv_fact(); }
const ll mod = 1000000007;
// const int MAX_N = 1024; // 4MB
// nCr % mod

inline ll gcds(ll a, ll b) { return b ? gcds(b, a % b) : a; }
inline ll lcms(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename Int, Int MOD, int N>
struct comb_util {
    std::array<Int, N + 1> fc, ifc;

    comb_util() {
        fc[0] = 1;
        for (int i = 1; i <= N; i++) fc[i] = fc[i - 1] * i % MOD;
        ifc[N] = inv(fc[N]);
        for (int i = N - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % MOD;
    }

    Int fact(Int n) { return fc[n]; }

    Int inv_fact(Int n) { return ifc[n]; }

    Int inv(Int n) { return pow(n, MOD - 2); }

    Int pow(Int n, Int a) {
        Int res = 1, exp = n;
        for (; a; a /= 2) {
            if (a & 1) res = res * exp % MOD;
            exp = exp * exp % MOD;
        }
        return res;
    }

    Int perm(Int n, Int r) {
        if (r < 0 || n < r)
            return 0;
        else
            return fc[n] * ifc[n - r] % MOD;
    }

    Int binom(Int n, Int r) {
        if (n < 0 || r < 0 || n < r) return 0;
        return fc[n] * ifc[r] % MOD * ifc[n - r] % MOD;
    }

    Int homo(Int n, Int r) {
        if (n < 0 || r < 0) return 0;
        return r == 0 ? 1 : binom(n + r - 1, r);
    }
};

using comb = comb_util<long long, 1000000007, 3000000>;

#define RK 200000000000
#define LK 300000000000
#define PL 400000000000
#define MI 500000000000
#define KA 600000000000
#define PI acos(-1)
#define int long long

int dp[70][70][5000];

signed main(){
    int n,kk;
    cin>>n>>kk;
    vector<int> v(n);
    int total=0;
    lp(i,n){
        cin>>v[i];
        total+=v[i];
    }
    vector<int> r(kk), b(kk);
    int rsum=0,bsum=0;
    lp(i,kk){
        cin>>r[i];
        rsum+=r[i];
    }
    lp(i,kk){
        cin>>b[i];
        bsum+=b[i];
    }
    sort(all(r));
    sort(all(b));
    reverse(all(r));
    reverse(all(b));
    sort(all(v));
    reverse(all(v));

    //dp[a問目まで][Rをb種類割り振って][RにC人振った]
    lp(i,70)lp(j,70)lp(k,5000)dp[i][j][k]=INFS;
    dp[0][0][0]=0;
    lp(i,n){
        lp(j,70){
            lp(k,5000){
                if(dp[i][j][k]==INFS)continue;
                int rcnt=j;
                int bcnt=i-j;
                if(rcnt!=k-1){
                    int rover=max(0ll, v[i]-r[rcnt]);
                    dp[i+1][j+1][k+v[i]]=min(dp[i+1][j+1][k+v[i]], dp[i][j][k]+rover);
                }
                if(bcnt!=k-1){
                    int bover=max(0ll, v[i]-b[bcnt]);
                    dp[i+1][j][k]=min(dp[i+1][j][k], dp[i][j][k]+bover);          
                }
            }
        }
    }
    int ans = INFS;
    lp(i,70){
        lp(j,5000){
            if(dp[n][i][j]!=INFS){
                int rtotal=j;
                int btotal=total-j;
                if(rsum<rtotal||bsum<btotal)continue;
                ans=min(ans,dp[n][i][j]);
            }
        }
    }
    if(ans==INFS)ans=-1;
    cout<<ans<<endl;

    
    return 0;
}
accuracy: 0.58
time_ms: 30
memory_kb: 206596
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define N 65
using namespace std;
int n,K,ans,a[N],r[N],b[N],s0[N],s1[N],s2[N],dp[N][N][N*N];
int main() 
{
	scanf("%d%d",&n,&K);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&r[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&b[i]);
	sort(a+1,a+n+1,greater<int>());
	sort(r+1,r+K+1,greater<int>());
	sort(b+1,b+K+1,greater<int>());
	for (int i=1;i<=n;i++)
		s0[i]=s0[i-1]+a[i];
	for (int i=1;i<=K;i++)
	{
		s1[i]=s1[i-1]+r[i];
		s2[i]=s2[i-1]+b[i];
	}
	/*if (s0[n]>s1[K]+s2[K])
	{
		puts("-1");
		return 0;
	}*/
	for (int i=0;i<=K;i++)
		for (int j=0;j<=K;j++)
			for (int k=0;k<=s0[n];k++)
				dp[i][j][k]=-12345679;
	dp[0][0][0]=0;
	ans=-1;
	for (int i=0;i<=K;i++)
		for (int j=0;j<=min(K,n-i);j++)
			for (int k=0;k<=s0[i+j];k++)
			{
				if (i&&k>=a[i+j])
					dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-a[i+j]]+min(a[i+j],r[i]));
				if (j)
					dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]+min(a[i+j],b[j]));
				if (i+j==n&&s1[K]>=k&&s2[K]>=s0[n]-k)
					ans=max(ans,dp[i][j][k]);
			}
	if (s0[n]>s1[K]+s2[K])
		printf("%d\n",-1); else
		printf("%d\n",(ans==-1)?-1:(s0[n]-ans));
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 61740
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#define N 65
using namespace std;
int n,K,ans,a[N],r[N],b[N],s0[N],s1[N],s2[N],dp[N][N][N*N];
int main() 
{
	scanf("%d%d",&n,&K);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&r[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&b[i]);
	sort(a+1,a+n+1,greater<int>());
	sort(r+1,r+K+1,greater<int>());
	sort(b+1,b+K+1,greater<int>());
	for (int i=1;i<=n;i++)
		s0[i]=s0[i-1]+a[i];
	for (int i=1;i<=K;i++)
	{
		s1[i]=s1[i-1]+r[i];
		s2[i]=s2[i-1]+b[i];
	}
	/*if (s0[n]>s1[K]+s2[K])
	{
		puts("-1");
		return 0;
	}*/
	for (int i=0;i<=K;i++)
		for (int j=0;j<=K;j++)
			for (int k=0;k<=s0[n];k++)
				dp[i][j][k]=-12345679;
	dp[0][0][0]=0;
	ans=-1;
	for (int i=0;i<=K;i++)
		for (int j=0;j<=min(K,n-i);j++)
			for (int k=0;k<=s0[i+j];k++)
			{
				if (i&&k>=a[i+j])
					dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-a[i+j]]+min(a[i+j],r[i]));
				if (j)
					dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]+min(a[i+j],b[j]));
				if (i+j==n&&s1[K]>=k&&s2[K]>=s0[n]-k)
				{
					ans=max(ans,dp[i][j][k]);
					if (s0[n]>s1[K]+s2[K])
					{
						ans=-1;
						break;
					}
				}
			}
	printf("%d\n",(ans==-1)?-1:(s0[n]-ans));
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 61744
final_rank: 8

code_snippet: #include<bits/stdc++.h>
#define N 65
using namespace std;
int n,K,ans,a[N],r[N],b[N],s0[N],s1[N],s2[N],dp[N][N][N*N];
int main() 
{
	scanf("%d%d",&n,&K);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&r[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&b[i]);
	sort(a+1,a+n+1,greater<int>());
	sort(r+1,r+K+1,greater<int>());
	sort(b+1,b+K+1,greater<int>());
	for (int i=1;i<=n;i++)
		s0[i]=s0[i-1]+a[i];
	for (int i=1;i<=K;i++)
	{
		s1[i]=s1[i-1]+r[i];
		s2[i]=s2[i-1]+b[i];
	}
	/*if (s0[n]>s1[K]+s2[K])
	{
		puts("-1");
		return 0;
	}*/
	for (int i=0;i<=K;i++)
		for (int j=0;j<=K;j++)
			for (int k=0;k<=s0[n];k++)
				dp[i][j][k]=-12345679;
	dp[0][0][0]=0;
	ans=-1;
	for (int i=0;i<=K;i++)
		for (int j=0;j<=min(K,n-i);j++)
			for (int k=0;k<=s0[i+j];k++)
			{
				if (i&&k>=a[i+j])
					dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-a[i+j]]+min(a[i+j],r[i]));
				if (j)
					dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]+min(a[i+j],b[j]));
				if (i+j==n&&s1[K]>=k&&s2[K]>=s0[n]-k)
				{
					if (s0[n]<=s1[K]+s2[K])
						ans=max(ans,dp[i][j][k]); else
						ans=-1;
				}
			}
	printf("%d\n",(ans==-1)?-1:(s0[n]-ans));
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 61772
final_rank: 9

code_snippet: #include<bits/stdc++.h>
#define N 65
using namespace std;
int n,K,ans,a[N],r[N],b[N],s0[N],s1[N],s2[N],dp[N][N][N*N];
int main() 
{
	scanf("%d%d",&n,&K);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&r[i]);
	for (int i=1;i<=K;i++)
		scanf("%d",&b[i]);
	sort(a+1,a+n+1,greater<int>());
	sort(r+1,r+K+1,greater<int>());
	sort(b+1,b+K+1,greater<int>());
	for (int i=1;i<=n;i++)
		s0[i]=s0[i-1]+a[i];
	for (int i=1;i<=K;i++)
	{
		s1[i]=s1[i-1]+r[i];
		s2[i]=s2[i-1]+b[i];
	}
	for (int i=0;i<=K;i++)
		for (int j=0;j<=K;j++)
			for (int k=0;k<=s0[n];k++)
				dp[i][j][k]=-12345679;
	dp[0][0][0]=0;
	ans=-1;
	for (int i=0;i<=K;i++)
		for (int j=0;j<=min(K,n-i);j++)
			for (int k=0;k<=s0[i+j];k++)
			{
				if (i&&k>=a[i+j])
					dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-a[i+j]]+min(a[i+j],r[i]));
				if (j)
					dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]+min(a[i+j],b[j]));
				if (i+j==n&&s1[K]>=k&&s2[K]>=s0[n]-k)
					ans=max(ans,dp[i][j][k]);
			}
	if (s0[n]>s1[K]+s2[K])
		printf("-1\n"); else
		printf("%d\n",s0[n]-ans);
	return 0;
}
accuracy: 0.58
time_ms: 10
memory_kb: 45632
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,m,a[109],r[109],b[109],sum[109],sumr,sumb;
bool cmp(int x,int y){return x>y;}
int f[69][69][3009];

//f[i][j][k]表示r匹配到i，b匹配到j，与r匹配的a之和为k 

int main() {
	scanf("%d%d",&n,&m);sum[0]=sumr=sumb=0;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++) scanf("%d",&r[i]);
	for(int i=1;i<=m;i++) scanf("%d",&b[i]);
	sort(a+1,a+n+1,cmp);
	sort(r+1,r+m+1,cmp);sort(b+1,b+m+1,cmp);
	for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
	for(int i=1;i<=m;i++) sumr+=r[i],sumb+=b[i];
	memset(f,-1,sizeof(f));
	f[0][0][0]=0;int ans=-1;
	for(int i=0;i<=m;i++){
		for(int j=0;j<=m;j++){
			if(i+j>n) continue;
			for(int k=0;k<=sum[i+j];k++){
				if(f[i][j][k]==-1) continue;
				if(i+j==n){
					if(sumr>=k&&sumb>=sum[n]-k) ans=max(ans,f[i][j][k]+sumr+sumb-sum[n]);
					continue;
				} 
				if(sumr>=k+a[i+j+1]&&i<m) f[i+1][j][k+a[i+j+1]]=max(f[i+1][j][k+a[i+j+1]],f[i][j][k]+min(a[i+j+1],r[i+1]));
				if(sumb>=sum[i+j+1]-k&&j<m) f[i][j+1][k]=max(f[i][j+1][k],f[i][j][k]+min(a[i+j+1],b[j+1])); 
			}
		}
	}
	printf("%d\n",ans==-1?ans:(sumr+sumb-ans));	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 59236
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,m,a[109],r[109],b[109],sum[109],sumr,sumb;
bool cmp(int x,int y){return x>y;}
int f[69][69][3009];

//f[i][j][k]表示r匹配到i，b匹配到j，与r匹配的a之和为k 

int main() {
	scanf("%d%d",&n,&m);sum[0]=sumr=sumb=0;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++) scanf("%d",&r[i]);
	for(int i=1;i<=m;i++) scanf("%d",&b[i]);
	sort(a+1,a+n+1,cmp);
	sort(r+1,r+m+1,cmp);sort(b+1,b+m+1,cmp);
	for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
	for(int i=1;i<=m;i++) sumr+=r[i],sumb+=b[i];
	memset(f,-1,sizeof(f));
	f[0][0][0]=0;int ans=-1;
	for(int i=0;i<=m;i++){
		for(int j=0;j<=m;j++){
			if(i+j>n) continue;
			for(int k=0;k<=sum[i+j];k++){
				if(i+j==n){
					if(sumr>=k&&sumb>=sum[n]-k) ans=max(ans,f[i][j][k]+sumr+sumb-sum[n]);
					continue;
				} 
				if(f[i][j][k]==-1) continue;
				if(i<m) f[i+1][j][k+a[i+j+1]]=max(f[i+1][j][k+a[i+j+1]],f[i][j][k]+min(a[i+j+1],r[i+1]));
				if(j<m) f[i][j+1][k]=max(f[i][j+1][k],f[i][j][k]+min(a[i+j+1],b[j+1])); 
			}
		}
	}
		
	printf("%d\n",ans==-1?ans:(sumr+sumb-ans));	
	return 0;
}
accuracy: 0.58
time_ms: 10
memory_kb: 59236
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
using namespace std;
const int N=65;
int n,k,dp[N][N][N*50];
int a[N],r[N],b[N],sa[N],sr[N],sb[N];
bool cmp(int a,int b)
{
	return a>b;
}
signed main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=k;i++) scanf("%d",&r[i]);
	for (int i=1;i<=k;i++) scanf("%d",&b[i]);
	sort(a+1,a+1+n,cmp);
	sort(r+1,r+1+k,cmp);
	sort(b+1,b+1+k,cmp);
	for (int i=1;i<=n;i++) sa[i]=sa[i-1]+a[i];
	for (int i=1;i<=k;i++) sr[i]=sr[i-1]+r[i],sb[i]=sb[i-1]+b[i];
	memset(dp,-1,sizeof(dp));
	dp[0][0][0]=0;
	int ans=inf;
	for (int i=0;i<=k;i++)
	{
		for (int j=0;j<=min(k,n-i);j++)
		{
			for (int s=0;s<=sa[i+j];s++)
			{
				if (dp[i][j][s]==-1) continue;
				if (i+j==n && s<=sr[k] && sa[n]-s<=sb[k])
				  ans=min(ans,sa[n]-dp[i][j][s]);
				if (j<min(k,n-i))
				  dp[i][j+1][s]=max(dp[i][j+1][s],dp[i][j][s]+min(b[j+1],a[i+j+1]));
				if (i<k)
				  dp[i+1][j][s+a[i+j+1]]=max(dp[i+1][j][s+a[i+j+1]],dp[i][j][s]+min(r[i+1],a[i+j+1]));
			}
		}
	}
	if (ans==inf) printf("-1\n");
	else printf("%d\n",ans);
}
accuracy: 1
time_ms: 10
memory_kb: 56916
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
using namespace std;
const int N=65;
int n,k,dp[N][N][N*50];
int a[N],r[N],b[N],sa[N],sr[N],sb[N];
bool cmp(int a,int b)
{
	return a>b;
}
signed main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=k;i++) scanf("%d",&r[i]);
	for (int i=1;i<=k;i++) scanf("%d",&b[i]);
	sort(a+1,a+1+n,cmp);
	sort(r+1,r+1+k,cmp);
	sort(b+1,b+1+k,cmp);
	for (int i=1;i<=n;i++) sa[i]=sa[i-1]+a[i];
	for (int i=1;i<=k;i++) sr[i]=sr[i-1]+r[i],sb[i]=sb[i-1]+b[i];
	memset(dp,-1,sizeof(dp));
	dp[0][0][0]=0;
	int ans=inf;
	for (int i=0;i<=k;i++)
	{
		for (int j=0;j<=min(k,n-i);j++)
		{
			for (int s=0;s<=sa[i+j];s++)
			{
				if (dp[i][j][s]==-1) continue;
				if (i+j==n)
				{
					if (s>sr[i] || sa[n]-s>sb[j]) continue;
					ans=min(ans,sa[n]-dp[i][j][s]);
//					printf("%d %d %d %d\n",i,j,s,dp[i][j][s]);
				}
				if (j<min(k,n-i))
				  dp[i][j+1][s]=max(dp[i][j+1][s],dp[i][j][s]+min(b[j+1],a[i+j+1]));
				if (i<k)
				  dp[i+1][j][s+a[i+j+1]]=max(dp[i+1][j][s+a[i+j+1]],dp[i][j][s]+min(r[i+1],a[i+j+1]));
			}
		}
	}
	if (ans==inf) printf("-1\n");
	else printf("%d\n",ans);
}
accuracy: 0.72
time_ms: 10
memory_kb: 56916
final_rank: 14

code_snippet: #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
typedef int ll;
typedef unsigned un;
typedef std::string str;
typedef std::pair<ll,ll> pll;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
void umax(ll& a,ll t){if(t>a)a=t;}
void umin(ll& a,ll t){if(t<a)a=t;}
const ll INF=1ll<<28;
#define MAXN 61
ll f[MAXN][MAXN][3011];
ll suma[MAXN],sumR,sumB;
ll a[MAXN],r[MAXN],b[MAXN];
void decrease(ll* a,ll* b){std::sort(a,b),std::reverse(a,b);}
int main()
{
	ll n=read(),k=read();
	for(ll i=1;i<=n;++i)a[i]=read();decrease(a+1,a+n+1);
	for(ll i=1;i<=k;++i)r[i]=read();decrease(r+1,r+k+1);
	for(ll i=1;i<=k;++i)b[i]=read();decrease(r+1,r+k+1);
	for(ll i=1;i<=n;++i)suma[i]=suma[i-1]+a[i],sumR+=r[i],sumB+=b[i];
	memset(f,0xcf,sizeof f);
	f[0][0][0]=0;
	ll ans=INF;
	for(ll i=0;i<=k;++i)
		for(ll j=0;j<=k&&i+j<=n;++j)
		{
			if(i+j==0)continue;
			for(ll s=0;s<=suma[i+j];++s)
			{
				if(i&&s>=a[i+j])umax(f[i][j][s],f[i-1][j][s-a[i+j]]+min(r[i],a[i+j]));
				if(j)umax(f[i][j][s],f[i][j-1][s]+min(b[j],a[i+j]));
				if(i+j==n)
				{
					if(sumR>=s&&sumB>=suma[n]-s)umin(ans,suma[n]-f[i][j][s]);
				}
			}
		}
	if(ans==INF)ans=-1;
	printf("%d\n",ans);
}
accuracy: 0.1
time_ms: 10
memory_kb: 47044
final_rank: 19

code_snippet: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
    x=0; char c=gc(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
    while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
    if (flag) x=-x;
}
template <class T> T _max(T a,T b){return a>b ? a : b;}
template <class T> T _min(T a,T b){return a<b ? a : b;}
template <class T> bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int N=200005,inf=0x3f3f3f3f;
int n,k;
int a[N],r[N],b[N];
int suma[N],sumb,sumr;
void init()
{
	read(n); read(k);
	for (int i=1;i<=n;++i)
	{
		read(a[i]);
	}
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for (int i=1;i<=k;++i)
	{
		read(r[i]);
	}
	sort(r+1,r+k+1);
	reverse(r+1,r+k+1);
	for (int i=1;i<=k;++i)
	{
		read(b[i]);
	}
	sort(b+1,b+k+1);
	reverse(b+1,b+k+1);
	for (int i=1;i<=n;++i)
	{
		suma[i]=suma[i-1]+a[i];
		sumb+=b[i];
		sumr+=r[i];
	}
}

int dp[62][62][3005];
void solve()
{
	int ans=inf;
	memset(dp,-1,sizeof dp);
	dp[0][0][0]=0;
	//outarr(a,1,n);
	for (int i=0;i<=k;++i)
	{
		for (int j=0;j<=k&&i+j<=n;++j)
		{
			for (int sum=0;sum<=suma[i+j];++sum)
			{
				if (dp[i][j][sum]==-1) continue;
				//out(i); out(j); out(sum); outln(dp[i][j][sum]);
				if (n==i+j)
				{
					if (sumr>=sum&&sumb>=suma[n]-sum)
					{
						//out(i); out(j); out(sum); outln(suma[n]-sum);
						checkmin(ans,suma[n]-dp[i][j][sum]);
					}
					continue;
				}
				if (i<k)
				{
					int new_sum=sum+a[i+j+1];
					checkmax(dp[i+1][j][new_sum],dp[i][j][sum]+_min(r[i+1],a[i+j+1]));
				}
				if (j<k)
				{
					int new_sum=sum;
					checkmax(dp[i][j+1][new_sum],dp[i][j][sum]+_min(b[j+1],a[i+j+1]));
				}
			}
		}
	}
	if (ans==inf) ans=-1;
	printf("%d\n",ans);
}

#undef int
int main()
{
	init();
	solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 48416
final_rank: 4

code_snippet: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
    x=0; char c=gc(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
    while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
    if (flag) x=-x;
}
template <class T> T _max(T a,T b){return a>b ? a : b;}
template <class T> T _min(T a,T b){return a<b ? a : b;}
template <class T> bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int N=200005,inf=0x3f3f3f3f;
int n,k;
int a[N],r[N],b[N];
int suma[N],sumr[N],sumb[N];
void init()
{
	read(n); read(k);
	for (int i=1;i<=n;++i)
	{
		read(a[i]);
	}
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for (int i=1;i<=k;++i)
	{
		read(r[i]);
	}
	sort(r+1,r+k+1);
	reverse(r+1,r+k+1);
	for (int i=1;i<=k;++i)
	{
		read(b[i]);
	}
	sort(b+1,b+k+1);
	reverse(b+1,b+k+1);
	for (int i=1;i<=n;++i)
	{
		suma[i]=suma[i-1]+a[i];
		sumb[i]=sumb[i-1]+b[i];
		sumr[i]=sumr[i-1]+r[i];
	}
}

int dp[62][62][3005];
void solve()
{
	int ans=inf;
	memset(dp,-1,sizeof dp);
	dp[0][0][0]=0;
	//outarr(a,1,n);outarr(r,1,k);outarr(b,1,k);
	for (int i=0;i<=k;++i)
	{
		for (int j=0;j<=k&&i+j<=n;++j)
		{
			for (int sum=0;sum<=suma[i+j];++sum)
			{
				if (dp[i][j][sum]==-1) continue;
				//out(i); out(j); out(sum); outln(dp[i][j][sum]);
				if (n==i+j)
				{
					if (sumr[k]>=sum&&sumb[k]>=suma[n]-sum)
					{
						checkmin(ans,suma[n]-dp[i][j][sum]);
					}
					continue;
				}
				if (i<k)
				{
					int new_sum=sum+a[i+j+1];
					checkmax(dp[i+1][j][new_sum],dp[i][j][sum]+_min(r[i+1],a[i+j+1]));
				}
				if (j<k)
				{
					int new_sum=sum;
					checkmax(dp[i][j+1][new_sum],dp[i][j][sum]+_min(b[j+1],a[i+j+1]));
				}
			}
		}
	}
	if (ans==inf) ans=-1;
	printf("%d\n",ans);
}

#undef int
int main()
{
	init();
	solve();
    return 0;
}
accuracy: 0.08
time_ms: 10
memory_kb: 48408
final_rank: 20