Go: package main

import (
    "bufio"
    "os"
    "fmt"
    "strings"
    "strconv"
    "sort"
    "math"
)

/************** Input *************/
var sc = bufio.NewScanner(os.Stdin)

func ReadLineToInt () []int{
    sc.Scan()
    line := strings.Split(sc.Text(), " ")
    length := len(line)
    var intLine []int
    for i := 0;i < length; i++ {
        n, _ := strconv.Atoi(line[i])
        intLine = append(intLine, n)
    }
    return intLine
}

/************* Pairs **************/
type Pair struct {
    a int
    b int
}

type SortablePairs struct {
    arr []*Pair
    lessfunc []lessFunc
}

type lessFunc func(p *Pair, q *Pair)bool

func (sp SortablePairs) Len() int {
    return len(sp.arr)
}

func (sp SortablePairs) Swap(i ,j int) {
    sp.arr[i], sp.arr[j] = sp.arr[j], sp.arr[i]
}

func (sp SortablePairs) Less(i, j int) bool {
    p, q := sp.arr[i], sp.arr[j]
    for k := 0; k < len(sp.lessfunc); k++ {
        less := sp.lessfunc[k]
        if less(p, q) {
            return true
        } else if less(q, p) {
            return false
        }
    }
    return false
}

/***********  min, max  *************/
func min(a, b int) int {
    return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
    return int(math.Max(float64(a), float64(b)))
}


/************  main  ***************/
func main() {
    nmk := ReadLineToInt()
    n, m, k := nmk[0], nmk[1], nmk[2]
    aSlice := ReadLineToInt()
    bSlice := ReadLineToInt()
    sp := SortablePairs{}
    for i := 0; i < n; i++ {
        sp.arr = append(sp.arr, &Pair{a: aSlice[i], b: bSlice[i]})
    }

    sortFunc := func(p, q *Pair) bool {
        return p.a > q.a
    }

    sp.lessfunc = append(sp.lessfunc, sortFunc)
    sort.Sort(sp)

    var dp [4][10001]int
    for i := 0; i < n; i++ {
        item := sp.arr[i]
        a := item.a
        b := item.b
        for y := k - 1;y >= 0; y-- {
            for x := m - 1; x >= 0; x-- {
                use := min(b, m - x)
                dp[y + 1][x + use] = max(dp[y + 1][x + use], dp[y][x] + use * a)
            }
        }
    }
    var ans int
    for y := 0; y <= k; y++ {
        for x := 0; x <= m; x++ {
            ans = max(ans, dp[y][x] + m - x)
        }
    }
    fmt.Println(ans)
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) begin(a),end(a)
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
bool test = 0;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)1e6 + 5

int bit_count(int N) {
	int cnt = 0;
	while (N) {
		if (N & 1) cnt++;
		N >>= 1;
	}
	return cnt;
}

signed main(void) {
	int i, j, k, l;
	int N, M, K;
	int a[18], b[18];
	cin >> N >> M >> K;
	rep(i, N) cin >> a[i];
	rep(i, N) cin >> b[i];

	int mask;
	int cnt = 0;
	rep(mask, 1 << N) {
		int mask_cnt = 0;
		vector<pair<int,int>> mask_pr;
		if (bit_count(mask) != K) continue;
		rep(i, N) {
			if ((mask >> i) & 1) {
				mask_pr.push_back({ a[i],b[i] });
			}
		}
		int cur_poteto = M;
		sort(ALL(mask_pr));
		for(i=K-1;i>-1;i--) {
			mask_cnt += mask_pr[i].first * min(mask_pr[i].second, cur_poteto);
			cur_poteto -= mask_pr[i].second;
			if (cur_poteto < 0) break;
		}
		mask_cnt += max(cur_poteto, 0);
		if (cnt < mask_cnt) {
			//cerr << "!";
			//rep(i, K) {
			//	cerr << mask_pr[i].first << " ";
			//}
			//dump(mask_cnt);
			cnt = mask_cnt;
		}
	}
	cout << cnt << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
const double PI  = 3.1415926535897932384626433832795028841971;
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n, m, k;
int a[15], b[15];
P c[15];

int main(){
	cin >> n >> m >> k;
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,n) c[i] = P(a[i],b[i]);
    int ans = 0;
    sort(c,c+n);
    rep(i,(1<<n)){
        int fl = 0, ii = i;
        vector<P> tmp;
        rep(j,n){
            if(ii&1){
                fl++;
                tmp.push_back(c[j]);
            }
            ii >>= 1;
        }
        if(fl > k) continue;
        sort(tmp.begin(),tmp.end(),greater<P>());
        int mm = m, cnt = 0;
        rep(j,tmp.size()){
            cnt += tmp[j].first*min(mm,tmp[j].second);
            mm -= tmp[j].second;
            if(mm <= 0) break;
        }
        ans = max(ans,cnt+max(mm,0));
    }
    cout << ans << endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) begin(a),end(a)
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
bool test = 0;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)1e6 + 5

