[Codeforces] [580B] Kefa and Company

Problem Description

Loading...

Problem Analysis

Sort by the amount of money
Compute the total friendliness for each interval of people who can attend together
For each new person, check whether they can be together with the person at the far left
If not, remove the person at the far left
Changing the sum value this way
And update max

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node {
    int m,s;
} a[100200];
bool cmp(node A,node B) {
    return A.m<B.m;
}
int main() {
    int n,d;
    while(scanf("%d %d",&n,&d)!=EOF) {
        for(int i=0; i<n; i++) {
            scanf("%d %d",&a[i].m,&a[i].s);
        }
        sort(a,a+n,cmp);
        int l=0;
        __int64 max=0,sum=0;
        for(int i=0; i<n; i++) {
            sum+=(__int64)a[i].s;
            while(a[i].m>=a[l].m+d) {
                sum-=(__int64)a[l].s;
                l++;
            }
            if(max<sum)
                max=sum;
        }
        printf("%I64d\n",max);
    }
    return 0;
}

Loading...