fork(1) download
  1. #include <algorithm>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. #define maxn 5000
  7. #define maxm 100000
  8.  
  9. int main(){
  10.  
  11. int i,j,n,m,p,cost[maxm+1],a[maxn];
  12. long long int dp[maxn],opt,c;
  13.  
  14. scanf("%d%d",&n,&m);
  15. for(i=0;i<n;i++)scanf("%d",&a[i]);
  16. sort(a,a+n);
  17.  
  18. for(i=1;i<=m;i++)scanf("%d",&cost[i]);
  19. for(i=m-1;i>0;i--)
  20. if(cost[i]>cost[i+1])cost[i]=cost[i+1];
  21.  
  22. for(i=0;i<n;i++){
  23. p=a[i]+1;
  24. opt=cost[p-a[0]];
  25. for(j=0;j<i;j++){
  26. c=dp[j]+cost[p-a[j+1]];
  27. if(c<opt)opt=c;
  28. }
  29. dp[i]=opt;
  30. }
  31.  
  32. printf("%lld\n",dp[n-1]);
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3472KB
stdin
6 12
1 2 11 8 4 12 2
3 4 4 8 9 15 16 17 18 19 19
stdout
9