#include #include #include #include using namespace std; const int maxn = 1e5 + 10; struct node { int idx, shortest_path; node() {} node(int _idx, int _shortest_path) { idx = _idx; shortest_path = _shortest_path; } bool operator < (const node & tmp) const { return shortest_path > tmp.shortest_path; } }; int main(){ int n, V; cin >> n >> V; vector portals(n); for(int i = 0; i < n; i++) { cin >> portals[i]; } vector> graph[maxn]; for(int i = 0; i < n; i++) { if(i + 1 < n) { graph[i].push_back(make_pair(i + 1, 1)); } if(i - 1 >= 0) { graph[i].push_back(make_pair(i - 1, 1)); } for(int j = 0; j < n; j++) { if(portals[i] == portals[j]) { graph[i].push_back(make_pair(j, V)); } } } vector visited(n, false); vector dist(n, 2e9); priority_queue pq; pq.push(node(0, 0)); while(!pq.empty()) { node c = pq.top(); pq.pop(); if(visited[c.idx]) { continue; } visited[c.idx] = true; for(int i = 0; i < (int) graph[c.idx].size(); i++) { int neighbour = graph[c.idx][i].first; int weight = graph[c.idx][i].second; if(!visited[neighbour] and c.shortest_path + weight < dist[neighbour]) { dist[neighbour] = c.shortest_path + weight; pq.push(node(neighbour, dist[neighbour])); } } } cout << dist[n - 1] << endl; return 0; }