Codeforces Round 321 (Div. 2) 580B - Kefa and Company Solution


  

Problem Link  :  https://codeforces.com/problemset/problem/580/B

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. const int mx = 200009;
  10. using ll = long long ;
  11. int32_t main() {
  12. ios_base::sync_with_stdio(0);
  13. cin.tie(0);
  14. ll n , d ;
  15. cin >> n >> d ;
  16. pair < ll , ll > p[n] ;
  17. for ( int i = 0 ; i < n ; i++ ){
  18. cin >> p[i].first >> p[i].second ;
  19. }
  20. sort ( p , p + n ) ;
  21. ll ans = 0 , sum = 0 , j = 0 ;
  22. for ( int i = 0 ; i < n ; ) {
  23. if ( p[i].first - p[j].first < d ) {
  24. sum += p[i].second ;
  25. i++ ;
  26. }
  27. else {
  28. sum -= p[j].second ;
  29. j++ ;
  30. }
  31. ans = max ( ans , sum ) ;
  32. }
  33. cout << ans << endl;
  34. return 0 ;
  35. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.