solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n, d;
cin >> n >> d;
vector<pair<int, int> > fr(n);
for (int i = 0; i < n; i++) cin >> fr[i].first >> fr[i].second;
sort(fr.begin(), fr.end());
vector<long long> w(n + 1, 0);
for (int i = 1; i <= n; i++) w[i] = w[i - 1] + fr[i - 1].second;
long long ans = -1;
for (int i = 0; i < n; i++) {
int mn = max(0, fr[i].first - d + 1);
int ln = 0, rn = n - 1;
while (fr[ln].first < mn) {
int m = (rn + ln) / 2;
if (fr[m].first < mn)
ln = m + 1;
else
rn = m;
}
int mx = mn + d - 1;
int lx = 0, rx = n - 1;
while (fr[rx].first > mx) {
int m = (rx + lx) / 2;
if (fr[m].first > mx)
rx = m - 1;
else
lx = m + 1;
}
ans = max(ans, w[rx + 1] - w[ln]);
}
cout << ans;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d;
cin >> n >> d;
vector<pair<long long, long long>> v;
long long a, b;
for (long long i = 0; i < n; i++) {
cin >> a >> b;
v.push_back({a, b});
}
sort(v.begin(), v.end());
long long i = 0, j = 0, ma = 0, cur = 0;
while (j < v.size()) {
if (v[j].first - v[i].first < d) {
cur += v[j].second;
ma = max(cur, ma);
j++;
} else {
cur -= v[i].second;
ma = max(cur, ma);
i++;
if (i > j) {
j = i;
}
}
}
cout << ma;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > a;
long long b[100001], c[100001];
int n;
long long d;
bool gr(pair<int, int> a, pair<int, int> b) {
if (a.second < b.second) return false;
return true;
}
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a.push_back(make_pair(x, y));
}
sort(a.begin(), a.end());
long long ans = 0;
long long sum = 0;
int l = 0;
for (int i = 0; i < n; i++) {
sum += a[i].second;
while (a[i].first - a[l].first >= d) {
sum -= a[l].second;
l++;
}
if (ans < sum) ans = sum;
}
cout << ans;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, diff;
cin >> n >> diff;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v[i] = make_pair(x, y);
}
sort(v.begin(), v.end());
long long l = 0, r = 1, sum = v[0].second, ans = sum;
while (r < n) {
if (abs(v[r].first - v[l].first) < diff) {
sum += v[r].second;
ans = max(sum, ans);
r++;
} else {
sum -= v[l].second;
l++;
}
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
r = lambda: map(int,input().split())
f,m = r()
l = [list(r()) for _ in range(f)]
l = sorted(l,key = lambda x: x[0])
last = 0
max_score = 0
current_score = 0
for cash, friend in l:
if cash >= (last+m):
max_score = max(max_score,current_score)
current_score = friend
last = cash
else:
current_score += friend
max_score = max(max_score,current_score)
print (max_score) | 8 | PYTHON3 |
#!/usr/bin/env python3
if __name__ == "__main__":
n, d = map(int, input().split())
li = []
for i in range(n):
m, s = map(int, input().split())
li.append((m,s))
li = sorted(li)
total = li[0][1]
maxTotal = li[0][1]
p = 0
for i in range(1,n):
while li[i][0] >= d + li[p][0]:
total -= li[p][1]
p += 1
total += li[i][1]
maxTotal = max(maxTotal, total)
print(maxTotal)
| 8 | PYTHON3 |
import re
import sys
exit=sys.exit
from bisect import bisect_left as bsl,bisect_right as bsr
from collections import Counter,defaultdict as ddict,deque
from functools import lru_cache
cache=lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum=enumerate
ri=lambda:int(rln())
ris=lambda:list(map(int,rfs()))
rln=sys.stdin.readline
rl=lambda:rln().rstrip('\n')
rfs=lambda:rln().split()
mod=1000000007
d4=[(0,-1),(1,0),(0,1),(-1,0)]
d8=[(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]
########################################################################
n,d=ris()
a=[]
for _ in range(n):
a.append(ris())
a.sort()
ans=s=l=0
for r in range(n):
s+=a[r][1]
while d<=a[r][0]-a[l][0]:
s-=a[l][1]
l+=1
ans=max(ans,s)
print(ans)
| 8 | PYTHON3 |
z,zz=input,lambda:list(map(int,z().split()))
zzz=lambda:[int(i) for i in stdin.readline().split()]
szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz())
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if (x%i==0 and x!=2) or x==0:return 0
return 1
def dfs(u,visit,graph):
visit[u]=True
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
###########################---Test-Case---#################################
"""
"""
###########################---START-CODING---##############################
n,m=zz()
lst=[]
for _ in range(n):
lst.append(zz())
lst=sorted(lst)
ans=0
l=0
s=0
for i in range(n):
for j in range(i+1):
if lst[i][0]-lst[l][0]>=m:
s-=lst[l][1]
l+=1
if l>n:
l=n-1
else:
break
s+=lst[i][1]
ans=max(ans,s)
print(ans)
| 8 | PYTHON3 |
#!/usr/bin/python3
# -*- coding: <utf-8> -*-
import itertools as ittls
from collections import Counter
def sqr(x):
return x*x
def inputarray(func = int):
return map(func, input().split())
# -------------------------------
# -------------------------------
N, d = map(int, input().split())
A = [(None, None)]*N
for i in range(N):
A[i] = tuple(map(int, input().split()))
A.sort(key=lambda x: x[0])
prefix = [0] + [None]*len(A)
for i, (money, friendship) in enumerate(A):
prefix[i + 1] = prefix[i] + friendship
res, i, j = 0, 0, 0
while i < len(A):
while j < len(A) and A[j][0] < A[i][0] + d:
j = j + 1
if prefix[j] - prefix[i] > res:
res = prefix[j] - prefix[i]
i = i + 1
print(res)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
while (a && b) a > b ? a %= b : b %= a;
return a + b;
}
long long int lcm(long long int a, long long int b) {
return (max(a, b) / gcd(a, b)) * min(a, b);
}
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b != 0) {
if (b % 2 != 0) {
res = (res * a) % 1000000007;
b--;
} else {
a = (a * a) % 1000000007;
b /= 2;
}
}
return res;
}
long long int f[100005];
long long int n, d;
pair<long long int, long long int> v[100005];
long long int Find(long long int u) {
long long int l = 0, r = n;
while (l < r) {
long long int m = (l + r + 1) / 2;
if (v[m].first > u)
r = m - 1;
else
l = m;
}
return l;
}
int main() {
ios::sync_with_stdio(0);
;
long long int i, j, k, c = 0, m, t, l, r, x, y, w, low, high, mid;
string s;
cin >> n >> d;
vector<long long int> a, b;
for (i = 1; i <= n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v + 1, v + n + 1);
for (i = 1; i <= n; i++) {
f[i] = f[i - 1] + v[i].second;
c = max(c, f[i] - f[Find(v[i].first - d)]);
}
cout << c;
return 0;
}
| 8 | CPP |
n,d = map(int,input().split())
friends = []
for i in range(n):
money, factor = map(int,input().split())
friends.append([money,factor])
friends.sort()
ans = 0
tmp_ans = friends[0][1]
back = 0
front = 1
while front < n:
if friends[front][0] - friends[back][0] < d:
tmp_ans+=friends[front][1]
front+=1
else:
if tmp_ans > ans:
ans = tmp_ans
while friends[front][0] - friends[back][0] >= d:
tmp_ans -= friends[back][1]
back+=1
if back == front:
tmp_ans+=friends[back][1]
front+=1
if tmp_ans > ans:
ans = tmp_ans
print(ans) | 8 | PYTHON3 |
if __name__ == '__main__':
n, d = map(int, input().split())
line = list()
for i in range(n):
line.append(list(map(int, input().split())))
line.sort(key=lambda a: a[0])
refer = list()
note = line[0][0]
flag_a = flag_b = False
flag_v = 0
for i in range(n - 1):
if flag_a:
if flag_v - line[i][0] >= d:
continue
else:
flag_a = False
if flag_b:
break
for j in range(i + 1, n):
if line[j][0] - line[i][0] >= d:
refer.append(sum([line[k][1] for k in range(i, j)]))
flag_a = True
flag_v = line[j][0]
break
if j == n - 1:
refer.append(sum([line[k][1] for k in range(i, n)]))
flag_b = True
break
refer.append(line[-1][1])
print(max(refer))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline void in(int &n) {
n = 0;
int ch = getchar();
int sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
}
n = n * sign;
}
pair<int, int> a[100005];
int b[100005];
long long int f[100005];
int main() {
int n, d;
in(n);
in(d);
for (int i = 1; i <= n; i++) {
in(a[i].first);
in(a[i].second);
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[i] = a[i].first;
}
for (int i = n; i >= 1; i--) {
f[i] = f[i + 1] + a[i].second;
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
int up = upper_bound(b + 1, b + 1 + n, a[i].first + d - 1) - (b + 1);
ans = max(ans, f[i] - f[up + 1]);
}
printf("%I64d\n", ans);
return 0;
}
| 8 | CPP |
from sys import stdin, stdout
lines = stdin.readlines()
n, d = int(lines[0].split()[0]), int(lines[0].split()[1])
a = sorted([[int(x.split()[0]),int(x.split()[1])] for x in lines[1:]])
max_val = 0
start_pointer = 0
cur_val = 0
for end_pointer in range(n):
cur_val += a[end_pointer][1]
while (a[end_pointer][0] - a[start_pointer][0]) >= d:
cur_val -= a[start_pointer][1]
start_pointer += 1
if cur_val > max_val:
max_val = cur_val
print(max_val) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, d;
cin >> n >> d;
vector<std::pair<int, int> > v;
long long m, s;
for (int i = 0; i < (n); ++i) {
cin >> m >> s;
v.push_back(make_pair(m, s));
}
sort(v.begin(), v.end());
long long maxm = v[0].second, sum = 0, prev = v[0].first, index = 0;
for (int i = 1; i <= (n - 1); ++i) {
if (i == 1 && v[i].first - prev < d) {
sum += v[i - 1].second;
maxm = max(sum, maxm);
}
if (v[i].first - prev < d) {
sum += v[i].second;
maxm = max(sum, maxm);
} else {
sum -= v[index].second;
maxm = max(sum, maxm);
index++;
prev = v[index].first;
i--;
}
}
cout << maxm;
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long int INF = 1e18;
const int MX = 300001;
int rownum[] = {-1, 0, 0, 1};
int colnum[] = {0, -1, 1, 0};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t, x, y, p, q, c, e, i, j, n, m, k;
cin >> n >> p;
vector<pair<long long int, long long int> > v;
vector<long long int> vf;
for (i = 0; i < n; i++) {
cin >> x >> y;
v.push_back(make_pair(x, y));
vf.push_back(x);
}
sort(v.begin(), v.end());
sort(vf.begin(), vf.end());
for (i = 1; i < n; i++) {
v[i].second += v[i - 1].second;
}
k = 0;
for (i = 0; i < n; i++) {
q = upper_bound(vf.begin(), vf.end(), vf[i] + p - 1) - vf.begin();
if (i == 0)
k = max(k, v[q - 1].second);
else
k = max(k, v[q - 1].second - v[i - 1].second);
}
cout << k;
}
| 8 | CPP |
import bisect
n,d=map(int,input().split())
a,b,c,i1,l,s,st,lt=0,0,0,0,[],[],[],[]
f=0
for i,j in enumerate(range(n)):
m,p=map(int,input().split())
l.append([m,p])
l=sorted(l,key=lambda li:li[0])
for i , j in enumerate(range(n)):
s.append(l[i][0])
st.append(l[i][1])
while i1<=n-1:
a=bisect.bisect_right(s,(s[i1]+(d-1)))-1
c=bisect.bisect_left(s,(s[a]-(d-1)))
f=bisect.bisect_left(s,(s[i1]-(d-1)))
lt.append(sum(st[c:a+1]))
lt.append(sum(st[f:i1+1]))
i1=a+1
print(max(lt)) | 8 | PYTHON3 |
n,d = list(map(int,input().split()))
arr = []
for i in range(n):
arr.append(list(map(int,input().split())))
arr.sort()
l,r = 0,0
ans = 0
t = 0
# print(arr)
while r<n:
# print(l,r)
if arr[r][0]-arr[l][0]<d:
t+=arr[r][1]
r+=1
else:
ans = max(ans,t)
t-=arr[l][1]
l+=1
ans = max(ans,t)
print(ans)
| 8 | PYTHON3 |
def binary_search(arr, n, key):
l, r = 0, n-1
while (l <= r):
mid = l+r >> 1
if (arr[mid][0] <= key):
l = mid+1
else:
r = mid-1
return l
def main():
n, d = map(int, input().split())
arr = []
for _ in range(n):
m, s = map(int, input().split())
arr += [(m, s)]
arr.sort()
arr_acc = [0] * (n+1)
for i in range(n):
arr_acc[i+1] = arr_acc[i] + arr[i][1]
res = 0
for i in range(n):
key = arr[i][0]+d-1
j = binary_search(arr, n, key)
res = max(res, arr_acc[j] - arr_acc[i])
print(res)
main() | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int i, d, j, k, l, t, n;
cin >> n >> d;
vector<pair<long long int, long long int>> v;
l = -1;
for (i = 0; i < n; i++) {
cin >> j >> k;
v.push_back(make_pair(j, k));
}
sort(v.begin(), v.end());
long long int cnt = v[0].first;
long long int ci = 0;
long long int ans = v[0].second;
long long int sum = v[0].second;
for (i = 1; i < n; i++) {
if (v[i].first - cnt < d) {
sum += v[i].second;
continue;
} else {
ans = max(ans, sum);
sum += v[i].second;
for (j = ci; j < i; j++) {
if (v[i].first - v[j].first < d) {
cnt = v[j].first;
ci = j;
break;
} else
sum -= v[j].second;
}
if (j == i) {
cnt = v[i].first;
ci = i;
}
}
}
ans = max(ans, sum);
cout << ans << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\n";
err(++it, args...);
}
struct xx {
bool operator()(pair<int, int>& x, pair<int, int>& y) {
return y.first > x.first;
}
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int n, d;
cin >> n >> d;
vector<pair<int, int>> arr(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
arr[i] = {x, y};
}
sort(arr.begin(), arr.end(), xx());
long long max_factor = INT_MIN;
long long maxx = 0;
int j = 0;
for (int i = 0; i < n && j < n;) {
if (abs(arr[j].first - arr[i].first) < d) {
maxx += arr[j].second;
j++;
} else {
{
string _s = "maxx";
replace(_s.begin(), _s.end(), ',', ' ');
stringstream _ss(_s);
istream_iterator<string> _it(_ss);
err(_it, maxx);
};
max_factor = max(maxx, max_factor);
maxx -= arr[i].second;
i++;
}
max_factor = max(maxx, max_factor);
}
cout << max_factor;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
pair<int, int> a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, d;
cin >> n >> d;
for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
sort(a, a + n);
int L = 0, R = 0;
long long temp = 0, ans = INT_MIN;
while (R < n) {
temp += a[R++].second;
while (a[R - 1].first - a[L].first >= d) temp -= a[L++].second;
ans = max(ans, temp);
}
cout << ans << '\n';
return 0;
}
| 8 | CPP |
n, d = map(int, input().split())
p = []
for i in range(n):
m, f = map(int, input().split())
p.append([m, f])
p = sorted(p)
x2 = 0
max_f = 0
f = 0
for x1 in range(n):
x2 = max(x1, x2)
while x2 < n and p[x2][0] - p[x1][0] < d:
f += p[x2][1]
x2 += 1
if f > max_f:
max_f = f
f -= p[x1][1]
print(max_f) | 8 | PYTHON3 |
n, d = list(map(int, input().split()))
friends = []
max_friendship = 0
tmp_index = 0
tmp_friendship = 0
for k in range(n):
m, s = list(map(int, input().split()))
friends.append({'m': m, 's': s})
friends = sorted(friends, key=lambda x: x['m'])
k = 0
while k < n:
if abs(friends[tmp_index]['m'] - friends[k]['m']) >= d:
tmp_friendship -= friends[tmp_index]['s']
tmp_index += 1
else:
tmp_friendship += friends[k]['s']
max_friendship = max(max_friendship, tmp_friendship)
k += 1
print(max_friendship)
| 8 | PYTHON3 |
[n,d] = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
a.sort()
ans=a[0][1]
res=a[0][1]
p1=0
p2=0
while(p2 < n-1):
if (a[p2+1][0] - a[p1][0] < d):
p2 += 1
ans += a[p2][1]
if(ans > res):
res=ans
else:
ans -= a[p1][1]
p1 += 1
print(res)
| 8 | PYTHON3 |
n, d = list(map(int, input().split()))
pairs = []
for i in range(n):
pairs.append(list(map(int, input().split())))
pairs.sort(key=lambda x: x[0])
i = 0
j = 0
count = 0
numbers = []
while(i < n):
if(pairs[i][0] - pairs[j][0] < d):
count += pairs[i][1]
numbers.append(count)
i += 1
else:
count -= pairs[j][1]
j += 1
print(max(numbers)) | 8 | PYTHON3 |
n, k = [int(i) for i in input().split()]
arr = []
for i in range(n):
s = [int(i) for i in input().split()]
arr.append(s)
lst = []
arr.sort()
v = arr[0][1]
j = 1
for i in range(len(arr)):
if j == n:
break
while abs(arr[i][0] - arr[j][0]) < k:
v += arr[j][1]
j += 1
if j > n - 1:
break
lst.append(v)
v -= arr[i][1]
lst = lst + [i[1] for i in arr]
print(max(lst))
| 8 | PYTHON3 |
#
# Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
#
# Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks.
# Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa.
# The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count).
# A friend feels poor if in the company there is someone who has at least d units of money more than he does.
# Also, Kefa wants the total friendship factor of the members of the company to be maximum.
# Help him invite an optimal company!
#
# Input
#
# The first line of the input contains two space-separated integers, n and d (1ββ€βnββ€β105, ) β the number of Kefa's
# friends and the minimum difference between the amount of money in order to feel poor, respectively.
#
# Next n lines contain the descriptions of Kefa's friends, the (iβ+β1)-th line contains
# the description of the i-th friend of type mi, si (0ββ€βmi,βsiββ€β109) β the amount of money
# and the friendship factor, respectively.
#
# Output
#
# Print the maximum total friendship factor that can be reached.
#
# Examples
#
# Input
#
# 4 5
# 75 5
# 0 100
# 150 20
# 75 1
#
# Output
#
# 100
#
# Input
#
# 5 100
# 0 7
# 11 32
# 99 10
# 46 8
# 87 54
#
# Output
#
# 111
#
# Note
#
# In the first sample test the most profitable strategy is to form a company from only the second friend.
# At all other variants the total degree of friendship will be worse.
#
# In the second sample test we can take all the friends.
#
import sys
class Solution:
def max_friendship_factor(self, money_diff, friends_data):
# sort by money amount, so that we could go through all
# the intervals where money diff in [0, money_diff]
friends_data = sorted(friends_data, key=lambda f: f[0])
# count friendship factors for every prefix
factors = [friends_data[0][1]]
for i in range(1, len(friends_data)):
factors.append(factors[i-1] + friends_data[i][1])
# go through all the intervals where
# money diff in [0, money_diff]
ff_max, j = 0, len(friends_data) - 1
for i in range(len(friends_data) - 1, -1, -1):
# find interval left border, on each step j will either
# stay the same or decrease since we're decreasing right border
while j > 0 and friends_data[j][0] - friends_data[i][0] >= money_diff:
j -= 1
# calculate friendship factor for the interval
ff = factors[j]
if i > 0:
ff -= factors[i - 1]
# compare if it's total maximum
if ff > ff_max:
ff_max = ff
return ff_max
def main():
inp = sys.stdin.readline()
friends, money_diff = list(map(int, inp.split()))
friends_data = []
while friends > 0:
inp = sys.stdin.readline()
friends_data.append(list(map(int, inp.split())))
friends -= 1
print(Solution().max_friendship_factor(money_diff, friends_data))
if __name__ == "__main__":
main()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int main() {
int n, d;
scanf("%d%d", &n, &d);
v.resize(n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &v[i].first, &v[i].second);
}
sort(v.begin(), v.end());
long long sum = 0, ans = 0;
int l = 0, r = 0;
while (r < n) {
if (v[r].first - d < v[l].first) {
sum += v[r].second;
r++;
} else {
ans = max(ans, sum);
sum -= v[l].second;
l++;
}
}
cout << max(ans, sum);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a, a + n);
long long int mx = 0, x = 0, l = 0, r = 0;
for (; l < n; l++) {
while (r < n && a[r].first < a[l].first + d) {
x += a[r].second;
r++;
}
mx = max(mx, x);
x -= a[l].second;
}
cout << mx << "\n";
return 0;
}
| 8 | CPP |
n,m=map(int,input().split())
ls=[list(map(int,input().split())) for i in range(n) ]
ls.sort(reverse=True)
su,maximum=0,0
su=ls[0][1]
maximum=su
top=0
i=1
while i<n:
while i<n and ls[i][0] +m > ls[top][0] :
su+=ls[i][1]
i+=1
maximum=max(maximum,su)
if i==n: break
while i>top and ls[i][0] +m <= ls[top][0]:
su-=ls[top][1]
top+=1
su+=ls[i][1]
maximum=max(maximum,su)
i+=1
print(maximum)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
typedef struct Friend {
long long m, s;
} F;
long long n, d, res = 0;
F a[100001];
void quicksort(long long d, long long c) {
if (d >= c) return;
srand(time(NULL));
long long i1 = d, j1 = c, val = a[d + rand() % (c - d + 1)].m;
while (i1 <= j1) {
while (a[i1].m < val) i1++;
while (a[j1].m > val) j1--;
if (i1 <= j1) {
if (i1 < j1) {
F tmp = a[i1];
a[i1] = a[j1];
a[j1] = tmp;
}
i1++;
j1--;
}
}
quicksort(d, j1);
quicksort(i1, c);
}
int main() {
scanf("%I64d%I64d", &n, &d);
for (long long i = 1; i <= n; i++) scanf("%I64d%I64d", &a[i].m, &a[i].s);
quicksort(1, n);
a[0].s = 0;
for (long long i = 1; i <= n; i++) a[i].s += a[i - 1].s;
long long j = 1;
for (long long i = 1; i <= n; i++) {
while (j < n && a[j + 1].m - a[i].m < d) j++;
if (a[j].s - a[i - 1].s > res) res = a[j].s - a[i - 1].s;
}
printf("%I64d", res);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int xDir[8] = {0, 0, -1, 1, 1, -1, -1, 1};
int yDir[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int EPS = 1e-10;
int dComp(double d1, double d2) {
if (abs(d1 - d2) <= EPS) return 0;
if (d1 > d2)
return 1;
else
return -1;
}
bool lucky(int i) {
string s = to_string(i);
for (int j = 0; j < s.size(); j++)
if (s[j] - '0' != 4 && s[j] - '0' != 7) return false;
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
vector<pair<int, int> > vec(100001);
for (int i = 0; i < n; i++) {
cin >> vec[i].first >> vec[i].second;
}
sort(vec.begin(), vec.begin() + n);
int left = 0, right = 0;
long long sum = 0;
long long mx = -1;
while (left <= right && right < n && left < n) {
if (abs(vec[left].first - vec[right].first) < d) {
sum += vec[right].second;
right++;
mx = max(mx, sum);
} else {
sum -= vec[left].second;
left++;
}
mx = max(mx, sum);
}
cout << mx << endl;
}
| 8 | CPP |
def bina(arr,target,i,n):
l,h,ans=i,n-1,i
while(l<=h):
m=(l+h)//2
if arr[m][0]>=target:
h=m-1
else:
ans=m
l=m+1
return ans
n,d=list(map(int,input().split()))
arr=[tuple(map(int,input().split())) for _ in range(n)]
arr.sort(key=lambda x :x[0])
prefix=[0]*n
prefix[0]=arr[0][1]
for i in range(1,n):
prefix[i]=prefix[i-1]+arr[i][1]
mx=-1
for i in range(n):
target=arr[i][0]+d
ind=bina(arr,target,i,n)
mx=max(mx,prefix[ind]-prefix[i]+arr[i][1])
print(mx) | 8 | PYTHON3 |
import sys
from bisect import bisect_right
def main():
n, d = map(int, input().split())
friends = sorted(tuple(int(c) for c in line.split()) for line in sys.stdin.readlines())
money = [e[0] for e in friends]
friendship_sum = [0]
for _, e in friends:
friendship_sum.append(friendship_sum[-1] + e)
_min = friends[0][0]
i = 1
start = 0
ans = friends[0][1]
while i < n:
cur = money[i]
diff = cur - _min
_min = min(_min, cur)
if diff >= d:
start = bisect_right(money, cur - d)
_min = money[start]
friendship = friendship_sum[i+1] - friendship_sum[start]
ans = max(ans, friendship)
i += 1
print(ans)
if __name__ == "__main__":
main()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second > b.second);
}
void solve() {
long long n, d;
cin >> n >> d;
vector<pair<long long, long long>> v(n);
long long sum1 = 0;
for (long long i = 0; i < n; i++) {
long long x, y;
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
long long l = 0, r = 0;
long long ans = 0;
long long s = 0;
for (; l < n; ++l) {
while (r < n && v[r].first < v[l].first + d) {
s += v[r].second;
++r;
}
ans = max(ans, s);
s -= v[l].second;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
solve();
return 0;
}
| 8 | CPP |
n, d = map(int, input().split())
a = []
for i in range(n):
m, s = map(int, input().split())
a.append((m, s))
a.sort()
l = 0
r = 0
sm = 0
mx = 0
while l < n:
while a[r][0] - a[l][0] < d:
sm += a[r][1]
r += 1
if r == n:
break
if sm > mx:
mx = sm
if r == n:
break
sm -= a[l][1]
l += 1
print(mx)
| 8 | PYTHON3 |
def key_tri(argument):
return argument[0]
n,d = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(n)]
L.sort(key=key_tri)
deb = 0
T = []
s = 0
for k in range(1,n):
if L[k][0] - L[deb][0] >= d:
s = 0
for i in range(deb,k):
s+=L[i][1]
T+=[s]
s = deb+1
while (L[k][0] - L[s][0]) >= d:
s+=1
deb = s
s = 0
for k in range(deb,n):
s+=L[k][1]
T = T + [s]
print(max(T))
| 8 | PYTHON3 |
from functools import cmp_to_key
def readnums():
return list(map(lambda x: int(x), input().split(" ")))
def mycmp(a, b):
if a[0] > b[0]:
return 1
elif a[0] < b[0]:
return -1
else:
if a[1] > b[1]:
return 1
elif a[1] < b[1]:
return -1
return 0
n, d = readnums()
fr = []
for i in range(n):
fr.append(tuple(readnums()))
fr.sort(key=cmp_to_key(mycmp))
# print(fr)
ans = 0
min_money = 0
friendship = 0
i = 0
j = 0
while i < n:
min_money = fr[i][0]
ans = max(ans, friendship)
if (i > j):
j = i
while (j < n) and (abs(fr[j][0] - min_money) < d):
# print(i, j, min_money, (abs(fr[j][0] - min_money)))
friendship += fr[j][1]
j += 1
# print(j, friendship)
ans = max(ans, friendship)
# print(ans)
friendship -= fr[i][1]
i += 1
print(ans)
| 8 | PYTHON3 |
n,d=map(int,input().split(" "))
sum1=0
maximum=-1
a=[]
arr=[]
arr.append(0)
for x in range(n):
a.append(list(map(int,input().split(" "))))
a.sort()
for x in range(n):
sum1+=a[x][1]
arr.append(sum1)
y=0
for x in range(n):
while(y<n and a[y][0]-a[x][0]<d):
y+=1
maximum=max(maximum,arr[y]-arr[x])
print(maximum)
| 8 | PYTHON3 |
import bisect
from collections import Counter,defaultdict
from sys import stdin, stdout
input = stdin.readline
I =lambda:int(input())
M =lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
n,m=M()
d=defaultdict(int)
b=[]
ans=0
for _ in range(n):
a,b=M()
d[a]+=b
ans=max(ans,d[a])
c=list(d.keys());c.sort()
e=[0];val=0
for i in range(len(c)):
val+=d[c[i]]
e+=[val]
for i in range(len(c)):
f=c[i]+m
ind=bisect.bisect_left(c,f)
ans=max(ans,e[ind]-e[i])
print(ans)
| 8 | PYTHON3 |
from collections import namedtuple
Friend = namedtuple("Friend", "money value")
n, d = map(int, input().split())
# At first we sort all friends in money ascending order.
a = []
for i in range(n):
money, value = map(int, input().split())
a.append(Friend(money, value))
a.sort()
# Now the answer is some array subsegment.
s = ans = a[0].value
tail = 0
# Next, we use the method of two pointers for finding the required subsegment.
for head in range(1, n):
while (a[head].money - a[tail].money >= d):
s -= a[tail].value
tail += 1
s += a[head].value
ans = max(s, ans)
print(ans)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 100;
long long sum;
long long sumLeft[MAX];
pair<int, int> arr[MAX];
int main() {
int n, d;
scanf("%d %d", &n, &d);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &arr[i].first, &arr[i].second);
}
arr[n + 1].first = INT_MAX;
sort(arr, arr + n + 1);
for (int i = 1; i <= n; i++) sumLeft[i] = sumLeft[i - 1] + arr[i].second;
sum = sumLeft[n];
long long ans = 0;
for (int i = 1; i <= n; i++) {
int index =
distance(arr, upper_bound(arr, arr + n + 1,
make_pair(arr[i].first + d - 1, INT_MAX)));
ans = max(ans, sumLeft[index - 1] - sumLeft[i - 1]);
}
cout << ans << endl;
}
| 8 | CPP |
entrada = list(map(int, input().split()))
friends = []
for i in range(entrada[0]):
friend = list(map(int, input().split()))
friends.append(friend)
friends.sort()
combinations = []
pivot1 = 0
pivot2 = 0
totalSoma = 0
while(pivot1!=entrada[0]):
if (pivot2<entrada[0] and friends[pivot2][0]-friends[pivot1][0]<entrada[1]):
totalSoma += friends[pivot2][1]
pivot2+=1
else:
combinations.append(totalSoma)
totalSoma -= friends[pivot1][1]
pivot1+=1
combinations.append(totalSoma)
combinations.sort()
print (combinations[-1])
| 8 | PYTHON3 |
entrada = list(map(int, input().split(' ')))
lista = []
friendship = 0
p = 1
for i in range(entrada[0]):
temp = list(map(int, input().split()))
lista.append((temp[0],temp[1]))
lista.sort()
lista2 = [0]
for i in range(1, entrada[0]+1):
lista2.append(lista2[i-1] + lista[i-1][1])
for i in range(entrada[0]):
while ((p < entrada[0]) and (lista[i][0] + entrada[1] > lista[p][0])):
p +=1
friendship = max(friendship, lista2[p] - lista2[i]);
print(friendship)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int n, d;
long long sum[maxn];
pair<int, int> a[maxn];
while (cin >> n >> d) {
for (int i = 1; i <= n; ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + a[i].second;
}
int j = 1;
long long res = 0;
for (int i = 0; i <= n; ++i) {
while (a[i].first - a[j].first >= d) {
++j;
}
res = max(res, sum[i] - sum[j - 1]);
}
cout << res << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0, lim - 1);
return uid(rang);
}
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
void solve() {
long long n, d;
cin >> n >> d;
vector<pair<long long, long long> > v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
vector<long long> cs(n, 0);
for (long long i = 0; i < n; i++) {
cs[i] = v[i].second;
if (i) cs[i] += cs[i - 1];
}
long long res = 0;
for (long long i = 0; i < n; i++) {
pair<long long, long long> p = {v[i].first + d, -1};
long long idx = lower_bound(v.begin(), v.end(), p) - v.begin() - 1;
long long curr_res = cs[idx];
if (i) curr_res -= cs[i - 1];
res = max(res, curr_res);
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d %d", &n, &k);
vector<pair<int, int> > A;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
A.push_back(make_pair(x, y));
}
sort(A.begin(), A.end());
long long ans = 0;
long long co = 0;
long long pre;
vector<pair<int, int> >::iterator it = A.begin();
vector<pair<int, int> >::iterator fir = A.begin();
co = (*it).second;
pre = (*it).first;
ans = (*it).second;
it++;
for (; it != A.end(); it++) {
while ((*it).first - ((*fir).first) >= k) {
co = co - (*fir).second;
fir++;
}
co += (*it).second;
ans = max(ans, co);
}
printf("%lld", ans);
return 0;
}
| 8 | CPP |
n,d = map(int,input().split())
x = []
for i in range(n):
a,b = map(int,input().split())
x.append((a,b))
x.sort()
y = []
s = x[0][1]
i,j = 0,1
while i<n and j<n:
if j<n and x[j][0] - x[i][0] < d:
s += x[j][1]
j+=1
else:
y.append(s)
s -= x[i][1]
i+=1
y.append(s)
print(max(y)) | 8 | PYTHON3 |
n, d = map(int, input().split())
v = sorted([list(map(int, input().split())) for i in range(n)], key = lambda x: x[0])
b = 0; s = sol = v[0][1]
for i in range(1, n):
while v[i][0] >= d + v[b][0]:
s -= v[b][1]
b += 1
s += v[i][1]
sol = max(sol, s)
print(sol)
| 8 | PYTHON3 |
def getKey(item):
return item[0]
n,d = map(int, input().split())
li = []
for _ in range(n):
l = [int(x) for x in input().split()]
li.append(l)
li = sorted(li, key=getKey)
mx = -1
sum = [0]*n
sum[0] = li[0][1]
for i in range(1, n):
sum[i] = sum[i-1]+li[i][1]
end = 0
for i in range(0, n):
tmp = end
for j in range(tmp, n):
if li[j][0] - li[i][0] < d:
end = j
else:
break
cnt = li[i][1]
if end > i and i == 0:
cnt = sum[end]
elif end > i:
cnt = sum[end] - sum[i-1]
if cnt > mx:
mx = cnt
print(mx)
| 8 | PYTHON3 |
user_input=input()
first_line=user_input.split(' ')
n=int(first_line[0])
d=int(first_line[1])
long_list=[]
for i in range(n):
extended_list=[]
user_input = input()
line = user_input.split(' ')
extended_list.extend([int(line[0]),int(line[1])])
long_list.append(extended_list)
long_list.sort()
index=0
maximum=0
for i in range(len(long_list)):
if maximum<long_list[i][1]:
maximum=long_list[i][1]
answer=maximum
maximum=0
i=0
count=0
looped="false"
while i in range(len(long_list)):
if long_list[i][0]-long_list[index][0]>d:
if count>0 and looped=="false":
i-=count
count=0
index=i
looped="true"
else:
index=i
maximum=0
if long_list[i][0]-long_list[index][0]<d:
count+=1
if maximum==0:
maximum=long_list[index][1]
else:
maximum+=long_list[i][1]
answer=max(answer,maximum)
i+=1
print(answer)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
int const N = 1e5 + 5;
using namespace std;
long long n, d, ans;
pair<long long, long long> p[N];
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
sort(p, p + n);
for (int i = 1; i < n; i++) p[i].second += p[i - 1].second;
for (int i = 0; i < n; i++) {
long long low = i, high = n - 1, mid, dif;
if (!i)
dif = 0;
else
dif = p[i - 1].second;
while (low <= high) {
mid = (low + high) / 2;
if (p[mid].first - p[i].first < d)
low = mid + 1, ans = max(ans, p[mid].second - dif);
else
high = mid - 1;
}
}
cout << ans;
return 0;
}
| 8 | CPP |
n,d=map(int,input().split())
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
a.sort()
p1=p2=0;ma=0;su=0
while p2<n:
if a[p2][0]-a[p1][0]<d:
su+=a[p2][1];p2+=1;ma=max(ma,su)
else:
su-=a[p1][1];p1+=1
print(ma) | 8 | PYTHON3 |
"""
Codeforces Round #321 (Div. 2)
Problem 580 B. Kefa and Company
@author yamaton
@date 2015-09-22
"""
import itertools as it
import functools
import operator
import collections
import math
import sys
import bisect
def solve(xs, d):
xs.sort()
ms, fs = list(zip(*xs))
n = len(ms)
fsacc = list(it.accumulate(it.chain((0,), fs)))
out = []
for i, money in enumerate(ms):
k = bisect.bisect_right(ms, money + d - 1)
# print_stderr('----------')
# print_stderr('i =', i)
# print_stderr('k =', k)
# print_stderr('d =', d)
# print_stderr('ms=', ms)
# print_stderr('ms[i] =', ms[i])
# print_stderr('ms[k-1] =', ms[k-1])
# print_stderr('fs[i] =', fs[i])
# print_stderr('fs[k-1] =', fs[k-1])
# out.append(sum(fs[i:k]))
out.append(fsacc[k] - fsacc[i])
if k == n:
break
return max(out)
def print_stderr(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def main():
# Assume the friendship factors are all positive
[n, d] = [int(i) for i in input().strip().split()]
money_friend_pairs = [tuple(int(i) for i in input().strip().split()) for _ in range(n)]
result = solve(money_friend_pairs, d)
print(result)
if __name__ == '__main__':
main()
| 8 | PYTHON3 |
n,d=map(int,input().split())
l=sorted(list(map(int,input().split())) for _ in range(n))
j=0
fl=0
m=0
for i in range(n):
fl+=l[i][1]
while (l[i][0]-l[j][0])>=d:
fl-=l[j][1]
j+=1
m=max(m,fl)
print(m)
| 8 | PYTHON3 |
'''
Created on 3Oct.,2017
@author: thorsten
'''
user = input().split(" ")
numberFriends = int(user[0])
diffToFeelPoor = int(user[1])
arr = []
maxFac = -1
for _ in range(numberFriends):
userIn = input().split(" ")
money = int(userIn[0])
frFac = int(userIn[1])
if frFac>maxFac:
maxFac = frFac
arr.append((money, frFac))
if diffToFeelPoor > 0:
arr.sort(key=lambda tup: tup[0])
currFac = arr[0][1]
maxFac = arr[0][1]
startIn = 0
index = 1
while index < len(arr):
if arr[index][0]<(arr[startIn][0]+diffToFeelPoor):
currFac += arr[index][1]
if currFac > maxFac:
maxFac = currFac
else:
currFac += arr[index][1]
for index2 in range(startIn, index+1):
if arr[index][0]>=(arr[index2][0]+diffToFeelPoor):
currFac -= arr[index2][1]
startIn = index2+1
else:
break
if currFac > maxFac:
maxFac = currFac
index += 1
if currFac > maxFac:
maxFac = currFac
print(maxFac)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
int const N = 1e5 + 5;
using namespace std;
long long n, d, ans, sum;
pair<long long, long long> p[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p, p + n);
for (int i = 1; i < n; i++) p[i].second += p[i - 1].second;
for (int i = 0; i < n; i++) {
long long low = i, high = n - 1, mid, dif;
if (!i)
dif = 0;
else
dif = p[i - 1].second;
while (low <= high) {
mid = (low + high) / 2;
bool yes = false;
if (p[mid].first - p[i].first < d) {
low = mid + 1;
ans = max(ans, p[mid].second - dif);
} else {
high = mid - 1;
}
}
}
cout << ans;
return 0;
}
| 8 | CPP |
def arr_2d(n):
return [[float(x) for x in stdin.readline().split()] for i in range(n)]
def get_col(arr, i):
return [row[i] for row in arr]
def arr_sum(arr):
arr.appendleft(0)
return list(accumulate(arr, lambda x, y: x + y))
from bisect import *
from sys import stdin
from itertools import accumulate
from collections import deque
n, d = map(int, stdin.readline().split())
a = deque(sorted(arr_2d(n), key=lambda x: x[0]))
col, ans, col2 = get_col(a, 0), 0, arr_sum(deque(get_col(a, 1)))
for i in range(n):
ix = bisect_right(col, a[i][0] + d - 1)
if ix == n or a[ix][0] > a[i][0] + d - 1:
ix -= 1
ans = max(ans, col2[ix + 1] - col2[i])
print(int(ans))
| 8 | PYTHON3 |
def solve(n,d,friends):
big = float('inf')
small = float('-inf')
friends.sort()
maxer = small
# two pointers
f = 0
s = 0
ind = 0
curr = 0
while(ind < n):
miner = friends[s][0]
while(s < n and abs(friends[ind][0] - friends[s][0]) >= d):
curr -= friends[s][1]
s += 1
miner = friends[s][0]
flag = True
while(ind < n and abs(friends[ind][0] - miner) < d):
curr += friends[ind][1]
ind += 1
flag = False
maxer = max(maxer,curr)
return maxer
def main():
# t = int(input())
# for i in range(t):
# n = int(input())
d = input()
d = [int(i) for i in d.split()]
a = d[0]
b = d[1]
friends = []
for i in range(a):
d = input()
d = [int(i) for i in d.split()]
m = d[0]
f = d[1]
friends.append((m,f))
# c = d[2]
# e = d[4]
# e = input()
# e = [int(i) for i in e.split()]
ans = solve(a,b,friends)
print(ans)
# for i in ans:
# print(i,end = "")
# print()
main()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
/*
#include<boost/multiprecision/cpp_int.hpp>
namespace mp=boost::multiprecision;
*/
using namespace std;
#define ll long long int
#define lo mp::cpp_int
#define pb push_back
#define pob pop_back
#define mk make_pair
#define endl "\n"
#define ce(x) cout << x <<"\n"
#define fill(s,v) memset(s,v,sizeof(s))
#define speed { ios_base::sync_with_stdio(false); cin.tie(0); }
#define deb(x) cout<<#x<<" "<<x<<endl;
#define INFINITE 1000000009
#define ull unsigned long long int
#define take(a,n) for(ll pp=0;pp<n;pp++) { cin>>a[pp]; }
#define take2(a,n) for(ll p=0;p<n;p++) { ll temp;cin>>temp;a.pb(temp); }
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,a,n) for(ll i=a;i<n;i++)
#define show(a,n) for(ll p=0;p<n;p++) cout<<a[p]<<" ";
#define _ <<" "<<
#define nl cout<<endl;
#define sq(x) ((x)*(x))
#define call(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ff first
#define ss second
#define lld long double
#define sc(n) scanf("%lld",&n);
#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;typedef pair<int, int> pii;
#define copy(b,a,n) for(ll i=0;i<n;i++) b[i]=a[i];
#define clock() clock_out()
#define tc(tt) \
ll tt; \
cin >> tt; \
for (int _tt = 0; _tt < tt; _tt++)
typedef vector<vector<ll>> matrix ;
typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;
const ll dx[4] = {-1, 0, 1, 0};
const ll dy[4] = {0, -1, 0, 1};
const ll mod = 1000000007;
ll gcd(ll a,ll b){if(!b)return a;return gcd(b,a%b);}
ll power_mod(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll modexpo(ll x,ll n,ll M){if(n==0)return 1;else if(n%2 == 0)return modexpo((x*x)%M,n/2,M);else return(x*modexpo((x*x)%M,(n-1)/2,M))%M;}
ll lcm(ll a, ll b){return (a*b)/gcd(a, b);}
ll sumofdigits(ll n){ll out=0;while(n){out+=(n%10);n/=10;}return out;}
bool isPrime(ll n){if (n <= 1)
return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false;
for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;return true;}
// ll power(ll a,ll b){if(b==0)return 1;if(b&1)return a*power(a,b-1);ll res=power(a,b/2);return res*res;}
ll bexpo(ll x,ll n){if(n==0)return 1;else if(n%2==0)return bexpo(x*x,n/2);else return x*bexpo(x*x,(n-1)/2);}
ll log(ll d){return log2(d);}
bool isPower(ll x, ll y){if(x==1) return (y==1); ll pow=1; while(pow<y) pow*=x; return(pow == y);}
bool isPerfectSquare(long double x){long double sr = sqrt(x);return ((sr - floor(sr)) == 0);}
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b) { return (a.second < b.second);}
ll isPalindrome(string s){ll l=0;ll h=s.size()-1;while(h>l){if(s[l++]!=s[h--])return 0;}return 1;}
ll setbits(ll n) { if(n==0)return 0; else return 1+setbits(n&(n-1)); }
ll modInverse(ll A,ll M){return modexpo(A,M-2,M);}
ll suma(ll *a,ll n){ll sum=0;for(ll i=0;i<n;i++)sum+=a[i];return sum;}
// void clock_out(){
// cerr<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
// }
//------modular inverse by extended euclid-------//
// ll d, x, y;
// void extendedEuclid(ll A, ll B) {
// if(B == 0) {
// d = A;
// x = 1;
// y = 0;
// }
// else {
// extendedEuclid(B, A%B);
// ll temp = x;
// x = y;
// y = temp - (A/B)*y;
// }
// }
// ll modInverse2(ll A, ll M){
// extendedEuclid(A,M);
// return (x%M+M)%M;
// }
//---------------------------------//
//---------------matrix exponentiation-------------------//
// matrix mul(matrix a,matrix b){
// matrix res(n+1,vector<ll>(n+1));
// f(i,n)f(j,n)f(k,n)res[i][j]=(res[i][j]%mod+((a[i][k]%mod)*(b[k][j]%mod))%mod)%mod;
// return res;
// }
// matrix pow(matrix A,ll p) {
// if(p==1) return A;
// matrix res=pow(A,p/2) ;
// res=mul(res,res);
// if(p&1) res=mul(res,A) ;
// return res;
// }
//-------------------------------------------------//
vector<ll> factorize(ll n) {
vector<ll> res;
for (ll i = 2; i * i <= n; ++i) {
while (n % i == 0) {
res.push_back(i);
n /= i;
}
}
if (n != 1) {
res.push_back(n);
}
return res;
}
vector<ll> divisors(ll n){
vector<ll> res;
for (ll i=1; i<=sqrt(n); i++){
if (n%i == 0){ if (n/i == i){ res.pb(i); } else{ res.pb(n/i); res.pb(i); } }
}
return res;
}
vector<ll> primeFactors(ll n){
if(n==1) return vector<ll>(1,1);
bool idx[1]={false};
vector<ll> res;
while (n%2 == 0){
if(!idx[1]){idx[1]=true;res.pb(2);}
n = n/2;
}
for (ll i=3;i*i<=n; i+=2){
bool once=false;
while (n%i==0){ if(!once){res.pb(i);once=true;} n=n/i; }
}
if (n > 2)res.pb(n);
return res;
}
//--------------------------------------------//
ll dxx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dyy[] = {0, 1, 1, 1, 0, -1, -1, -1};
//----check if a point is in range-----///
///__builtin_ctz(x) --> get power of 2 dude !!!!
bool check(ll x,ll y,ll n,ll m){return(x>=0&&x<n&&y>=0&&y<m);}
const int FULL_MASK = (1 << 10) - 1;
#define pi 3.1415926535897932
vector<vector<ll>> ed;
void bfs(ll s, vector<ll> &d) {
d[s] = 0;
queue<ll> q;
q.push(s);
while (!q.empty()) {
ll v = q.front();
q.pop();
for (auto to : ed[v]) {
if (d[to] == 1e9) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
}
// void dfs(int u){
// vis[u]=1;
// // temp.push_back(u);
// for(int i=0;i<adj[u].size();i++){
// if(!vis[adj[u][i]])
// dfs(adj[u][i]);
// }
// }
// void dfs(ll u){
// if(vis[u]) return;
// deg += g[u].size();
// ver++;
// vis[u] = 1;
// for(auto v: g[u])dfs(v);
// }
ll maxSubArraySum(ll a[],ll size)
{
ll max_so_far = a[0];
ll curr_max = a[0];
for (ll i = 1; i < size; i++)
{
curr_max = max(a[i], curr_max+a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
struct node{
ll a,b,c,d;
};
int n,m,fg,k=1;
char c;
char mmp[70][70];
int mp[70][70];
void dfs(int x,int y,int ex,int ey)
{
//cout<<k++<<endl;
if(fg){
return;
}
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>=0&&xx<n&&yy>=0&&m>yy&&mmp[xx][yy]==c)
{
if(xx==ex&&yy==ey) continue;
else if(mp[xx][yy]||fg){
fg=1;
return;
}
else if(!mp[xx][yy])
{
mp[xx][yy]=1;
dfs(xx,yy,x,y);
}
}
}
return;
}
void solve(){
ll n, m; cin >> n >> m;
pair < ll, ll > arr[n];
for(ll i = 0; i < n; i ++) cin >> arr[i].ff >> arr[i].ss;
sort(arr, arr + n);
ll mx = 0, s = 0;
ll r = 0;
for(ll l = 0; l < n; l ++){
while(r < n && arr[l].ff + m > arr[r].ff){
s += arr[r].ss; r ++;
}
mx = max(mx , s);
s -= arr[l].ss;
}
cout << mx << endl;
}
int main(){
// tc(tt){
solve();
// }
}
/*
*/ | 8 | CPP |
n, diff = map(int, input().split(" "))
m_f = []
for i in range(n):
m_f.append(tuple(map(int, input().split(" "))))
m_f.sort(key=lambda x: x[0])
start = 0
end = 1
max_f = m_f[0]
cost = m_f[start][1]
total = m_f[start][1]
while len(m_f) > end >= start:
if m_f[end][0] - m_f[start][0] >= diff:
total = max(total, cost)
cost -= m_f[start][1]
start += 1
else:
cost += m_f[end][1]
end += 1
total = max(total, cost)
print(total) | 8 | PYTHON3 |
n, m = tuple( int( x ) for x in input().split() )
v = [ ]
for i in range( n ):
i = input()
v.append( tuple( int( x ) for x in i.split() ) )
v.sort( reverse = True )
maxi = val = v[ 0 ][ 1 ]
i = j = 0
for j in range( 1, len( v ) ):
val += v[ j ][ 1 ]
while abs( v[ j ][ 0 ] - v[ i ][ 0 ] ) >= m:
val -= v[ i ][ 1 ]
i += 1
if val > maxi:
maxi = val
print( maxi )
| 8 | PYTHON3 |
n, d = map(int, input().split())
al = [list(map(int, input().split())) for _ in range(n)]
al.sort()
res = al[0][1]
t = al[0][1]
idx = 0
for i in range(1, n):
if al[i][0] - al[idx][0] < d:
t += al[i][1]
else:
while al[i][0] - al[idx][0] >= d:
t -= al[idx][1]
idx += 1
t += al[i][1]
res = max(res, t);
print(res)
| 8 | PYTHON3 |
first_line = input().split()
n = int(first_line[0])
d = int(first_line[1])
friends = [[0, 0] for i in range(n)]
for i in range(n):
a_friend = input().split()
friends[i][0] = int(a_friend[0])
friends[i][1] = int(a_friend[1])
friends = sorted(friends)
left = 0
right = 0
result = 0
addition = 0
while(left < n and right < n):
while(right < n and ((friends[right][0]-friends[left][0]) < d)):
addition += friends[right][1]
right += 1
result = max(result, addition)
addition -= friends[left][1]
left += 1
print(result)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 1000005;
int N;
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
pair<long long, long long> p[MX];
long long pref[MX];
bool cmp(pair<long long, long long>& p1, pair<long long, long long>& p2) {
if (p1.first == p2.first) return p1.second > p2.second;
return p1.first < p2.first;
}
int ch(int x, int aim, int d) {
if (abs(p[aim].first - p[x].first) < d) return 2;
return 0;
}
long long Ra(int l, int r) {
long long las = min(r, N - 1);
if (l < 1) return pref[las];
return pref[las] - pref[l - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long d;
cin >> N >> d;
for (int i = 0; i < N; i++) cin >> p[i].first >> p[i].second;
sort(p, p + N, cmp);
for (int i = 0; i < N; i++) {
if (i) pref[i] = pref[i - 1];
pref[i] += p[i].second;
}
long long ans = 0;
for (int i = 0; i < N; i++) {
long long l = i, m, h = N - 1, res = -1e18;
while (l <= h) {
m = l + h;
m /= 2;
if (ch(i, m, d)) {
res = max(res, Ra(i, m));
l = m + 1;
} else
h = m - 1;
}
ans = max(ans, res);
}
cout << ans;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long A = 100000000000000LL, N = 228228;
char e[21];
vector<string> ot;
pair<int, pair<int, string> > a[N];
long long o, p[3] = {-1, -1, -1};
int i, j, l, r, n, m;
int main() {
cin >> n >> m;
for (i = 0; i < n; ++i) {
scanf("%d%d", &a[i].first, &a[i].second.first);
a[i].second.second = e;
}
sort(a, a + n);
for (l = 0; l < n; o -= a[l].second.first, ++l) {
while (r < n && abs(a[l].first - a[r].first) < m)
o += a[r].second.first, ++r;
if (o >= p[0]) p[0] = o, p[1] = l, p[2] = r;
}
for (i = p[1]; i < p[2]; ++i) ot.push_back(a[i].second.second);
cout << p[0] << "\n";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int z = 1;
while (z--) {
int n, d;
cin >> n >> d;
vector<pair<int, int>> friends;
int arr[n];
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
arr[i] = a;
friends.push_back(make_pair(a, b));
}
sort(friends.begin(), friends.end());
sort(arr, arr + n);
long long int prefix[n];
prefix[0] = friends[0].second;
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + friends[i].second;
}
long long int maxx = 0;
for (int i = 0; i < n; i++) {
int pos = upper_bound(arr + i, arr + n, arr[i] + d - 1) - arr;
maxx = max(maxx, prefix[pos - 1] - (i ? prefix[i - 1] : 0));
}
cout << maxx << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class X>
class vec {
public:
X x, y;
vec<X>(X a = 0, X b = 0) { x = a, y = b; }
X dis(vec a) { return (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y); }
vec<X> operator-(vec<X> b) { return vec<X>(x - b.x, y - b.y); }
X operator*(vec<X> b) { return x * b.y - y * b.x; }
};
void sf(int &x) { scanf("%d", &x); }
void sf(long long &x) { scanf("%lld", &x); }
void sf(long long &x, long long &y) { scanf("%lld%lld", &x, &y); }
void sf(float &x) { scanf("%f", &x); }
void sf(double &x) { scanf("%lf", &x); }
void sf(int &x, int &y) { scanf("%d%d", &x, &y); }
void sf(float &x, float &y) { scanf("%f%f", &x, &y); }
void sf(double &x, double &y) { scanf("%lf%lf", &x, &y); }
void sf(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); }
void sf(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
void sf(long long &x, long long &y, long long &z) {
scanf("%lld%lld%lld", &x, &y, &z);
}
void sf(float &x, float &y, float &z) { scanf("%u%u%u", &x, &y, &z); }
void sf(char &x) { x = getchar(); }
void sf(char *s) { scanf("%s", s); }
void sf(string &s) { cin >> s; }
void sf(vec<int> &x) {
int a, b;
sf(a, b);
x = vec<int>(a, b);
}
void pf(int x) { printf("%d\n", x); }
void pf(int x, int y) { printf("%d %d\n", x, y); }
void pf(int x, int y, int z) { printf("%d %d %d\n", x, y, z); }
void pf(long long x) { printf("%lld\n", x); }
void pf(long long x, long long y) { printf("%lld %lld\n", x, y); }
void pf(long long x, long long y, long long z) {
printf("%lld %lld %lld\n", x, y, z);
}
void pf(float x) { printf("%u\n", x); }
void pf(double x) { printf("%.9lf\n", x); }
void pf(double x, double y) { printf("%.5lf %.5lf\n", x, y); }
void pf(char x) { printf("%c\n", x); }
void pf(char *x) { printf("%s\n", x); }
void pf(string x) {
cout << x;
puts("");
}
void pf(vec<int> x) { printf("%d %d\n", x.x, x.y); }
long long STN(string s) {
long long sm;
stringstream ss(s);
ss >> sm;
return sm;
}
template <class T>
T bigmod(T b, T p, T m) {
if (p == 0) return 1 % m;
T x = b;
T ans = 1;
while (p) {
if (p & 1) ans = (ans * x) % m;
p >>= 1;
x = (x * x) % m;
}
return ans;
}
template <class T>
T gcd(T x, T y) {
if (y == 0) return x;
return gcd(y, x % y);
}
template <typename T>
T POW(T b, T p) {
if (p == 0) return 1;
if (p == 1) return b;
if (p % 2 == 0) {
T s = POW(b, p / 2);
return s * s;
}
return b * POW(b, p - 1);
}
template <typename T>
T modinv(T num, T m) {
return bigmod(num, m - 2, m);
}
template <class T>
string NTS(T Number) {
stringstream ss;
ss << Number;
return ss.str();
}
long long n, d, c[100009];
vector<pair<long long, long long> > v;
int main() {
v.push_back(make_pair(-1, -1));
sf(n, d);
for (int i = 0; i < n; i++) {
long long a, b;
sf(a, b);
v.push_back(make_pair(a, b));
}
sort(v.begin(), v.end());
for (int i = 1; i <= n; i++) {
c[i] = c[i - 1] + v[i].second;
}
long long an = 0;
for (int i = 1; i <= n; i++) {
long long hi = n, lo = i, a = v[i].first + d;
while (hi > lo) {
long long mid = (hi + lo + 1) / 2;
if (v[mid].first < a)
lo = mid;
else
hi = mid - 1;
}
long long kk = c[lo] - c[i - 1];
an = max(an, kk);
}
pf(an);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int T;
double pi = acos(-1.0);
long long factorial(long long x) {
long long total = 1;
for (int i = 2; i <= x; i++) total *= i;
return total;
}
bool isprime(long long x) {
vector<bool> prime(x + 1, 1);
prime[0] = prime[1] = 0;
for (long long i = 2; i * i <= x; i++)
if (prime[i])
for (long long j = i * i; j <= x; j += i) prime[j] = 0;
return prime[x];
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long temp = 1;
long long fastpow(long long base, long long exponent) {
if (exponent == 0) return 1;
if (exponent == 1) return (temp * base);
if (exponent % 2 == 0)
return fastpow(base * base, exponent / 2);
else {
temp *= base;
fastpow(base * base, exponent / 2);
}
}
long fib(long n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 2) + fib(n - 1);
}
void solve() {
int n, diff;
cin >> n >> diff;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v[i] = make_pair(x, y);
}
sort(v.begin(), v.end());
long long l = 0, r = 1, sum = v[0].second, ans = sum;
while (r < n) {
if (abs(v[r].first - v[l].first) < diff) {
sum += v[r].second;
ans = max(sum, ans);
r++;
} else {
sum -= v[l].second;
l++;
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
{ solve(); }
return 0;
}
| 8 | CPP |
n, d = [int(x) for x in input().split()]
prieteni = []
for i in range(n):
pr = [int(x) for x in input().split()]
prieteni += [pr]
prieteni.sort(key=lambda x : x[0])
l = 0
r = 0
final = prieteni[l][1]
max = 0
while r < n:
if l == r:
final = prieteni[l][1]
r += 1
if r == n:
if final > max:
max = final
break
if prieteni[r][0] - prieteni[l][0] < d:
final += prieteni[r][1]
r += 1
if r == n:
if final > max:
max = final
else:
if final > max:
max = final
l += 1
final = final - prieteni[l-1][1]
print(max) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(int n, pair<int, int> const& p) {
return ((p.first < n) && (p.second < n));
}
int main() {
int n, d;
cin >> n >> d;
pair<long long, long long> a[n + 1];
long long b[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i].first;
b[i] = a[i].first;
cin >> a[i].second;
}
sort(b, b + n);
sort(a, a + n);
long long c[n + 2];
c[0] = 0;
for (int i = 1; i <= n; i++) {
c[i] = c[i - 1] + a[i - 1].second;
}
long long mx = 0;
for (long long i = 0; i < n; i++) {
long long sum = 0;
long long j = upper_bound(b, b + n, a[i].first + d - 1) - b;
sum = c[j] - c[i];
mx = max(mx, sum);
}
cout << mx << '\n';
}
| 8 | CPP |
n,d=map(int,input().split())
a=[]
for i in range(n):
x,y=map(int,input().split())
a.append([x,y])
a.sort()
partialsum=[0]*n
partialsum[0]=a[0][1]
for i in range(1,n):
partialsum[i]=partialsum[i-1]+a[i][1]
mx=partialsum[0]
cnt=0
for i in range(1,n):
while cnt<i and a[i][0]-a[cnt][0]>=d:
cnt+=1
if cnt>0:
mx=max(mx,partialsum[i]-partialsum[cnt-1])
else:
mx=max(mx,partialsum[i])
print(mx)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n, d, a, b;
cin >> n >> d;
vector<pair<int, int> > V;
for (int i = 0; i < n; i++) {
cin >> a >> b;
V.push_back(pair<int, int>(a, b));
}
sort(V.begin(), V.end());
long long int sum = 0, r_sum = 0;
for (a = 0, b = 0; a < n; a++) {
while (V[a].first + d > V[b].first && b < n) r_sum += V[b].second, b++;
sum = max(sum, r_sum);
r_sum -= V[a].second;
}
cout << sum << "\n";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
void __print(long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
long long f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void solve() {
long long n, d;
cin >> n >> d;
vector<pair<long long, long long> > a(n + 1);
for (long long i = 0; i < (n); i++) {
long long m, f;
cin >> m >> f;
a[i + 1] = {m, f};
}
sort(a.begin()++, a.end()++);
long long l = 1;
long long r = 2;
long long ans = a[1].second;
long long curr = ans;
while (r <= n) {
while (r <= n and a[r].first - a[l].first < d) {
curr += a[r].second;
r++;
}
ans = max(curr, ans);
curr -= a[l].second;
l++;
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
struct prieten {
int money;
int friendship;
} v[100001];
int part_Hoare(int s, int d) {
int i, j;
i = s - 1;
j = d + 1;
int randpoz, r1, r2, r3;
int nr = d - s + 1;
r1 = rand() % nr + s;
r2 = rand() % nr + s;
r3 = rand() % nr + s;
int minr, maxr;
minr = min(min(r1, r2), min(r2, r3));
maxr = max(max(r1, r2), max(r2, r3));
randpoz = r1 + r2 + r3 - minr - maxr;
int pivot = v[randpoz].money;
while (true) {
do {
++i;
} while (v[i].money < pivot);
do {
--j;
} while (v[j].money > pivot);
if (i >= j) return j;
prieten aux;
aux.money = v[i].money;
aux.friendship = v[i].friendship;
v[i].money = v[j].money;
v[i].friendship = v[j].friendship;
v[j].money = aux.money;
v[j].friendship = aux.friendship;
}
}
void qsort(int i, int j) {
if (i < j) {
int k = part_Hoare(i, j);
qsort(i, k);
qsort(k + 1, j);
}
}
int caut_bin(int st, int dr, int x, int dif) {
int m = (st + dr) / 2;
if (st >= dr) {
if ((v[m].money - x) <= dif)
return st;
else
return st - 1;
}
if ((v[m].money - x) > dif)
return caut_bin(st, m - 1, x, dif);
else
return caut_bin(m + 1, dr, x, dif);
}
int main() {
int n, d, i, j, k;
long long smax = 0;
cin >> n >> d;
for (i = 1; i <= n; i++) cin >> v[i].money >> v[i].friendship;
qsort(1, n);
if ((v[2].money - v[1].money) < d) {
j = caut_bin(2, n, v[i].money, d - 1);
for (i = 1; i <= j; i++) smax += v[i].friendship;
} else if (v[1].friendship > smax) {
smax = v[1].friendship;
j = 1;
}
i = 1;
j++;
long long spart = smax;
while (j < n) {
while (((v[j].money - v[i].money) >= d) && (i < j)) {
spart -= v[i].friendship;
i++;
}
while (((v[j].money - v[i].money) < d) && (j <= n)) {
spart += v[j].friendship;
j++;
}
if (spart > smax) smax = spart;
}
if (v[n].friendship > smax) smax = v[n].friendship;
cout << smax;
return 0;
}
| 8 | CPP |
import sys
input = lambda: sys.stdin.readline().strip("\r\n")
n, d = map(int, input().split())
ls = []
for _ in range(n):
m, s = map(int, input().split())
ls.append((m, s))
ls.sort()
alpha = 0
maxi = 0
j = 0
for i in range(n):
alpha += ls[i][1]
while ls[i][0] - ls[j][0] >= d:
alpha -= ls[j][1]
j += 1
maxi = max(maxi, alpha)
print(maxi)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, d;
cin >> n >> d;
vector<pair<long long int, long long int>> friends(n);
vector<long long int> prefix(n);
vector<long long int> results;
results.reserve(n);
for (auto&& _friend : friends) cin >> _friend.first >> _friend.second;
sort(friends.begin(), friends.end());
prefix[0] = friends[0].second;
for (long long int i = 1; i < n; i++)
prefix[i] = prefix[i - 1] + friends[i].second;
for (long long int i = 0; i < n; i++) {
long long int l_min = i, r_max;
long long int l = i, r = n - 1;
while (r >= l) {
long long int m = (l + r) / 2;
if (friends[m].first >= friends[i].first + d) {
r = m - 1;
} else {
l = m + 1;
}
}
r_max = l - 1;
if (l_min == 0)
results.push_back(prefix[r_max]);
else
results.push_back(prefix[r_max] - prefix[l_min - 1]);
}
long long int _max = -1;
for (auto&& result : results)
if (result > _max) _max = result;
cout << _max;
return 0;
}
| 8 | CPP |
n,k=map(int,input().split())
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
l.sort()
su=l[0][1]
ma=0
first,last=0,1
while first<n and last<n:
if l[last][0]-l[first][0]>=k:
if su>ma:
ma=su
su-=l[first][1]
first+=1
else:
su+=l[last][1]
last+=1
if su>ma:
ma=su
print(ma) | 8 | PYTHON3 |
import sys
from bisect import bisect_right
def main():
n, d = map(int, input().split())
friends = sorted(tuple(int(c) for c in line.split()) for line in sys.stdin.readlines())
money = [e[0] for e in friends]
friendship_sum = [0]
for _, e in friends:
friendship_sum.append(friendship_sum[-1] + e)
start = 0
_min, ans = friends[0]
for i in range(1, n):
cur = friends[i][0]
if cur - _min >= d:
start = bisect_right(money, cur - d)
_min = friends[start][0]
friendship = friendship_sum[i + 1] - friendship_sum[start]
ans = max(ans, friendship)
print(ans)
if __name__ == "__main__":
main()
| 8 | PYTHON3 |
n,d=map(int,input().split())
a=[]
for i in range(n):
x=list(map(int,input().split()))
a.append(x)
a.sort()
i=0
mx=0
x=a[i][0]
s=a[i][1]
j=1
while j!=n:
if a[j][0]-x<d:
s+=a[j][1]
mx=max(s,mx)
j+=1
else:
mx=max(s,mx)
s-=a[i][1]
i+=1
x=a[i][0]
print(max(mx,s)) | 8 | PYTHON3 |
from collections import namedtuple
from operator import itemgetter
Friend = namedtuple("Friend", "m s")
n, d = map(int, input().split())
f = [ ]
for i in range(n):
f.append(Friend(*map(int, input().split())))
f.sort(key=itemgetter(0))
left = 0
cur = f[0].s
result = cur
for i, fr in enumerate(f[1:], 1):
while left < i and f[left].m + d <= fr.m:
cur -= f[left].s
left += 1
cur += fr.s
result = max(result, cur)
print(result)
| 8 | PYTHON3 |
import sys
import math
import collections
import bisect
from collections import deque as queue
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()
for t in range(1):
n,m=get_ints()
ans=[]
for i in range(n):
ans.append(get_list())
ans.sort()
start=0
end=0
maxim=0
val=0
while end<n and start<n:
if ans[end][0]-ans[start][0]<m:
val+=ans[end][1]
end+=1
else:
val-=ans[start][1]
start+=1
maxim=max(val,maxim)
print(maxim) | 8 | PYTHON3 |
def get_max_friendship_factor(friends, min_money_dif):
friends.sort()
max_friendship_factor = 0
right = 0
friendship_factor = 0
for left in range(0, len(friends)):
while right < len(friends) and abs(friends[left][0] - friends[right][0]) < min_money_dif:
friendship_factor += friends[right][1]
right += 1
max_friendship_factor = max(max_friendship_factor, friendship_factor)
friendship_factor -= friends[left][1]
return max_friendship_factor
def solve():
first_line = input()
num_friends, min_money_dif = [int(num) for num in first_line.split()]
friends = []
for _ in range(0, num_friends):
line = input()
friend = tuple([int(num) for num in line.split()])
friends.append(friend)
max_friendship_factor = get_max_friendship_factor(friends, min_money_dif)
print(max_friendship_factor)
if __name__ == '__main__':
solve()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long arr[100009];
template <class T>
inline void rd(T &i) {
bool minus = false;
char c;
for (c = getchar(); (c < '0' || c > '9') && (c != '-'); c = getchar())
;
if (c == '-') {
minus = true;
c = '0';
}
for (i = 0; c >= '0' && c <= '9'; c = getchar())
i = (i << 3) + (i << 1) + (c - 48);
if (minus) i = (~i) + 1;
}
bool cmp(const pair<int, int> &l, const pair<int, int> &r) {
return l.first < r.first;
}
int main() {
vector<pair<int, int> > v;
int n, d, a, b;
long long sum;
pair<int, int> p;
rd(n);
rd(d);
for (int i = 0; i < n; i++) {
rd(a);
rd(b);
p = make_pair(a, b);
v.push_back(p);
}
sort(v.begin(), v.end());
arr[0] = v[0].second;
for (int i = 1; i < n; i++) arr[i] = v[i].second + arr[i - 1];
long long mx = 0;
for (int i = 0; i < n; i++) {
int ind = lower_bound(v.begin(), v.end(), pair<int, int>(v[i].first + d, 0),
cmp) -
v.begin() - 1;
if (i == 0)
sum = arr[ind];
else
sum = arr[ind] - arr[i - 1];
if (sum > mx) mx = sum;
}
cout << mx << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, d;
cin >> n >> d;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
long long mx = 0, val = 0;
int low = 0, high = 0;
while (high < n) {
if ((v[high].first - v[low].first) < d) {
val += v[high].second;
mx = max(mx, val);
high++;
} else {
val -= v[low].second;
low++;
}
}
cout << mx << endl;
}
int main() { solve(); }
| 8 | CPP |
n, d = list(map(int, input().split()))
persons = []
for i in range(n):
m, s = list(map(int, input().split()))
persons.append((m, s))
persons.sort()
max_s = 0
cur_s = persons[0][1]
good_persons = [persons[0]]
for i in range(1, n):
if abs(good_persons[0][0] - persons[i][0]) < d:
good_persons.append(persons[i])
cur_s += persons[i][1]
else:
j = 0
rec_s = 0
while (j < len(good_persons)) and (abs((good_persons[j][0] - persons[i][0])) >= d):
rec_s += good_persons[j][1]
j += 1
if rec_s <= persons[i][1]:
cur_s = cur_s - rec_s + persons[i][1]
good_persons = good_persons[j:] + [persons[i]]
else:
max_s = max(max_s, cur_s)
cur_s = cur_s - rec_s + persons[i][1]
good_persons = good_persons[j:] + [persons[i]]
print(max(cur_s, max_s))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> vt[100001];
int main() {
int n, d;
long long maxy = 0;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
vt[i] = make_pair(a, b);
}
sort(vt, vt + n);
for (int i = 0; i < n; i++) {
if (i > 0) vt[i].second += vt[i - 1].second;
}
for (int i = 0; i < n; i++) {
long long sum = 0;
int l = i, r = n - 1, ans;
while (l <= r) {
int mid = (l + r) >> 1;
if ((vt[mid].first - vt[i].first) >= d) {
r = mid - 1;
} else {
ans = mid;
l = mid + 1;
}
}
if (i > 0)
sum = vt[ans].second - vt[i - 1].second;
else
sum = vt[ans].second;
maxy = max(maxy, sum);
}
cout << maxy << endl;
}
| 8 | CPP |
def main():
def solve(arr):
arr.sort(key = lambda x: x[0])
l = 0
s = 0
max_ = 0
for i in range(len(arr)):
s += arr[i][1]
while arr[i][0] - arr[l][0] >= d:
s -= arr[l][1]
l += 1
max_ = max(max_,s)
return max_
q = []
n,d = list(map(lambda x: int(x),input().split()))
for i in range(n):
money,ff = list(map(lambda x: int(x),input().split()))
q.append((money,ff))
print(solve(q))
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# My magical way of doing recursion in python. This
# isn't the fastest, but at least it works.
from types import GeneratorType
def bootstrap(func, stack=[]):
def wrapped_function(*args, **kwargs):
if stack:
return func(*args, **kwargs)
else:
call = func(*args, **kwargs)
while True:
if type(call) is GeneratorType:
stack.append(call)
call = next(call)
else:
stack.pop()
if not stack:
break
call = stack[-1].send(call)
return call
return wrapped_function
if __name__== "__main__":
main() | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long m;
long long f;
};
bool comp(node a, node b) { return a.m < b.m; }
int main() {
long long n;
long long d;
cin >> n;
cin >> d;
int pt1, pt2;
node a[n];
for (int i = 0; i < n; i++) {
cin >> a[i].m;
cin >> a[i].f;
}
sort(a, a + n, comp);
int i = 0, j = 1;
long long sum = a[0].f;
long long curr = a[0].f;
while (j < n) {
if (a[j].m - a[i].m < d) {
curr += a[j].f;
j++;
} else {
if (curr > sum) sum = curr;
curr -= a[i].f;
i++;
}
}
if (curr > sum) sum = curr;
cout << sum << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 1;
long long ans = -1e18, n, d, pre[N], tmp = 0;
vector<pair<long long, long long>> v;
int main() {
cin >> n >> d;
long long x, y;
v.push_back({-1, -1});
for (int i = 1; i <= n; i++) {
cin >> x >> y;
v.push_back({x, y});
}
sort(v.begin(), v.end());
pre[0] = 0;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + v[i].second;
long long i, j = 1;
for (i = 1; i <= n; i++) {
while (v[i].first - v[j].first >= d) {
j++;
}
tmp = pre[i] - pre[j - 1];
ans = max(ans, tmp);
}
cout << ans;
}
| 8 | CPP |
def main():
n,d = [int(x) for x in input().split()]
preffac = [0]
friends = []
for i in range(n):
m,s = [int(x) for x in input().split()]
friends.append((m,s))
friends.sort()
kon = -1
wyn = 0
ob = 0
for pocz in range(n):
if kon < pocz:
kon = pocz
ob = friends[pocz][1]
while kon+1 < n and (friends[kon+1][0] - friends[pocz][0]) < d:
ob += friends[kon+1][1]
kon += 1
wyn = max(wyn, ob)
ob -= friends[pocz][1]
print(wyn)
main()
| 8 | PYTHON3 |
n,k=map(int,input().split())
f,v=[],[]
d={}
for _ in range(n):
a,b=map(int,input().split())
if a in d:
temp=d[a]
temp.append(b)
else:
d[a]=[b]
for i in sorted(d):
v.extend([i]*len(d[i]))
f.extend(sorted(d[i]))
start,end=0,1
m=max(f)
pre,temp=[],0
for i in f:
temp+=i
pre.append(temp)
while(start<n and end<n):
if v[end]-v[start]>=k:
start+=1
else:
if start==0:
m=max(m,pre[end])
else:
m=max(m,pre[end]-pre[start-1])
end+=1
if start==0:
m=max(m,pre[end-1])
else:
m=max(m,pre[end-1]-pre[start-1])
print(m)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long A = 100000000000000LL, N = 228228;
char e[21];
vector<string> ot;
pair<int, pair<int, string> > a[N];
long long o, p[3] = {-1, -1, -1};
int i, j, l, r, n, m;
int main() {
cin >> n >> m;
for (i = 0; i < n; ++i) {
scanf("%d%d", &a[i].first, &a[i].second.first);
a[i].second.second = e;
}
sort(a, a + n);
for (l = 0; l < n; o -= a[l].second.first, ++l) {
while (r < n && abs(a[l].first - a[r].first) < m)
o += a[r].second.first, ++r;
if (o >= p[0]) p[0] = o, p[1] = l, p[2] = r;
}
for (i = p[1]; i < p[2]; ++i) ot.push_back(a[i].second.second);
cout << p[0] << "\n";
}
| 8 | CPP |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
n, d = map(int,input().split())
F = []
for i in range(n):
F.append(list(map(int,input().split())))
F = sorted(F, key = lambda x:x[0])
f_factor = 0
j = 0
ans = -1
for i in range(n):
if i != 0:
f_factor -= F[i-1][1]
while j < n:
if F[j][0] - F[i][0] >= d:
break
f_factor += F[j][1]
j+=1
ans = max(ans, f_factor)
print(ans)
| 8 | PYTHON3 |
n,d = map(int,input().split())
li = []
for i in range(n):
li.append(list(map(int,input().split())))
li = sorted(li)
su = [0] * n
su[0] = li[0][1]
for i in range(1,n):
su[i] = su[i - 1] + li[i][1]
li = [x[0] for x in li]
po = 0
ma = 0
for i in range(n):
while((po < n-1) and (li[po+1] - li[i] < d)):
po = po + 1
if(i == 0):
ma = max(ma,su[po])
else:
ma = max(ma,su[po] - su[i-1])
print(ma) | 8 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long int MOD = 1000000007;
double eps = 1e-12;
long long int d;
vector<pair<long long int, long long int> > v;
vector<long long int> pref_fri(100001, 0);
long long int exp(long long int a, long long int b) {
if (b == 0)
return 1;
else if (b == 1)
return a;
else {
long long int x = exp(a, b / 2);
long long int ans = ((x % MOD) * (x % MOD)) % MOD;
if (b % 2 == 1) {
ans *= (a % MOD);
ans %= MOD;
}
return ans;
}
}
long long int bs(int t, int hi) {
int lo = 0;
while (lo != hi) {
int mid = (hi + lo) / 2;
if (v[t].first - v[mid].first < d)
hi = mid;
else
lo = mid + 1;
}
if (v[t].first - v[lo].first < d && lo != 0) {
return pref_fri[t] - pref_fri[lo - 1];
} else if (v[t].first - v[lo].first < d && lo == 0) {
return pref_fri[t];
}
return -1;
}
void solve() {
int n;
cin >> n >> d;
for (int i = 0; i < n; i++) {
long long int a, b;
cin >> a >> b;
v.push_back(make_pair(a, b));
}
sort((v).begin(), (v).end());
pref_fri[0] = v[0].second;
for (int i = 1; i < n; i++) pref_fri[i] = v[i].second + pref_fri[i - 1];
long long int ans = v[0].second;
for (int i = 1; i < n; i++) {
long long int var = bs(i, i);
ans = max(ans, var);
}
cout << ans << "\n";
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
| 8 | CPP |
N, M = map(int, input().split())
values = []
for i in range(N):
m, f = input().split()
values.append((int(m), int(f)))
values.sort()
r = 0
l = 0
mx = 0
sum =0
while (r < N):
dif = values[r][0] - values[l][0]
if dif < M:
sum = sum + values[r][1]
mx = max(mx, sum)
r += 1
else:
sum -= values[l][1]
l += 1
print(mx) | 8 | PYTHON3 |
#include <bits/stdc++.h>
const int MAXN = 200001;
struct People {
long long m, s;
bool operator<(const People &p) const { return m < p.m; }
} p[MAXN];
int n;
long long d, s[MAXN];
int main() {
scanf("%d%I64d", &n, &d);
d--;
for (int i = 1; i <= n; i++) {
scanf("%I64d%I64d", &p[i].m, &p[i].s);
}
std::sort(p + 1, p + n + 1);
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + p[i].s;
}
long long answer = 0;
for (int i = 1; i <= n; i++) {
int pos = std::upper_bound(p + 1, p + n + 1, (People){p[i].m + d}) - p - 1;
answer = std::max(answer, s[pos] - s[i - 1]);
}
printf("%I64d\n", answer);
return 0;
}
| 8 | CPP |
#In the name of Allah
from sys import stdin, stdout
input = stdin.readline
n, h = map(int, input().split())
f = sorted([list(map(int, input().split())) for i in range(n)])
ans = 0
fc = f[0][1]
start = 0
for i in range(1, n):
while f[i][0] - f[start][0] >= h:
ans = max(ans, fc)
fc -= f[start][1]
start += 1
#print(f[start],fc, i)
fc += f[i][1]
ans = max(ans, fc)
#print(fc, ans)
stdout.write(str(max(ans, fc)))
| 8 | PYTHON3 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.