Source Code
#include <cstring>
#include <vector>
#include <stdio.h>
#include <bitset>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include<sstream>
#include <iomanip>
#include <vector>
#include<utility>
#include <iostream>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll n,m,x; cin >> n>>m;
	vector<ll>v;
	for (int i = 0; i < n; i++) {
		cin >> x;
		v.push_back(x);
	}
	sort(v.begin(), v.end());
	ll ctr = v[0];
	ll c = 0,c1=0;
	for (int i = 1; i < m; i++) {
		if (v[i] == v[i - 1]) {
			c++;
			
		}
		ctr = v[i];
		if (v[i] - v[i - 1] > 1) {
			c1 += ((v[i] - v[i - 1]) - 1);
		}
	}
	c -= c1;
	if (c < 0) { c = 0; }
	cout << ctr+c << "\n";
}
Copy
Cutting Trees makamkh899
GNU G++17
27 ms
1.5 MB
Wrong Answer