USACO 1.3 Barn Repair
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* ID: aznfy1 PROG: barn1 LANG: C++ */ #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int space[200]; int length; int tmp1,tmp2; int tmp[400]; int main() { freopen("barn1.in","r",stdin); freopen("barn1.out","w",stdout); int BoardNum,HomeNum,CowNum; while(cin>>BoardNum>>HomeNum>>CowNum) { int k=0;tmp1=0; tmp2=0; length=CowNum; for(int i=0;i<CowNum;i++) { cin>>tmp[i]; } sort(tmp,tmp+CowNum); for(int i=0;i<CowNum;i++) { tmp1=tmp[i]; if(tmp2!=0&&(tmp1-tmp2>1)) { space[k]=tmp1-tmp2-1; k++; } tmp2=tmp1; } sort(space,space+k); for(int i=0;i<k+1-BoardNum;i++) { length+=space[i]; } cout<<length<<endl; } return 0; }