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<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int n,m,k;
    cin >> n >> m >> k;
    vi a(n),b(n);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    int ans = 0;
    if(k == 1){
        rep(i,n){
            ans = max(ans, a[i]*min(b[i],m) + m - min(b[i],m));
        }
    }else if(k == 2){
        rep(i,n)rep(j,n)if(i != j){
            int t = a[i]*min(b[i],m);
            int left = m - min(b[i],m);
            t += a[j]*min(b[j],left);
            left -= min(b[j], left);
            ans = max(ans, t + left);
        }
    }else if(k == 3){
        rep(i,n)rep(j,n)rep(k,n)if(i != j && i != k && j != k){
            //cout << i << " " << j << " " << k << " " ;
            int t = a[i]*min(b[i],m);
            //cout << min(b[i],m) << " " ;
            int left = m - min(b[i],m);
            t += a[j]*min(b[j],left);
            //cout << min(b[j], left) << " " ;
            left -= min(b[j],left);
            t += a[k]*min(b[k],left);
            left -= min(b[k], left);
            //cout << min(b[k],left) << endl;
            //cout << i << " " << j << " " << k << " " << t << endl;
            ans = max(ans, t + left);
        }
    }
    cout << ans << endl;
}

C++: /*  -*- coding: utf-8 -*-
 *
 * 1588.cc: Potatoes
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 15;
const int MAX_M = 10000;
const int MAX_K = 3;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;

/* global variables */

pii ps[MAX_N];

/* subroutines */

void comb(int u, int k, int n, int bits, vi &cs) {
  if (k <= 0) {
    cs.push_back(bits);
    return;
  }
  if (u >= n) return;

  for (int i = u, bi = 1 << i; i < n; i++, bi <<= 1)
    comb(i + 1, k - 1, n, bits | bi, cs);
}

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  for (int i = 0; i < n; i++) scanf("%d", &ps[i].first);
  for (int i = 0; i < n; i++) scanf("%d", &ps[i].second);
  sort(ps, ps + n);

  vi cs;
  comb(0, k, n, 0, cs);
  //printf("%lu\n", cs.size());

  int maxsum = 0;
  for (vi::iterator vit = cs.begin(); vit != cs.end(); vit++) {
    int &bits = *vit;
    int sum = 0, r = m;
    for (int i = n - 1, bi = 1 << i; i >= 0; i--, bi >>= 1)
      if (bits & bi) {
	int c = min(r, ps[i].second);
	sum += c * ps[i].first;
	r -= c;
      }

    setmax(maxsum, sum + r);
  }

  printf("%d\n", maxsum);
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int N,M,K;
int a[15],b[15];


int main(){
  cin>>N>>M>>K;
  for(int i=0;i<N;i++){
    cin>>a[i];
  }
  for(int i=0;i<N;i++){
    cin>>b[i];
  }

  int ans=M;


  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      if(i==j)continue;
      for(int k=0;k<N;k++){
        if(i==k||j==k)continue;
        int tmp=M;

        int ai=min(tmp,b[i]);
        tmp-=ai;
        if(K>=1){
          int value = tmp + ai*a[i];
          ans=max(ans, value );
        }
        int aj=min(tmp,b[j]);
        tmp-=aj;
        if(K>=2){
          int value = tmp + ai*a[i] + aj*a[j];
          ans=max(ans, value );
        }
        int ak=min(tmp,b[k]);
        tmp-=ak;
        if(K==3){
          int value = tmp + ai*a[i] + aj*a[j] + ak*a[k];
          ans=max(ans, value );
        }

      }
    }
  }
  
  cout<<ans<<endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	vp in(n);
	rep(i,n)cin>>in[i].first;
	rep(i,n)in[i].first*=-1;
	rep(i,n)cin>>in[i].second;
	sort(all(in));
	int N=1<<n,ans=0;
	rep(i,N){
		int co=0;
		rep(j,n)if(i&1<<j)co++;
		if(co==k){
			int out=0;
			int sum=0;
			rep(j,n)if(i&1<<j){
				if(out<m){
					int t=min(in[j].second,m-out);
					out+=t;
					sum+=in[j].first*t;
				}
			}
			ans=max(ans,-sum+m-out);
		}
	}
	cout<<ans<<endl;
}