![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/00692b37e7075bd89cf9e0578c344869.png)
满分代码实现
#include <map>
#include <set>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
void solve();
};
void Solution::solve() {
vector<int> seq;
seq.push_back(0);
int n = 0, N = 0;
cin >> n >> N;
int tmp = 0;
for (int i = 0; i < n; i++) {
cin >> tmp;
seq.push_back(tmp);
}
int ans = 0, i = 0;
for (; i < n; i++) {
if (seq[i + 1] > N) {
ans += (N - seq[i]) * i;
break;
}
ans += (seq[i + 1] - seq[i]) * i;
}
if (i == n && seq[i] <= N) {
ans += (N - seq[i]) * i;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
Solution solution;
solution.solve();
return 0;
}