Submission #956138

#TimeUsernameProblemLanguageResultExecution timeMemory
956138manishjha91Stove (JOI18_stove)C++17
100 / 100
20 ms3364 KiB
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using lld = long double;

// #ifndef ONLINE_JUDGE
// #include "E:\Personal\cpp_codes\debugalgo.h"
// #else
// #define debug(x)
// #endif
// 

void solve(int tc)
{
	ll n,k;
	cin>>n>>k;
	
	

	vector<ll> a(n);
	for(int i=0; i<n; i++)
	{
		cin>>a[i];
	}
	
	sort(a.begin(),a.end());
	
	vector<ll> gap;
	
	for(int i=1; i<n; i++)
	{
		gap.push_back(a[i]-a[i-1]-1);
	}
	
	ll res = n;
	ll req = n;
	sort(gap.begin(),gap.end());
	
	for(int i=0; i<n-k; i++)
	{
		res+=gap[i];
	}
	
	cout<<res<<"\n";
	
}

int main()
{
	ios_base:: sync_with_stdio(0);
	cin.tie(0);
// #ifndef ONLINE_JUDGE
// 	freopen("Error.txt", "w", stderr);
// #endif

	int tt = 1;
	// cin >> tt;
	for(int tc=1; tc<=tt; tc++)
	{
		solve(tc);
	}
}

Compilation message (stderr)

stove.cpp: In function 'void solve(int)':
stove.cpp:38:5: warning: unused variable 'req' [-Wunused-variable]
   38 |  ll req = n;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...