Language: C++

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int N, A, B, C, a, b, c, d, e, f, g, h;
const int INF = 1<<29;

int dp[1010][1010];

int min_mp(int x, int l1, int l2, int l3, int l4, int l5, int l6, int l7, int l8){
//dpテーブルの初期化
  for (int i = 0; i < 1010; i++) for (int j = 0; j < 1010; j++) dp[i][j] = INF;
  for (int i = 0; i < 1010; i++){
    dp[i][l1] = 0;
    dp[i][l2] = 0;
    dp[i][l3] = 0;
    dp[i][l4] = 0;
    dp[i][l5] = 0;
    dp[i][l6] = 0;
    dp[i][l7] = 0;
    dp[i][l8] = 0;
  }
// dp操作
  if (x == l1 || x == l2 || x == l3 || x == l4 || x == l5 || x == l6 || x == l7 || x == l8){
    return 0;
  } else {
    for (int i = 0; i < 1010; i++){
      for (int j = 0; j < 1010; j++){
        if (j != x){
          if(j >= l8) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l8]+10, dp[i][j-l7]+10, dp[i][j-l6]+10, dp[i][j-l5]+10, dp[i][j-l4]+10, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l7 && j < l8) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l7]+10, dp[i][j-l6]+10, dp[i][j-l5]+10, dp[i][j-l4]+10, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l6 && j < l7) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l6]+10, dp[i][j-l5]+10, dp[i][j-l4]+10, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l5 && j < l6) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l5]+10, dp[i][j-l4]+10, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l4 && j < l5) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l4]+10, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l3 && j < l4) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l3]+10, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l2 && j < l3) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l2]+10, dp[i][j-l1]+10});
          else if (j >= l1 && j < l2) dp[i+1][j] = min({dp[i][j-1]+1, dp[i][j+1]+1, dp[i][j-l1]+10});
          else dp[i+1][j] = min(dp[i][j-1]+1, dp[i][j+1]+1);
        }else{
          dp[i+1][j] = dp[i][j];
        }
      }
    }
    return dp[1009][x];
  }
}

int main(){
  vector<int> vec(8);
  cin >> N >> A >> B >> C;
  for (int i = 0; i < 8; i++) cin >> vec.at(i);
  sort(vec.begin(),vec.end());

  a = vec.at(0);
  b = vec.at(1);
  c = vec.at(2);
  d = vec.at(3);
  e = vec.at(4);
  f = vec.at(5);
  g = vec.at(6);
  h = vec.at(7);

  int A_min = min_mp(A,a,b,c,d,e,f,g,h);
  int B_min = min_mp(B,a,b,c,d,e,f,g,h);
  int C_min = min_mp(C,a,b,c,d,e,f,g,h);

  cout << A_min + B_min + C_min << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:50:3: error: 'vector' was not declared in this scope
   50 |   vector<int> vec(8);
      |   ^~~~~~
a.cc:4:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    3 | #include <algorithm>
  +++ |+#include <vector>
    4 | using namespace std;
a.cc:50:10: error: expected primary-expression before 'int'
   50 |   vector<int> vec(8);
      |          ^~~
a.cc:52:38: error: 'vec' was not declared in this scope
   52 |   for (int i = 0; i < 8; i++) cin >> vec.at(i);
      |                                      ^~~
a.cc:53:8: error: 'vec' was not declared in this scope
   53 |   sort(vec.begin(),vec.end());
      |        ^~~