Skip to content

Instantly share code, notes, and snippets.

@gvnee
Created April 14, 2023 08:03
Show Gist options
  • Save gvnee/2aadfd9df4dff062dd1bb31b01613b27 to your computer and use it in GitHub Desktop.
Save gvnee/2aadfd9df4dff062dd1bb31b01613b27 to your computer and use it in GitHub Desktop.
best solution for USACO Barn repair
/*
ID: -------
PROG: barn1
LANG: C++
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
ifstream fin ("barn1.in");
ofstream fout ("barn1.out");
int main(){
int m, s, c;
fin>>m>>s>>c;
if(m>c) m = c;
int arr[c];
for(int i = 0;i<c;i++)
fin>>arr[i];
sort(arr, arr+c);
vector<int> gap;
for(int i = 1;i<c;i++)
gap.pb(arr[i] - arr[i-1]);
sort(gap.begin(), gap.end());
ll res = 0;
for(int i = 0;i<=c-1-m;i++)
res+=gap[i];
fout<<res + m<<"\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment