fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const ll INF = 1e15;
  6. const int N = 1e6+10;
  7.  
  8. vector<pair<ll,ll> >v[1001];
  9.  
  10. int n;
  11. ll c,a[N],b[N],p[N];
  12.  
  13. ll dist(ll x,ll y){
  14. return min(max(y,x)-min(y,x),10-max(y,x)+min(y,x));
  15. }
  16. int main()
  17. {
  18. ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  19. cin>>n>>c;
  20. for(int i=1;i<=n;i++)cin>>p[i];
  21. for(int i=1;i<=n;i++)cin>>a[i];
  22. for(int i=1;i<=n;i++){
  23. cin>>b[i];
  24. v[p[i]].push_back({a[i],b[i]});
  25. }
  26. ll ans=0LL;
  27. for(int i=1;i<1001;i++){
  28. if(!v[i].size())continue;
  29. ll mncost=INF;
  30. for(int j=0;j<10;j++){
  31. ll cost=c;
  32. for(int k=0;k<v[i].size();k++){
  33. cost+=dist(v[i][k].second,1LL*j);
  34. }
  35. mncost=min(mncost,cost);
  36. }
  37. ll cost=0LL;
  38. for(int k=0;k<v[i].size();k++)cost+=dist(v[i][k].first,v[i][k].second);
  39. mncost=min(mncost,cost);
  40. ans+=mncost;
  41. }
  42. cout<<ans;
  43.  
  44.  
  45. return 0;
  46. }
  47.  
  48.  
  49. /*
  50.  
  51. 8 3
  52. 6 5 6 5 5 6 1 2
  53. 5 1 5 2 3 5 8 0
  54. 0 1 0 2 3 8 4 8
  55.  
  56. */
  57.  
Success #stdin #stdout 0s 4420KB
stdin
Standard input is empty
stdout
Standard output is empty