[POI2005]BAN-Bank Notes

#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

const int A = 1e7+10;
const int B = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
  for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  return x * f;
}

int a[210],b[210],val[B],c[B],p[B],cnt,n,m,f[B],g[20007][211];

int main()
{
    n=read();
    int s=n;
    memset(f,63,sizeof(f));
    f[0]=0;
    for (int i=1;i<=n;i++) a[i]=read(),g[a[i]][i]=1;
    for (int i=1;i<=n;i++) b[i]=read();
    
    for (int i=1;i<=n;i++)
    {
        int r=1;
        while (b[i])
        {
            c[++cnt]=a[i]*r; val[cnt]=r; b[i]-=r;r<<=1; p[cnt]=i; 
            if (b[i] <= r)
            {
                c[++cnt]=a[i]*b[i];
                val[cnt]=b[i];
                b[i]=0;
                p[cnt]=i;
            }
        }
    }
    
    m=read();
    for (int i=1;i<=cnt;i++)
        for (int j=m;j>=c[i];j--)
            if(f[j-c[i]]+val[i]<f[j])
            {
                memcpy(g[j],g[j-c[i]],sizeof (g[j-c[i]]));
                f[j]=f[j-c[i]]+val[i];
                g[j][p[i]]=g[j-c[i]][p[i]]+val[i];
            }
    
    printf("%d\n",f[m]);
    //cout << n;
//    for (int i=1;i<=n;i++) 
//    {
//        printf("%d ",g[m][i]);
//    } 
}
posted @ 2021-03-14 20:50  zxsoul  阅读(30)  评论(0编辑  收藏  举报