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++: #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template
typedef pair<int, int> pii;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    vector<int> used(n, 0);
    for (int i = 0; i < k; i++) {
        used[i] = 1;
    }
    sort(all(used));
    int ans = 0;
    do{
        int tm = m;
        vector<pii> poteto;
        for(int i = 0; i < n; i++){
            if(used[i] == 1)poteto.push_back(pii(a[i], b[i]));
        }
        sort(all(poteto));
        reverse(all(poteto));
        int tmp = 0;
        for (int i = 0; i < poteto.size() and tm > 0; i++) {
            tmp += poteto[i].first*min(tm, poteto[i].second);
            tm -= poteto[i].second;
        }
        ans = max(ans, tmp + tm);
    }while(next_permutation(all(used)));
    cout << ans << endl;
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,m,k;
P farm[15];

int func(int v,int rest,int used){
	if(v==n || used==k)return rest;
	int res=max(rest,func(v+1,rest,used));
	int add=min(farm[v].second,rest)*farm[v].first;
	return max(res,func(v+1,rest-min(farm[v].second,rest),used+1)+add);
}

int main(void){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<n;i++){
		scanf("%d",&farm[i].first);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&farm[i].second);
	}
	sort(farm,farm+n,greater<P>());
	printf("%d\n",func(0,m,0));
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

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

int idx[4];
ll ans = 0;
void dfs(int num)
{
    if(num==k)
    {
        vector<pair<int,int>> v;
        rep(i,k) v.pb({a[idx[i]],b[idx[i]]});
        sort(all(v));
        reverse(all(v));

        int rem = m;
        ll t = 0;
        rep(i,k)
        {
            int A = v[i].fi, B = v[i].se;

            t += A*min(B,rem);
            rem -= min(B,rem);
        }
        t += rem;

        ans = max(ans,t);
        return;
    }

    rep(i,n)
    {
        bool ok = true;
        rep(j,num) ok &= (idx[j]!=i);

        if(ok)
        {
            idx[num] = i;
            dfs(num+1);
            idx[num] = -1;
        }
    }
}

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

    memset(idx,-1,sizeof(idx));
    dfs(0);
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<ll,ll> P;

int main(void){
	int i,j;

	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N,M,K;
	cin >> N >> M >> K;
	vector<ll>a(N),b(N);
	for(i=0;i<N;i++) cin >> a[i];
	for(i=0;i<N;i++) cin >> b[i];

	vector<P>c(N);
	for(i=0;i<N;i++) c.push_back(P(a[i],b[i]));
	sort(c.begin(),c.end());
	reverse(c.begin(),c.end());

	ll ans=0;
	for(i=0;i<(1<<N);i++){
		ll x=0;
		int m=M,k=K;
		if(__builtin_popcount(i)==K){
			for(j=0;j<N;j++){
				if(i&(1<<j)){
					if(m>=c[j].second){
						x+=(c[j].first)*(c[j].second);
					    m-=c[j].second;
						k--;
						if(k==0) break;
					}else{
						x+=(c[j].first)*m;
						m=0;
						break;
					}
			    }
		    }

			ans=max(ans,x+m);
		}
	}

	cout << ans << endl;

	return 0;
}