fork(1) download
  1. /*
  2.  * author :Sadik Hassan(_sad_)
  3.  *
  4. */
  5. #pragma GCC optimize("Ofast")
  6. #pragma GCC target("avx,avx2,fma")
  7. #pragma GCC optimization ("unroll-loops")
  8. #include "bits/stdc++.h"
  9. using namespace std;
  10. using ll = long long;
  11. using ld = long double;
  12. #define nl "\n"
  13. #define pb push_back
  14. #define fi first
  15. #define se second
  16. #define MP make_pair
  17. #define PI (acos(-1.0))
  18. #define rep1(i,n) for(int i=1;i<=n;i++)
  19. #define rep(i,n) for(int i=0;i<n;i++)
  20. #define urep(i,n) for(int i=n-1;i>=0;i--)
  21. #define urep1(i,n) for(int i=n;i>=1;i--)
  22. #define SZ(s) (int)s.size()
  23. #define all(x) (x).begin(), (x).end()
  24. #define rall(x) (x).rbegin(), (x).rend()
  25. #define w(t) int t;cin>>t;while(t--)
  26. #define _SAD() ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cout<<fixed<<setprecision(15);
  27.  
  28. typedef vector<int> vi;
  29. typedef vector<ll> vii;
  30. typedef set<int> si;
  31. typedef set<ll> sii;
  32. /*---------------------------------------------------------------------*/
  33.  
  34. const int N = (int)2e5+5;
  35. const int MOD =(int)1e9+7;
  36. const ll INF = (ll)1e18+5;
  37. const int M2 = 998244353;
  38. int main()
  39. {
  40. _SAD()
  41. int n,k;
  42. cin>>n>>k;
  43. ll cnt=0;
  44. vii a;
  45. rep(i,n)
  46. {
  47. ll x;
  48. cin>>x;
  49. if(x<k)
  50. {
  51. a.pb(x);
  52. }
  53. }
  54. sort(all(a));
  55. ll i=0,j=SZ(a)-1;
  56. while(i<=j)
  57. {
  58. ll pp=min(k-a[i],k-a[j]);
  59. cnt+=pp;
  60. a[i]+=pp;
  61. if(i!=j)a[j]+=pp;
  62. if(a[j]>=k)j--;
  63. if(a[i]>=k)i++;
  64. }
  65. cout<<cnt<<nl;
  66. return 0;
  67. }
  68.  
Time limit exceeded #stdin #stdout 5s 4204KB
stdin
Standard input is empty
stdout
Standard output is empty