fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define Thanh_Cong cout<<" You can call me: coder hihi ";
  4.  
  5. using namespace std;
  6.  
  7. #define pub push_back
  8.  
  9. #define LL long long
  10. #define L long
  11. #define ULL unsigned long long
  12.  
  13. const long long M= 1e9+7;
  14. const double pi = acos(-1);
  15. const long limit=1e5+5;
  16.  
  17. int n,d=0,k,res=0,check=0;
  18. long a[limit];
  19. vector<int> u;
  20.  
  21. /*int BS(long x)
  22. {
  23. int l=0,r=n-1,mi,res=-1;
  24. while(l<=r)
  25. {
  26. mi=(l+r)/2;
  27. if(abs(x-a[mi])<=5)
  28. {
  29. res=mi;
  30. l=mi+1;
  31. }
  32. else r=mi-1;
  33. }
  34. return res;
  35. }
  36. */
  37. int main()
  38. {
  39. ios_base::sync_with_stdio(0);
  40.  
  41. cin>>n>>k;
  42. for(int i=0;i<n;i++) cin>>a[i];
  43.  
  44. sort(a,a+n,greater<int>());
  45. int i,j=0;
  46. for(i=j;i<n;i++)
  47. {
  48. for(j=i;j<n;j++)
  49. {
  50. if(a[i]-a[j]<=5)
  51. {
  52. d++;
  53. if(j==n-1) check=1;
  54. }
  55. else break;
  56. }
  57. u.pub(d);
  58. if(check==1) break;
  59. d=0;
  60. }
  61. sort(u.begin(),u.end(),greater<int>());
  62.  
  63. for(int i=0;i<u.size()&&i<k;i++) res+=u.at(i);
  64. cout<<res;
  65. return 0;
  66. }
  67.  
  68.  
  69.  
  70. # ///////////// /// /// ////// ///// /// /// ///
  71. # /// /// /// /// /// /// // /// /// ///
  72. # /// /// /// /// /// /// // /// /// ///
  73. # /// /////////// /////////// /// // /// ///////////
  74. # /// /// /// /// /// /// // /// /// ///
  75. # /// /// /// /// /// /// // /// /// ///
  76. # /// /// /// /// /// /// ///// /// ///
  77.  
  78. # /////// //////// ///// /// ///////
  79. # /// /// /// /// /// // /// /// ///
  80. # /// /// /// /// // /// ///
  81. # /// /// /// /// // /// /// _____
  82. # /// /// /// /// // /// /// ____
  83. # /// /// /// /// /// // /// /// ///
  84. # /////// //////// /// ///// ///////
  85.  
  86.  
  87.  
  88.  
Success #stdin #stdout 0s 16016KB
stdin
Standard input is empty
stdout
Standard output is empty