G-排解忧伤


 1 #include 
 2 using namespace std;
 3 int a[100005];
 4 int main() {
 5     long long n, m, frontier = 0, ans = 0;
 6     cin >> n >> m;
 7     for (int i = 0; i < m; i++)
 8         cin >> a[i];
 9     sort(a, a+m); // 把每个人的心仪座位正序排序
10     for (int i = 0; i < m; i++) {
11         if (a[i] > frontier) { // 更新边界
12             frontier = a[i];
13         } else {
14             ans += frontier-a[i]+1; // 总怒气值增加
15             frontier++;
16             if (frontier > n) { // 怒气爆棚
17                 cout << "-1";
18                 return 0;
19             }
20         }
21     }
22 
23     cout << ans;
24     return 0;
25 }