Submission #650040

#TimeUsernameProblemLanguageResultExecution timeMemory
650040KenparStove (JOI18_stove)C++17
100 / 100
28 ms2776 KiB
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
#define endl '\n'
 
const ll MOD = 998244353;
const ll INF = 1e16;
const int INT_INF = 1e9;
const ll MAX = 1e6+1;

void solve(){
	int n;
	cin>>n;

	int k;
	cin>>k;

	vector<bool> merged(n, false);
	vector<int> arr(n);

	vector<pair<int,int>> dif;

	for(int i = 0; i < n; i++){
		cin>>arr[i];

		if(i != 0){
			dif.push_back({arr[i] - arr[i-1], i-1});
		}
	}

	sort(dif.begin(), dif.end() );

	ll ans = 0;

	for(pair<int,int> ch : dif){
		//cout<<ch.first<<" "<<ch.second<<endl;
	}

	for(int i = 0; i < n-k; i++){
		ans+=dif[i].first;
		merged[dif[i].second] = true;
	}

	for(int i = 0; i < n; i++){
		ans+=!merged[i];
	}

	cout<<ans;
}
 
 
int main()
{
	cin.tie(NULL);
	ios::sync_with_stdio(NULL);
	int t = 1;
 
	//cin >> t;
 
	while(t--){
		solve();
		cout<<endl;
	}
}

Compilation message (stderr)

stove.cpp: In function 'void solve()':
stove.cpp:36:20: warning: variable 'ch' set but not used [-Wunused-but-set-variable]
   36 |  for(pair<int,int> ch : dif){
      |                    ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...