fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define el '\n'
  4. #define fi first
  5. #define sec second
  6. #define ll long long
  7. #define pb push_back
  8. #define sz(v) (int)(v).size()
  9. #define all(v) (v).begin(),v.end()
  10. using namespace std;
  11.  
  12. const int maxn = (int)5e3;
  13. const int maxm = (int)1e5;
  14.  
  15. int a[maxn + 5], c[maxm + 5];
  16. int n, m;
  17.  
  18. void Input(){
  19. cin >> n >> m;
  20. for(int i = 1; i <= n; i++) cin >> a[i];
  21. for(int i = 1; i <= m; i++) cin >> c[i];
  22. sort(a + 1, a + n + 1);
  23. for(int i = m - 1; i >= 1; i--) c[i] = min(c[i], c[i + 1]);
  24. }
  25.  
  26. int dp[maxn + 5];
  27. //dp[i] kq toi uu nhat khi xet den vi tri i
  28.  
  29. void Solve(){
  30. dp[0] = 0;
  31. for(int i = 1; i <= n; i++) dp[i] = c[a[i] - a[1] + 1];
  32.  
  33. for(int i = 1; i <= n; i++){
  34. dp[i] = 1e9;
  35. for(int j = 1; j <= i; j++){
  36. int w = a[i] - a[j] + 1;
  37. dp[i] = min(dp[i], dp[j - 1] + c[w]);
  38. }
  39. }
  40. cout << dp[n];
  41. }
  42.  
  43. int main(){
  44. ios_base::sync_with_stdio(0);
  45. cin.tie(0);
  46. Input();
  47. Solve();
  48. }
  49.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty