1 solutions

  • 1
    @ 2024-5-27 15:11:14
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 1e6 + 5;
    int a[MAXN];
    int b[MAXN];
    int sum,n,m;
    
    int main(){
    	cin >> n >> m;
    	for (int i=1;i<=n;i++)
    		cin >> a[i];
    	
    	for (int i=1;i<=n-1;i++)	b[i] = a[i+1] - a[i] - 1;
    	sort(b+1,b+n);
    	
    	
    	int ans = a[n] - a[1] + 1;
    	int j = n-1;
    	for (int i=1;i<=m-1;i++){
    		ans -= b[j];
    		j--;
    	}
    		
    	cout << ans<< endl;
    }
    
    • 1

    Information

    ID
    1160
    Time
    1000ms
    Memory
    125MiB
    Difficulty
    2
    Tags
    # Submissions
    53
    Accepted
    33
    Uploaded By