Codeforces Round 321 (Div. 2) 580B - Kefa and Company Solution
Problem Link : https://codeforces.com/problemset/problem/580/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- const int mx = 200009;
- using ll = long long ;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- ll n , d ;
- cin >> n >> d ;
- pair < ll , ll > p[n] ;
- for ( int i = 0 ; i < n ; i++ ){
- cin >> p[i].first >> p[i].second ;
- }
- sort ( p , p + n ) ;
- ll ans = 0 , sum = 0 , j = 0 ;
- for ( int i = 0 ; i < n ; ) {
- if ( p[i].first - p[j].first < d ) {
- sum += p[i].second ;
- i++ ;
- }
- else {
- sum -= p[j].second ;
- j++ ;
- }
- ans = max ( ans , sum ) ;
- }
- cout << ans << endl;
- return 0 ;
- }
No comments