int bit_count(int N) {
	int cnt = 0;
	while (N) {
		if (N & 1) cnt++;
		N >>= 1;
	}
	return cnt;
}

signed main(void) {
	int i, j, k, l;
	int N, M, K;
	int a[18], b[18];
	cin >> N >> M >> K;
	rep(i, N) cin >> a[i];
	rep(i, N) cin >> b[i];

	int mask;
	int cnt = 0;
	rep(mask, 1 << N) {
		int mask_cnt = 0;
		vector<pair<int,int>> mask_pr;
		if (bit_count(mask) != K) continue;
		rep(i, N) {
			if ((mask >> i) & 1) {
				mask_pr.push_back({ a[i],b[i] });
			}
		}
		int cur_poteto = M;
		sort(ALL(mask_pr));
		for(i=K-1;i>-1;i--) {
			mask_cnt += mask_pr[i].first * min(mask_pr[i].second, cur_poteto);
			cur_poteto -= mask_pr[i].second;
			if (cur_poteto < 0) break;
		}
		mask_cnt += max(cur_poteto, 0);
		if (cnt < mask_cnt) {
			//cerr << "!";
			//rep(i, K) {
			//	cerr << mask_pr[i].first << " ";
			//}
			//dump(mask_cnt);
			cnt = mask_cnt;
		}
	}
	cout << cnt << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define ka first
#define	am second
using namespace	std;
int main(){
  int n,m,ma,a[15],b[15],r;
  pair<int,int> li[15];
  cin>>n>>m>>ma;
  for(int i=0;i<n;i++) cin>>a[i];
  for(int i=0;i<n;i++) cin>>b[i];
  for(int i=0;i<n;i++) li[i]=make_pair(a[i],b[i]);
  sort(li,li+n);
  if(ma==3){
    for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) for(int k=j+1;k<n;k++){
          int tmp=0,nokori=m;
          tmp+=li[k].ka*min(li[k].am,nokori);
          nokori-=min(li[k].am,nokori);
          tmp+=li[j].ka*min(li[j].am,nokori);
          nokori-=min(li[j].am,nokori);
          tmp+=li[i].ka*min(li[i].am,nokori);
	  nokori-=min(li[i].am,nokori);
          tmp+=nokori;
          r=max(r,tmp);
        }
  } else if(ma==2){
    for(int j=0;j<n;j++) for(int k=j+1;k<n;k++){
	int tmp=0,nokori=m;
        tmp+=li[k].ka*min(li[k].am,nokori);
        nokori-=min(li[k].am,nokori);
	tmp+=li[j].ka*min(li[j].am,nokori);
	nokori-=min(li[j].am,nokori);
	tmp+=nokori;
	r=max(r,tmp);
      }
  } else {
    for(int k=0;k<n;k++){
      int tmp=0,nokori=m;
      tmp+=li[k].ka*min(li[k].am,nokori);
      nokori-=min(li[k].am,nokori);
      tmp+=nokori;
      r=max(r,tmp);
    }
  }
  cout<<r<<endl;
  return 0;
}