solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
n,d = map(int,input().split())
l = []
for i in range(n):
a,b = map(int,input().split())
l.append([a,b])
l.sort()
ans = l[0][1]
c = l[0][1]
i = 0
j = 1
while j<n:
if l[j][0]-l[i][0]<d:
c+=l[j][1]
j+=1
else:
c-=l[i][1]
i+=1
ans = max(ans,c)
print(ans)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, d, i;
cin >> n >> d;
vector<pair<long long int, long long int> > a(n);
for (i = 0; i < (n); i++) cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end());
long long int cnt = 0;
long long int ans = 0;
long long int left, right;
right = 0;
for (left = 0; left < n; left++) {
while (right < n && a[right].first - a[left].first < d) {
cnt += a[right].second;
right++;
}
ans = max(ans, cnt);
cnt -= a[left].second;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
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.insert(0, 0)
return list(accumulate(arr, lambda x, y: x + y))
from bisect import *
from sys import stdin
from itertools import accumulate
n, d = map(int, stdin.readline().split())
a = sorted(arr_2d(n), key=lambda x: x[0])
col, ans, col2 = get_col(a, 0), 0, arr_sum(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 |
n, d = map(int, input().split())
#
#
# def binsearch(nums, target):
# left = 0
# right = len(nums) - 1
# while left <= right:
# mid = (left + right) // 2
# if nums[mid] == target:
# return mid
# elif nums[mid] > target:
# right = mid - 1
# elif nums[mid] < target:
# left = mid + 1
#
# return left
friends = [[0, 0]]
for i in range(n):
m, f = map(int, input().split())
friends.append([m, f])
friends.sort()
#print(friends)
friendship = [0]
monies = []
for i in range(1, n+1):
friendship.append(friendship[i-1] + friends[i][1])
#print(friendship)
ans = 0; j = 0; diff = 0
for i in range(n+1):
diff = friends[i][0] - friends[j][0]
if diff < d:
ans = max(friendship[i] - friendship[j], ans)
else:
while diff >= d:
j += 1
diff = friends[i][0] - friends[j][0]
j -= 1
ans = max(ans, friendship[i] - friendship[j])
print(ans)
# for i in range(n):
# monies.append(friends[i][0])
#
# print(friendship, monies)
# res = []
# for i in friends:
# left = binsearch(monies, i[0]-d+.1)
# right = binsearch(monies, i[0]+d-.1)
# print(left, right, i)
# res.append(friendship[right] - friendship[left])
# print(res)
# print(max(res)) | 8 | PYTHON3 |
n, d = [int(x) for x in input().split()]
li = []
for i in range(n):
l = list(map(int, input().split()))
li.append(l)
li.sort()
lii = []
b = 0
for i in range(n):
if i == 0:
num = 0
else:
num = num - li[i - 1][1]
a = 0
for j in range(b, n):
if li[j][0] < li[i][0] + d:
num += li[j][1]
else:
a = 1
b = j
break
if a == 0:
b = n
lii.append(num)
print(max(lii)) | 8 | PYTHON3 |
#maximum you never know
n , d= input().split()
n = int(n)
d = int(d)
list1 =[]
j = 0
for i in range(n):
list1.append([int(a) for a in input().split()])
list1.sort()
list2 = []
m1 = summ = j= 0
for i in range(len(list1)):
summ += list1[i][1]
while(list1[i][0] - list1[j][0] >=d):
summ-= list1[j][1]
j+=1
m1 = max(summ,m1)
#print(list2)
print(m1)
#print(list1) | 8 | PYTHON3 |
# key missing ""
n, d = [int(i) for i in input().split()]
lis = []
for i in range(n):
x, y = [int(i) for i in input().split()]
lis.append([x, y])
lis.sort()
pre_sum = [0]*n
pre_sum[0] = lis[0][1]
for i in range(1, n):
pre_sum[i] = pre_sum[i-1]+lis[i][1]
start = end = 0
ans = 0
#print(lis)
#print(pre_sum)
while end < n:
if (lis[end][0] - lis[start][0]) < d:
if start == 0:
ans = max(ans, pre_sum[end])
else:
ans = max(ans,pre_sum[end] - pre_sum[start-1])
end += 1
else:
start += 1
print(ans)
| 8 | PYTHON3 |
lst=[]
n,d=map(int,input().split())
for i in range(n):
mi,si = map(int,input().split())
lst.append([mi,si])
lst.sort()
f=0;l=0;ans=0;a=0
while l<n :
if lst[l][0]-lst[f][0]<d:
a+=lst[l][1]
l+=1
else:
if a > ans:
ans=a
a-=lst[f][1]
f=f+1
print(max(ans,a))
| 8 | PYTHON3 |
from sys import stdin, stdout
lines = stdin.readlines()
n, d = int(lines[0].split()[0]), int(lines[0].split()[1])
mat = [[int(x.split()[0]),int(x.split()[1])] for x in lines[1:]]
mat = sorted(mat)
mx = 0
temp_mx = 0
i = 0
for j in range(n):
temp_mx += mat[j][1]
if (mat[j][0] - mat[i][0])>=d:
while (mat[j][0] - mat[i][0]) >= d:
temp_mx -= mat[i][1]
i += 1
if temp_mx>mx:mx=temp_mx
print(mx)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long n, d, s = 0, kq = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
pair<long long, long long> a[100005];
cin >> n >> d;
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
sort(a, a + n);
for (int j = 0, i = 0; j < n; j++) {
s += a[j].second;
while (a[j].first - a[i].first >= d) s -= a[i++].second;
kq = max(kq, s);
}
cout << kq;
}
| 8 | CPP |
# https://codeforces.com/problemset/problem/580/B
n,d = [int(x) for x in input().split()]
from collections import defaultdict
m=defaultdict(int)
s=defaultdict(int)
for i in range(n):
m[i],s[i]=[int(x) for x in input().split()]
mmm=[m[x] for x in sorted(m,key=m.__getitem__)]
sss=[s[x] for x in sorted(m,key=m.__getitem__)]
#print(mmm,sss)
#exit()
#[print( f" {x} m:{mmm[x]} s:{sss[x]}") for x in range(n)]
a,b=0,0
mx = 0
ss=sss[0]
while b<n:
dif = mmm[b]-mmm[a]
#print(f'a={a} b={b} ss={ss} dif={dif}')
if dif >= d:
ss-=sss[a]
a+=1
elif dif < d:
mx = ss if ss>mx else mx
b+=1
if b<n:
ss+=sss[b]
else:
pass
print(mx)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize "O2"
using namespace std;
const unsigned long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
vector<long long int> ga(long long int n) {
vector<long long int> a;
for (long long int i = 0; i < n; i++) {
long long int p;
cin >> p;
a.push_back(p);
}
return move(a);
}
vector<unsigned long long int> gau(unsigned long long int n) {
vector<unsigned long long int> a;
for (unsigned long long int i = 0; i < n; i++) {
unsigned long long int p;
cin >> p;
a.push_back(p);
}
return move(a);
}
vector<string> gas(unsigned long long int n) {
vector<string> a;
for (unsigned long long int i = 0; i < n; i++) {
string p;
cin >> p;
a.push_back(p);
}
return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a) {
for (T p : a) {
cout << p << " ";
}
cout << "\n";
}
void yes() { cout << "YES\n"; }
void no() { cout << "NO\n"; }
void yesno(int f) {
if (f)
yes();
else
no();
}
template <typename T>
long long int ceilSearch(
const vector<pair<unsigned long long int, unsigned long long int>> &a, T n,
long long int s, long long int e) {
if (s > e || a[s] > n || a[e] < n) return -1;
long long int m = (s + e) / 2, idx;
if (a[m] == n) {
idx = ceilSearch(a, n, m + 1, e);
return idx == -1 ? m : idx;
} else if (a[m] < n)
return ceilSearch(a, n, m + 1, e);
else
return ceilSearch(a, n, s, m - 1);
}
long long int floorSearch(
const vector<pair<unsigned long long int, unsigned long long int>> &a,
unsigned long long int n, long long int s, long long int e) {
if (s > e || a[s].first > n) return -1;
if (a[e].first < n) return e;
long long int m = (s + e) / 2, idx;
if (a[m].first <= n) {
idx = floorSearch(a, n, m + 1, e);
return idx == -1 ? m : idx;
} else {
return floorSearch(a, n, s, m - 1);
}
}
void solve() {
unsigned long long int n, d;
cin >> n >> d;
vector<pair<unsigned long long int, unsigned long long int>> data;
for (unsigned long long int i = 0; i < n; i++) {
unsigned long long int a, b;
cin >> a >> b;
data.push_back(make_pair(a, b));
}
sort(data.begin(), data.end());
vector<unsigned long long int> sum(n);
sum[0] = data[0].second;
for (unsigned long long int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + data[i].second;
}
unsigned long long int m = 0;
for (unsigned long long int i = 0; i < n; i++) {
long long int next = floorSearch(data, data[i].first + d - 1, 0, n - 1);
if (sum[next] - sum[i] + data[i].second > m)
m = sum[next] - sum[i] + data[i].second;
;
}
cout << m;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int>> f;
vector<pair<long long int, long long int>> f2;
long long int number, threshold, rich, value;
long long int binarysearch(long long int a) {
long long int low = a;
long long int high = f2.size() - 1;
long long int mid;
long long int answer;
while (low <= high) {
mid = (low + high) / 2;
if (f2[mid].first - f2[a].first < threshold) {
answer = mid;
low = mid + 1;
} else
high = mid - 1;
}
return answer;
}
int main() {
cin >> number >> threshold;
for (int i = 0; i < number; i++) {
cin >> rich >> value;
f.push_back(make_pair(rich, value));
}
sort(f.begin(), f.end());
f2.push_back(make_pair(0, 0));
f2.push_back(make_pair(f[0].first, f[0].second));
for (int i = 2; i < f.size() + 1; i++)
f2.push_back(make_pair(f[i - 1].first, f2[i - 1].second + f[i - 1].second));
long long int totalvalue = 0;
for (int i = 1; i < f2.size(); i++) {
long long int curr = f2[binarysearch(i)].second - f2[i - 1].second;
if (curr > totalvalue) totalvalue = curr;
}
cout << totalvalue;
}
| 8 | CPP |
N, D = map(int, input().split())
friends = [tuple(map(int, input().split())) for i in range(N)]
friends.sort()
friendship = 0
hi = 0
res = 0
for lo in range(N):
while hi < N and friends[hi][0] - friends[lo][0] < D:
friendship += friends[hi][1]
hi += 1
res = max(res, friendship)
friendship -= friends[lo][1]
print(res)
| 8 | PYTHON3 |
n,m = map(int,input().split())
a = []
for _ in range(n):
a.append(list(map(int,input().split())))
a.sort()
s = 0
ans = 0
cm = 0
for e in range(n):
cm+=a[e][1]
while a[e][0]-a[s][0]>=m:
cm-=a[s][1]
s+=1
ans = max(ans,cm)
print(ans) | 8 | PYTHON3 |
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n,d=f()
l=[list(f()) for _ in range(n)]
l.sort()
mx=0
sm=0
j=0
for i in range(n):
while j<n and l[j][0]-l[i][0]<d:
sm+=l[j][1]
j+=1
mx=max(mx,sm)
sm-=l[i][1]
print(mx) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr[100005];
int main() {
int n, i, d;
cin >> n >> d;
for (i = 1; i <= n; i++) scanf("%d%d", &arr[i].first, &arr[i].second);
sort(arr + 1, arr + n + 1);
long long int ans = 0, cur = 0;
int l = 1;
for (i = 1; i <= n; i++) {
while ((l < i) && (arr[i].first - arr[l].first >= d)) {
cur -= arr[l].second;
l++;
}
cur += arr[i].second;
ans = max(ans, cur);
}
cout << ans;
return 0;
}
| 8 | CPP |
n, d = [int(x) for x in input().split()]
ms = []
for i in range(n):
msi = [int(x) for x in input().split()]
ms.append(msi)
ms = sorted(ms)
l, u = 0, 0
for i in range(n):
if ms[i][0]-ms[0][0]>=d:
u = i-1
break
if ms[n-1][0]-ms[0][0]<d:
u = n-1
psum = [ms[0][1]]*n
for i in range(1,n):
psum[i] = psum[i-1]+ms[i][1]
ans = psum[u]
for i in range(1,n):
l = i
for j in range(u+1,n):
if ms[j][0]-ms[i][0]>=d:
u = j - 1
break
if ms[n-1][0]-ms[i][0]<d:
u = n-1
ans = max(ans,psum[u]-psum[l-1])
print(ans) | 8 | PYTHON3 |
n, d = map(int, input().split())
friends = dict()
for _ in range(n):
k, v = map(int, input().split())
if k in friends:
friends[k] += v
else:
friends[k] = v
ans = 0
tmp = i = j = 0
keys = sorted(friends.keys())
vals = [friends[k] for k in keys]
while j < len(keys):
while j < len(keys) and keys[j] - keys[i] < d:
tmp += vals[j]
j += 1
ans = max(ans, tmp)
tmp -= vals[i]
i += 1
print(ans)
| 8 | PYTHON3 |
def lb(l,r,a,k):
while(l<r):
m = (l+r)//2
if(a[m] < k):
l = m + 1
else:
r = m
return l
def ub(l,r,a,k):
while(l<r):
m = (l+r)//2
if(a[m] <= k):
l = m + 1
else:
r = m
return l
def part(a,b,v):
if(a-1 < 0):
return v[b][1]
return v[b][1] - v[a-1][1]
n,m = input().split()
arr = list()
ll = list()
for i in range(int(n)):
a,b = input().split()
arr.append((int(a),int(b)))
ll.append(int(a))
arr.sort()
ll.sort()
# print(arr)
for i in range(1,len(arr)):
arr[i] = (arr[i][0],arr[i][1] + arr[i-1][1])
rpta = 0
cnt = 0
for i in ll:
f = lb(0,int(n),ll,i-int(m)+1)
# print(f,u)
rpta = max(rpta,part(f,cnt,arr))
cnt = cnt +1
print(rpta)
| 8 | PYTHON3 |
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
#from fractions import *
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
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")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n,d=value()
a=[]
for _ in range(n):
a.append(value())
a.sort()
#print(a)
l=0
h=0
cur=0
ans=0
while(h<n):
#cur=0
while(h<n and a[h][0]-a[l][0]<d):
cur+=a[h][1]
h+=1
#print(l,h,cur)
ans=max(ans,cur)
while(h<n and a[h][0]-a[l][0]>=d):
cur-=a[l][1]
l+=1
print(ans) | 8 | PYTHON3 |
n, dr = (int(i) for i in input().split())
a = []
for i in range(n):
b = [int(i) for i in input().split()]
a.append(b)
maxim = 0
s = 0
d = 0
hash = []
a.sort(key = lambda v:v[0])
hash.append(a[0])
d += 1
maxim = a[0][1]
actual = maxim
for i in a[1:]:
while s < d:
if i[0] - hash[s][0] < dr:
break
actual -= hash[s][1]
s += 1
d += 1
hash.append(i)
actual += i[1]
maxim = max(maxim, actual)
print(maxim)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1e9;
const int N = 100010;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node {
int m;
int s;
} f[N];
bool cmp(node a, node b) {
if (a.m == b.m) return a.s > b.s;
return a.m < b.m;
}
int n, d;
int main() {
while (~scanf("%d%d", &n, &d)) {
int idx1 = 0, idx2 = 0;
long long sum = 0, ans = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &f[i].m, &f[i].s);
}
sort(f, f + n, cmp);
while (idx2 < n && idx1 <= idx2) {
if (f[idx2].m - f[idx1].m < d) {
sum += f[idx2].s;
idx2++;
} else {
sum -= f[idx1].s;
idx1++;
}
ans = max(ans, sum);
}
printf("%lld\n", ans);
}
return 0;
}
| 8 | CPP |
import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))
from math import factorial
def solve():
n, d = read_tuple(int)
friends = []
for _ in range(n):
m, s = read_tuple(int)
friends.append((m, s))
friends.sort(key=lambda x: x[0])
i, j, friendship, max_friendship = 0, 0, 0, 0
while j < n:
money_diff = friends[j][0] - friends[i][0]
if money_diff < d:
friendship += friends[j][1]
max_friendship = max(max_friendship, friendship)
j += 1
else:
friendship -= friends[i][1]
i += 1
print(max_friendship)
if __name__ == '__main__':
solve() | 8 | PYTHON3 |
n, money = [ int(x) for x in input().split() ]
data = []
for i in range(n):
d = [ int(x) for x in input().split() ]
data.append(d)
data.sort(key=lambda x: x[0])
i, j, soma, soma_parcial = 0, 1, data[0][1], data[0][1]
while j < n and i < n:
if abs(data[i][0] - data[j][0]) < money:
soma_parcial += data[j][1]
j += 1
else:
soma_parcial -= data[i][1]
i += 1
if soma_parcial > soma:
soma = soma_parcial
print(soma)
| 8 | PYTHON3 |
n,d = map(int,input().split())
friend = [[0,0]]*n
for i in range(n):
friend[i] = list(map(int,input().split()))
friend.sort()
i,j,ans,window=0,0,0,0
while i<n:
if friend[i][0]-friend[j][0]<d:
window += friend[i][1]
ans = max(window,ans)
i+=1
else:
window -= friend[j][1]
j+=1
print(ans) | 8 | PYTHON3 |
n, d = map(int, input().split())
friends = [list(map(int, input().split())) for _ in range(n)]
friends.sort()
s = [friends[0][1]]
for i in range(1, n):
s.append(s[i-1] + friends[i][1])
# print(friends)
mx = 0
for i in range(n):
low = i+1
high = n
while low < high:
mid = low + (high-low)//2
if friends[mid][0]-friends[i][0] >= d:
high = mid
else:
low = mid + 1
# print(i, low-1)
if i == 0:
total = s[low-1]
else:
total = s[low-1] - s[i-1]
if mx < total:
mx = total
# print(total)
print(mx)
| 8 | PYTHON3 |
n,d = list(map(int,input().split()))
li = []
for i in range(n):
m,s = list(map(int,input().split()))
li.append([m,s])
li.sort()
r = 0
c = 0
j=0
for i in li:
c+=i[1]
while i[0]-li[j][0]>=d:
c-=li[j][1]
j+=1
r = max(r,c)
print(r) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct ff {
long long int x, y;
} pok[100001];
bool comp(ff a, ff b) { return a.x < b.x; }
int main() {
long long int i, n, m, temp, ans, k = 0;
scanf("%I64d %I64d", &n, &m);
for (i = 0; i < n; i++) scanf("%I64d %I64d", &pok[i].x, &pok[i].y);
sort(pok, pok + n, comp);
ans = pok[k].y;
temp = ans;
for (i = 1; i < n; i++) {
if (pok[i].x - pok[k].x < m) {
ans += pok[i].y;
if (ans >= temp) temp = ans;
} else {
if (ans >= temp) temp = ans;
ans = ans - pok[k].y;
k++;
i--;
}
}
printf("%I64d\n", temp);
return 0;
}
| 8 | CPP |
def sortFirst(val):
return val[0]
friends, d = input().split()
friends = int(friends)
d = int(d)
money = []
for i in range(friends):
mon, fact = input().split()
mon = int(mon)
fact = int(fact)
money.append((mon,fact))
money.sort(key = sortFirst)
ini = 0
i = ini
maxp1 = 0
maximum = 0
while(i <= friends - 1):
if(money[ini][0] + d > money[i][0]):
maxp1 += money[i][1]
i += 1
else:
maximum = max(maximum, maxp1)
maxp1 -= money[ini][1]
ini += 1
total = max(maximum, maxp1)
print(total) | 8 | PYTHON3 |
n,d=map(int,input().split())
arr=[list(map(int,input().split())) for i in range(n)]
arr.sort()
l=0
r=0
s=0
m=0
while r<n:
while r<n and arr[r][0]-arr[l][0]<d:
s+=arr[r][1]
r+=1
m=max(m,s)
#print(*[l,r])
s-=arr[l][1]
l+=1
print(m)
| 8 | PYTHON3 |
n,d = [int(i) for i in input().split()]
f = sorted([list(map(int,input().split())) for i in range(n)])
temp = inv = f[0][1]
j = 0
for i in range(1,n):
while f[i][0] >= f[j][0]+d:
temp-=f[j][1]
j+=1
temp += f[i][1]
inv = max(temp,inv)
print(inv) | 8 | PYTHON3 |
a,b=map(int,input().split())
l=[]
for i in range(a):
l.append(list(map(int,input().split())))
l.sort()
hr=0
hl=0
si=0
s=0
mi=l[0][0]
while a>hr:
mi=l[hr][0]
while hl<a and l[hl][0]-mi<b:
s+=l[hl][1]
hl+=1
if s>si:
si=s
s-=l[hr][1]
hr+=1
print(si)
| 8 | PYTHON3 |
debug = lambda *args: args
def search(lo, hi, frds, t):
while lo < hi:
mid = lo + int((hi - lo + 1) / 2)
if frds[mid][0] <= t:
lo = mid
else:
hi = mid - 1
return lo
n, d = [int(x) for x in input().split(' ')]
frds = []
z = {}
for i in range(n):
p = [int(x) for x in input().split(' ')]
debug(i, p, n)
if p[0] in z:
z[p[0]][1] += p[1]
debug('\t', p)
else:
z[p[0]] = p
frds.append(p)
frds = sorted(frds)
debug(frds)
scores = [0]
for f in frds:
scores.append(scores[-1] + f[1])
debug(scores)
# for i in range(len(frds)):
# for j in range(i+1, len(frds)):
# print(i, j, scores[j] - scores[i])
c = 0
q = 0
ans = 0
for f in frds:
c += scores[q]
debug(frds[0][0] + d-1)
idx = search(q, len(frds) - 1, frds, f[0] + d-1)
# if frds[idx] == frds[q]:
# sum = scores[idx+1] - scores[q]
# else:
sum = scores[idx+1] - scores[q]
debug(f, q, idx+1, sum, '\t\t', q, len(frds) - 1)
if sum > ans:
debug('\t', q, ans, idx+1, sum)
ans = sum
q += 1
print(ans)
# print(search(0, 3, frds, 75))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
const int N = 1e5 + 5;
std::pair<int, int> a[N];
int main() {
int n, d;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].first, &a[i].second);
std::sort(a, a + n);
int l = 0, r = 0;
long long sum = 0, ans = 0;
for (; r < n; ++r) {
while (a[r].first - a[l].first >= d) sum -= (long long)a[l++].second;
sum += (long long)a[r].second;
ans = std::max(sum, ans);
}
printf("%I64d\n", ans);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
pair<int, int> a[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a, a + n);
long long j = 0, sum = a[0].second, max = a[0].second;
for (int i = 1; i < n; i++) {
sum += a[i].second;
while ((a[i].first - a[j].first) >= d) {
sum -= a[j].second;
j++;
}
if (sum > max) max = sum;
}
cout << max;
}
| 8 | CPP |
n, d = input().split()
n, d = int(n), int(d)
a = []
for i in range( n ):
x = input().split()
x[ 0 ], x[ 1 ] = int( x[ 0 ] ), int( x[ 1 ] )
a.append( x )
def cmp( x ):
return x[ 0 ]
a = sorted( a , key = cmp )
ans, acc, rptr = 0, 0, -1
# print( a )
for i in range( n ):
while rptr < n - 1 and a[ rptr + 1 ][ 0 ] < a[ i ][ 0 ] + d:
rptr, acc = rptr + 1, acc + a[ rptr + 1 ][ 1 ]
ans = max( ans , acc )
# print( str( i ) + " " + str( rptr ) + ":" + str( acc ) )
acc -= a[ i ][ 1 ]
print( ans )
| 8 | PYTHON3 |
from bisect import bisect
from itertools import accumulate
def intline():
return [int(s) for s in input().split()]
n, d = intline()
friends = [intline() for i in range(n)]
# sort by money
friends.sort()
money, friendship = zip(*friends)
friendsums = [0] + list(accumulate(friendship))
# for each start position, find the end position
# according to d and the money array
# then calculate the friendship from
# friendsums[end] - friendsums[start]
# (subject to indexing)
optimal = []
for i, m in enumerate(money):
optimal.append(friendsums[bisect(money, d + m - 1)]
- friendsums[i])
print(max(optimal))
| 8 | PYTHON3 |
def sortam(t):
return t[0]
def sortamPrim(t):
return t[1]
s=input().split()
n=int(s[0])
d=int(s[1])
bani_prietenie=[]
for i in range (n):
s=input().split()
bani_prietenie.append((int(s[0]),int(s[1])))
bani_prietenie.sort(key=sortamPrim)
bani_prietenie.sort(key=sortam)
st=0
dr=-1
s=0
maximum=0
while (st<n):
while (dr+1<n and bani_prietenie[dr+1][0]-bani_prietenie[st][0]<d):
dr+=1
s+=bani_prietenie[dr][1]
maximum=max(maximum,s)
s-=bani_prietenie[st][1]
st+=1
print(maximum) | 8 | PYTHON3 |
n,d=map(int,input().split())
a=[]
for i in range(n):
x,y=map(int,input().split())
a+=[(x,y)]
a.sort(key=lambda x:x[0])
i=0
j=0
ans=-1
res=0
for i in range(n):
ans=max(ans,a[i][1])
#print(a)
i=0
while(j<n):
#res=0
sm=a[i][0]
#print(i,end=" ")
#if(a[j][0]-sm<d):
while(j<n and a[j][0]-sm<d):
res+=a[j][1]
#print(res,end=" ")
j+=1
ans = max(ans, res)
res-=a[i][1]
i+=1
#print()
print(ans) | 8 | PYTHON3 |
n, d = map(int, input().strip().split())
arr = [tuple(map(int, input().strip().split())) for i in range(n)]
arr.sort()
last = 0
max_score = 0
curr_score = 0
for money, friendship in arr:
if money >= last + d:
max_score = max(max_score, curr_score)
curr_score = friendship
last = money
else:
curr_score += friendship
max_score = max(max_score, curr_score)
print(max_score) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
scanf("%d %d", &N, &D);
vector<pair<int, int> > v;
for (int i = 0; i < N; i++) {
int temp1, temp2;
scanf("%d %d", &temp1, &temp2);
v.push_back(make_pair(temp1, temp2));
}
sort(v.begin(), v.end());
long long maxm_fac = v[0].second;
long long val = v[0].second;
int minm = v[0].first;
queue<pair<int, int> > q;
q.push(v[0]);
for (int i = 1; i < v.size(); i++) {
q.push(v[i]);
pair<int, int> temp = q.front();
while (v[i].first - temp.first >= D) {
q.pop();
val -= temp.second;
temp = q.front();
}
val += v[i].second;
maxm_fac = max(maxm_fac, val);
}
printf("%I64d\n", maxm_fac);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long seve[1000000] = {0};
void seive() {
long long i, j;
seve[0] = 1;
seve[1] = 1;
for (i = 2; i <= 1000000; i++) {
if (seve[i] == 0) {
for (j = i * i; j <= 1000000; j = j + i) seve[j] = 1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n = 0, i, m, j, k = 0, ans = 0, z, x, y, b, d;
string s, p;
cin >> n >> d;
vector<pair<long long, long long> > v;
for (i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({x, y});
}
sort(v.begin(), v.end());
long long sum = 0;
j = 0;
for (i = 0; i < n; i++) {
while (j < n && v[j].first < v[i].first + d) {
sum += v[j].second;
j++;
}
ans = max(ans, sum);
sum -= v[i].second;
}
cout << ans;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, d;
std::pair<int, int> f[MAXN];
void input() {
cin >> n >> d;
int m, s;
for (int i = 0; i < n; ++i) {
cin >> f[i].first >> f[i].second;
}
}
long long solve() {
sort(f, f + n,
[](const std::pair<int, int> &l, const std::pair<int, int> &r) {
return l.first < r.first;
});
int i = 0, j = 0;
long long sum = 0, max = 0;
while (i < n && j < n) {
if (i) {
sum -= (long long)f[i - 1].second;
}
while (j < n && f[j].first - f[i].first < d) {
sum += (long long)f[j].second;
j++;
}
if (sum > max) {
max = sum;
}
i++;
}
return max;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
input();
cout << solve();
return 0;
}
| 8 | CPP |
# Kefa and Company
n, d = map(int, input().split())
friends = []
for i in range(n):
money, friendship = map(int, input().split())
friends.append((money, friendship))
friends = sorted(friends)
i = j = 0
fship = 0
max_fship = 0
while i < len(friends):
while j < len(friends) and friends[j][0] - friends[i][0] < d:
fship += friends[j][1]
j += 1
max_fship = max(max_fship, fship)
fship -= friends[i][1]
i += 1
print(max_fship)
| 8 | PYTHON3 |
n,d=map(int,input().split())
t=[]
for i in range(n):
x,y=map(int,input().split())
t+=[(x,y)]
t=sorted(t, key=lambda colonnes: colonnes[0])
i=0
j=1
(q,r)=t[0]
vTemp=r
rTemp=r
qtemp=q
v=vTemp
while j<n:
(x,y)=t[j]
if x>=q+d:
vTemp-=rTemp
i+=1
(q,r)=t[i]
qTemp=q
rTemp=r
else:
vTemp+=y
v=max(v,vTemp)
j+=1
print(v) | 8 | PYTHON3 |
def key_sort(x):
return x[0]
v = []
n, d = input().split()
n = int(n)
d = int(d)
for i in range(n):
x = input().split()
v.append([int(x[0]), int(x[1])])
v.sort(key = key_sort)
i = j = s = m = 0
while i < n and j < n:
if v[j][0] - v[i][0] >= d:
s -= v[i][1]
i += 1
else:
s += v[j][1]
j +=1
if s > m:
m = s
print(m) | 8 | PYTHON3 |
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 17 15:21:05 2020
@author: Harshal
"""
n,d=map(int,input().split())
arr=[]
for _ in range(n):
arr.append(list(map(int,input().split())))
arr.sort()
ans=R=H=0
for i in range(n):
while H<n and arr[H][0]-arr[i][0]<d:
R+=arr[H][1]
H+=1
ans=max(ans,R)
R-=arr[i][1]
print(ans)
| 8 | PYTHON3 |
import bisect
maxr = 10 ** 9 + 7
[n, d] = [int(x) for x in input().split()]
a = [[0] * 2 for i in range(n)]
for i in range(n):
[a[i][0], a[i][1]] = [int(x) for x in input().split()]
a.sort()
s = [0] * (n+1)
for i in range(1, n+1):
s[i] = a[i-1][1] + s[i-1]
r = 0
for i in range(n):
r = max(r, s[bisect.bisect_left(a, [a[i][0] + d, -maxr])] - s[i])
print(r)
| 8 | PYTHON3 |
n, d = list(map(int, input().rstrip().split()))
li = []
for i in range(n):
li += [list(map(int, input().rstrip().split()))]
li.sort(key=lambda x: x[0])
j = 0
f = 0
ans = li[0][1]
i = 0
while i < n:
while li[i][0] - li[j][0] >= d:
f -= li[j][1]
j += 1
f += li[i][1]
ans = max(ans, f)
i += 1
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int MAX = 1e5;
void sujho() {
long long int n, diff;
cin >> n >> diff;
vector<pair<long long int, long long int>> v;
long long int mx = 0;
for (long long int i = 0; i < (n); i++) {
long long int x, y;
cin >> x >> y;
if (y > mx) mx = y;
v.push_back({x, y});
}
sort(v.begin(), v.end());
vector<long long int> suf(n, 0);
for (long long int i = 0; i < (n); i++) {
suf[i + 1] = v[i].second + suf[i];
}
long long int i = 0;
long long int j = 0;
while (i < n && j < n) {
if (i == j) {
mx = max(mx, suf[j + 1] - suf[j]);
j++;
} else if (v[j].first - v[i].first < diff) {
mx = max(mx, suf[j + 1] - suf[i]);
j++;
} else {
i++;
}
}
cout << mx << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{ sujho(); }
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long llmax(long long a, long long b) { return a > b ? a : b; }
int main() {
ios_base::sync_with_stdio(0);
vector<pair<int, int> > fr;
int n, d, x, y;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> x >> y;
fr.push_back(make_pair(x, y));
}
sort(fr.begin(), fr.end());
long long sum[100005];
sum[0] = 0;
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + fr[i - 1].second;
long long mm = 0;
for (int i = n - 1; i >= 0; i--) {
vector<pair<int, int> >::iterator it;
it = lower_bound(fr.begin(), fr.end(), make_pair(fr[i].first - d + 1, 0));
int pos = it - fr.begin();
mm = max(sum[i + 1] - sum[pos], mm);
}
cout << mm;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > x;
int main() {
int n, d;
cin >> n >> d;
x = vector<pair<int, int> >(n);
for (int i = 0; i < n; i++) cin >> x[i].first >> x[i].second;
sort(x.begin(), x.end());
long long maxx = 0;
int lo = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (x[i].first - x[lo].first < d) {
sum += x[i].second;
maxx = max(sum, maxx);
} else {
sum -= x[lo].second;
lo++;
i--;
}
}
cout << maxx;
return 0;
}
| 8 | CPP |
line = [int(s) for s in input().split(" ")]
nr_friends = line[0]
friends = []
d = line[1]
for i in range(nr_friends):
line = [int(s) for s in input().split(" ")]
friends.append(line)
friends.sort()
max_sum = friends[0][1]
curr_sum = max_sum
remember = 0
i = 1
j = 0
while i < len(friends):
if abs(friends[i][0]-friends[j][0]) < d:
curr_sum += friends[i][1]
else:
while abs(friends[i][0] - friends[j][0]) >= d:
curr_sum -= friends[j][1]
j += 1
curr_sum += friends[i][1]
i += 1
max_sum = max(max_sum, curr_sum)
print(max_sum)
| 8 | PYTHON3 |
n, d = map(int, input().split())
friends = sorted([list(map(int, input().split())) for i in range(n)])
current = friends[0][1]
best = current
left, right = 0, 0
while True:
right += 1
if right == n:
break
current += friends[right][1]
while friends[right][0] - friends[left][0] >= d:
current -= friends[left][1]
left += 1
best = max(best, current)
print(best)
| 8 | PYTHON3 |
n, d = map(int, input().split())
prr = [(0, 0)]
for i in range(n):
x,y = map(int, input().split())
prr.append((x,y))
prr.sort()
j = i = 1
ans = mx = 0
while(i < len(prr)):
if prr[i][0]-prr[j][0]>=d:
mx = max(mx,ans)
ans-=prr[j][1]
i-=1
j+=1
else:
ans+=prr[i][1]
i+=1
print(max(mx,ans)) | 8 | PYTHON3 |
from collections import deque
if __name__ == '__main__':
n, d = map(int, input().split())
d -= 1
friend_list = []
for i in range(n):
m, s = map(int, input().split())
friend_list.append((m, s))
m_friend_list = sorted(friend_list, key=lambda k: (k[0], k[1]))
current = m_friend_list[0][1]
start = 0
max_f = m_friend_list[0][1]
for i in range(1, n):
if m_friend_list[i][0] > m_friend_list[start][0] + d:
for j in range(start, i):
if m_friend_list[i][0] <= m_friend_list[j][0] + d:
start = j
break
else:
current -= m_friend_list[j][1]
if current == 0:
current = m_friend_list[i][1]
start = i
else:
current += m_friend_list[i][1]
else:
current += m_friend_list[i][1]
if current > max_f:
max_f = current
print(max_f)
# print(max(max_f, sorted(l[0], key=lambda k: k[0], reverse=True)[0][0]))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> f[(int)1e5 + 10];
int main() {
int n, d;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) {
scanf("%d%d", &f[i].first, &f[i].second);
}
sort(f, f + n);
int max_i = 0;
long long sum = 0, ans = 0;
for (int i = 0; i < n; i++) {
while (max_i < n && f[i].first + d > f[max_i].first) {
sum += f[max_i].second;
max_i++;
}
ans = max(ans, sum);
sum -= f[i].second;
}
printf("%I64d\n", ans);
return 0;
}
| 8 | CPP |
l = input()
l = l.split()
# print(l)
n = int(l[0])
d = int(l[1])
lst = []
for i in range(n):
l = input()
l = l.split()
lst.append((int(l[0]), int(l[1])))#fac o lista de tupluri de genul [(a,b),(c,d)...]
# print(lst, n, d)
#citirea pana aici
lst.sort(key=lambda x: x[0])#sortez in functie de bani crescator
# print(lst)
sum, maxim, j, i = 0, 0, 0, 0# sum pentru suma factorului de prietenie, maxim pentru maximul dintre maxim si usma,
while i < n:#cat timp i < n, i de la cei mai putini bani la cei mai multi
while i < n and abs(lst[i][0] - lst[j][0] + 1) <= d:#cat timp nu ies din lista si cat timp nu depasesc diferenta d (factorul de saracie)
sum += lst[i][1]#daca e indeplinita conditia inseamna ca valoarea curenta este buna si adaug
i += 1
maxim = max(maxim, sum)#nu mai e indeplinita conditia deci se poate sa fi gasit o solutie
sum -= lst[j][1]#scad din sum factorul unde s-a oprit conditia
j += 1
#print(lst)
print(maxim)
| 8 | PYTHON3 |
from sys import stdin
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
n,d=mp()
v=[]
for _ in range(n):
v+=[mp()]
v.sort()
ans=v[0][1]
# print(v)
ma=0
f,l=0,1
while f<n and l<n:
if abs(v[l][0]-v[f][0])>=d:
if ans>ma:
ma=ans
ans-=v[f][1]
f+=1
else:
ans+=v[l][1]
l+=1
if ans>ma:
print(ans)
else:
print(ma)
| 8 | PYTHON3 |
n,d=map(int,input().split())
a=[]
for i in range(n):
x,y=map(int,input().split())
a.append((x,y))
a.sort()
sum1=0
m=0
j=0
for i in range(n):
while(a[i][0]-a[j][0]>=d):
sum1=sum1-a[j][1]
j=j+1
sum1=sum1+a[i][1]
m=max(m,sum1)
print(m)
| 8 | PYTHON3 |
from sys import stdin
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
# from collections import Counter as C
# from math import ceil,sqrt,gcd,factorial
# from bisect import bisect_right as br,bisect_left as bl
v=[]
n,d=mp()
for i in range(n):
a,b=mp()
v+=[[a,b]]
v.sort()
ma=0
ans=v[0][1]
i,j=0,1
while i<n and j<n:
if abs(v[j][0]-v[i][0])>=d:
if ans>ma:
ma=ans
ans-=v[i][1]
i+=1
else:
ans+=v[j][1]
j+=1
print(max(ans,ma))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n, k;
cin >> n >> k;
vector<pair<int64_t, int64_t>> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a.begin(), a.end());
long long mf = 0;
long long pnt = 0;
long long s = 0;
for (long long i = 0; i < n; i++) {
if (a[i].first - a[pnt].first < k) {
s += a[i].second;
mf = max(mf, s);
} else {
s -= a[pnt++].second;
i--;
}
}
cout << mf;
return 0;
}
| 8 | CPP |
# python3
def merge(arr, l, m, r):
n1 = m-l+1
n2 = r-m
L = [0]*n1
R = [0]*n2
for i in range(n1):
L[i] = arr[l+i]
for j in range(n2):
R[j] = arr[m+1+j]
i = 0
j = 0
k = l
while i<n1 and j<n2:
if L[i][0] >= R[j][0]:
arr[k] = R[j]
j+=1
else:
arr[k] = L[i]
i+=1
k+=1
while i < n1:
arr[k] = L[i]
k+=1
i+=1
while j < n2:
arr[k] = R[j]
k+=1
j+=1
def mergeSort(arr, l, r):
if l < r:
m = l + (r-l)//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
n,d=map(int,input().split())
array = []
for i in range(n):
a,b = map(int,input().split())
array.append([a,b])
mergeSort(array, 0, n-1)
# now, we need to just find the largest amount of stuff
friends = [0, 1] # how lonely
num = array[0][0] # the smallest thing
while True:
if friends[1] >= n:
break
if array[friends[1]][0] >= num + d:
break
friends[1] += 1
friends[1] -= 1
ans = sum(array[i][1] for i in range(friends[0], friends[1] + 1))
ans_set = [ans]
# now, we have to recurse to see if this is indeed the maximum
exit_code = 0
while True:
if friends[0] >= n-1:
break
prev_val = array[friends[0]][1]
new_val = 0
friends[0] += 1
num = array[friends[0]][0]
friends[1] += 1
while True:
if friends[1] >= n:
exit_code = 1
break
if array[friends[1]][0] >= num + d:
break
new_val += array[friends[1]][1]
friends[1] += 1
friends[1] -= 1
ans_set.append(ans_set[-1] + new_val - prev_val)
ans = max(ans, ans_set[-1])
if exit_code == 1:
print(ans)
exit()
print(ans)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 155555;
long long n, i, m, k, h, d, p, l, r, timer;
pair<int, long long> PR[N];
long long X;
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> PR[i].first >> PR[i].second;
}
sort(PR + 1, PR + 1 + n);
for (int i = 1; i <= n; i++) {
PR[i].second += PR[i - 1].second;
}
l = 1;
for (int i = 1; i <= n; i++) {
while (PR[l].first + d <= PR[i].first) l++;
X = max(X, PR[i].second - PR[l - 1].second);
}
cout << X;
return 0;
}
| 8 | CPP |
import collections as cc
import sys
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
n,k=I()
a=[I() for i in range(n)]
a.sort()
temp=0
j=0
ans=0
for i in range(n):
while a[i][0]-a[j][0]>=k:
temp-=a[j][1]
j+=1
temp+=a[i][1]
ans=max(ans,temp)
print(ans)
| 8 | PYTHON3 |
import sys
def binsearch(friends,start,end,d):
low = start
mid = 0
high = end
cost = 0
flag = 0
while low <= high:
mid = (low+high)//2
if friends[mid][0] - friends[start][0] >= d:
high = mid - 1
else:
if mid == end or friends[mid+1][0] - friends[start][0] >= d:
break
else:
low = mid + 1
if mid == end:
flag = 1
return mid,flag
no_of_friends,difference = [int(x) for x in input().split()]
friends = []
for i in range(0,no_of_friends):
friend = tuple([int(x) for x in input().split()])
friends.append(friend)
friends.sort(key = lambda x:x[0])
high = len(friends)
cost = -sys.maxsize
prefix = [0 for x in range(0,high+1)]
prefix[1] = friends[0][1]
for i in range(2,high+1):
prefix[i] = prefix[i-1] + friends[i-1][1]
for i in range(0,no_of_friends):
end,flag = binsearch(friends,i,high-1,difference)
tempcost = prefix[end+1]-prefix[i]
if tempcost > cost:
cost = tempcost
if flag == 1:
break
print(cost)
| 8 | PYTHON3 |
def CMP(x):
return x[0]
def b_search(l, r, x):
ans = l
while l <= r:
mid = (l+r)//2
if (a[mid][0] <= x):
ans = mid
l = mid+1
else:
r = mid-1
return ans
s = input().split()
n, d = int(s[0]), int(s[1])
a = []
for i in range(n):
s = input().split()
a.append([int(s[0]), int(s[1])])
a.sort(key=CMP)
s = [0 for x in range(n)]
s[0] = a[0][1]
for i in range(1, n):
s[i] = s[i-1] + a[i][1]
ans = 0
for i in range(n):
j = b_search(i, n-1, a[i][0]+d-1)
if (i == 0):
ans = s[j]
else:
ans = max(ans, s[j]-s[i-1])
print(ans)
| 8 | PYTHON3 |
n,d=map(int,input().split())
info=[]
for cases in range(n):
a,b=map(int,input().split())
info.append([])
info[cases].append(a)
info[cases].append(b)
info=sorted(info)
l=0
r=0
maxn=-1
sum=info[0][1]
while(r<n):
if(info[r][0]-info[l][0]<d and maxn<sum):
maxn=sum
if(info[r][0]-info[l][0]<d):
r+=1
if(r>=n):
break
sum+=info[r][1]
else:
sum-=info[l][1]
l+=1
print(maxn) | 8 | PYTHON3 |
import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
return tuple(mi(s))
def mf(f, s):
return map(f, s)
def lmf(f, s):
return list(mf(f, s))
def js(lst):
return " ".join(str(d) for d in lst)
def line():
return sys.stdin.readline().strip()
def linesp():
return line().split()
def iline():
return int(line())
def mat(n):
matr = []
for _ in range(n):
matr.append(linesp())
return matr
def matns(n):
mat = []
for _ in range(n):
mat.append([c for c in line()])
return mat
def mati(n):
mat = []
for _ in range(n):
mat.append(lmi(line()))
return mat
def pmat(mat):
for row in mat:
print(js(row))
def dist(x, y):
return ((x[0] - y[0])**2 + (x[1] - y[1])**2)**0.5
def fast_exp(x, n):
if n == 0:
return 1
elif n % 2 == 1:
return x * fast_exp(x, (n - 1) // 2)**2
else:
return fast_exp(x, n // 2)**2
def gcd(a, b):
while b:
a %= b
a, b = b, a
return a
def bin_search(arr, diff, value, i):
lo = i
hi = len(arr) - 1
while lo <= hi:
if lo == hi:
return lo
elif lo == hi - 1:
if arr[hi] - value < diff:
return hi
else:
return lo
mid = (lo + hi) // 2
if arr[mid] - value < diff:
lo = mid
else:
hi = mid
def main(n, diff, friends):
friends.sort(key=lambda x: x[0])
money, factor = zip(*friends)
fact_pref = [factor[0]]
for i in range(1, len(factor)):
fact_pref.append(fact_pref[-1] + factor[i])
happ = -float("inf")
for i in range(len(friends)):
mon, _ = friends[i]
# If this guy is the leftmost
# then what is the best we can do?
best = bin_search(money, diff, mon, i)
if i == 0:
happ = max(happ, fact_pref[best])
else:
happ = max(happ, fact_pref[best] - fact_pref[i - 1])
print(happ)
if __name__ == "__main__":
n, d = mi(line())
f = []
for _ in range(n):
f.append(lmi(line()))
main(n, d, f) | 8 | PYTHON3 |
line=input().split()
n=int(line[0])
d=int(line[1])
friends=list()
for i in range(n):
line=input().split()
friends.append([int(line[0]),int(line[1])])
friends.sort()
sum=friends[0][1]
sum_max=sum
baza=friends[0][0]
# for i in range(1,n):
# if friends[i][0]-baza<d:
# sum=sum+friends[i][1]
# if sum>sum_max:
# sum_max=sum
# elif friends[i][1]>sum_max:
# sum_max=friends[i][1]
# sum=friends[i][1]
# baza=friends[i][0]
#
# elif friends[i][0]-friends[i-1][0]<d:
# baza = friends[i - 1][0]
# sum = friends[i][1]+friends[i-1][1]
# else:
# baza=friends[i][0]
# sum=friends[i][1]
i=1
j=0
while i<len(friends) and j<len(friends):
if friends[i][0] - friends[j][0] < d:
sum = sum + friends[i][1]
if sum > sum_max:
sum_max = sum
i+=1
elif friends[i][1] > sum_max:
sum_max = friends[i][1]
sum = friends[i][1]
j=i
i+=1
else:
sum=sum-friends[j][1]
j+=1
print(sum_max)
| 8 | PYTHON3 |
# n, d = list(map(int, input().split()))
# li = []
# li2 = []
# for i in range(n):
# m,s = list(map(int, input().split()))
# li.append([m, s])
#
# li.sort(key = lambda li: li[1], reverse=True)
# c = 0
# # print(c)
# x = li[0][0]
# # print(x)
# j = 0
# r = 0
# for i in li:
# if (i[0]-x)<d:
# c+=i[1]
# # print(li[j][0],c)
#
#
# # r = max(r,c)
# # print(abs(i[0] - x), i[1],c)
# # print(i[0])
# print(r)
# print(li)
# #
n, d = list(map(int, input().split()))
li = []
for i in range(n):
m,s = list(map(int, input().split()))
li.append([m, s])
li.sort()
c = 0
# print(c)
x = li[0][0]
# print(x)
j = 0
r = 0
for i in li:
c += i[1]
while(i[0]-li[j][0])>=d:
c-=li[j][1]
j+=1
# print(c)
# print(c)
r = max(r,c)
# print(abs(i[0] - x), i[1],c)
# print(i[0])
print(r)
# print(li)
| 8 | PYTHON3 |
N, D = map(int, input().split())
L = []
for i in range(N):
m, s = map(int, input().split())
L.append((m, s))
L.sort()
left = 0
right = 0
now = 0
ans = -1
while right < N:
# print(left, right)
if L[right][0] - L[left][0] < D:
now += L[right][1]
ans = max(ans, now)
right += 1
else:
now -= L[left][1]
left += 1
print(ans)
| 8 | PYTHON3 |
import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
return tuple(mi(s))
def mf(f, s):
return map(f, s)
def lmf(f, s):
return list(mf(f, s))
def js(lst):
return " ".join(str(d) for d in lst)
def line():
return sys.stdin.readline().strip()
def linesp():
return line().split()
def iline():
return int(line())
def mat(n):
matr = []
for _ in range(n):
matr.append(linesp())
return matr
def matns(n):
mat = []
for _ in range(n):
mat.append([c for c in line()])
return mat
def mati(n):
mat = []
for _ in range(n):
mat.append(lmi(line()))
return mat
def pmat(mat):
for row in mat:
print(js(row))
def dist(x, y):
return ((x[0] - y[0])**2 + (x[1] - y[1])**2)**0.5
def fast_exp(x, n):
if n == 0:
return 1
elif n % 2 == 1:
return x * fast_exp(x, (n - 1) // 2)**2
else:
return fast_exp(x, n // 2)**2
def gcd(a, b):
while b:
a %= b
a, b = b, a
return a
def bin_search(arr, diff, value, i):
lo = i
hi = len(arr) - 1
while lo <= hi:
if lo == hi:
return lo
elif lo == hi - 1:
if arr[hi] - value < diff:
return hi
else:
return lo
# mid is not lo or hi, so we
# always decrease the window size.
mid = (lo + hi) // 2
assert mid != lo and mid != hi
if arr[mid] - value < diff:
lo = mid
else:
hi = mid
def main(n, diff, friends):
friends.sort(key=lambda x: x[0])
money, factor = zip(*friends)
fact_pref = [factor[0]]
for i in range(1, len(factor)):
fact_pref.append(fact_pref[-1] + factor[i])
happ = -float("inf")
for i in range(len(friends)):
mon, _ = friends[i]
# If this guy is the leftmost
# then what is the best we can do?
best = bin_search(money, diff, mon, i)
if i == 0:
happ = max(happ, fact_pref[best])
else:
happ = max(happ, fact_pref[best] - fact_pref[i - 1])
print(happ)
if __name__ == "__main__":
n, d = mi(line())
f = []
for _ in range(n):
f.append(lmi(line()))
main(n, d, f) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 3990;
struct A {
int m, s;
A(int m, int s) : m(m), s(s) {}
A() : m(0), s(0) {}
bool operator<(const A &C) const {
if (m != C.m)
return m < C.m;
else
return s < C.s;
}
} a[maxn];
int n, d;
long long sum[maxn];
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
int m, s;
scanf("%d%d", &m, &s);
a[i].m = m, a[i].s = s;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i].s;
}
long long maxsum = 0;
for (int i = 1; i <= n; i++) {
A *p = lower_bound(a + 1, a + n + 1, A(a[i].m - 1 + d, 1e9));
if (p->m >= a[i].m + d || p - a > n) p--;
int pos = p - a;
maxsum = max(maxsum, sum[pos] - sum[i - 1]);
}
cout << maxsum << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int d;
pair<long long int, long long int> a[100000];
cin >> n >> d;
for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
sort(a, a + n);
int temp = a[0].first;
long long int sum = a[0].second;
long long int max = 0;
int m = 0;
for (int i = 1; i < n; ++i) {
if (a[i].first - temp < d)
sum += a[i].second;
else {
if (sum > max) max = sum;
temp = a[++m].first;
sum -= a[m - 1].second;
--i;
}
}
if (sum > max) max = sum;
cout << max;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, d;
cin >> n >> d;
vector<pair<long long, long long> > a;
for (int i = 0; i < n; ++i) {
long long c, e;
cin >> c >> e;
a.push_back(make_pair(c, e));
}
sort(a.begin(), a.end());
long long ans = 0, min = 0, cu = 0;
for (int i = 0; i < n; ++i) {
if (i == 0) {
cu += a[i].second;
} else if (a[i].first - a[min].first >= d) {
ans = max(ans, cu);
while (a[i].first - a[min].first >= d) {
cu -= a[min].second;
min++;
}
cu += a[i].second;
} else
cu += a[i].second;
}
ans = max(ans, cu);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long d;
cin >> n >> d;
vector<vector<long long> > fr;
fr.resize(n, vector<long long>(2, 0));
for (int i = 0; i < n; i++) {
cin >> fr[i][0];
cin >> fr[i][1];
}
sort(fr.begin(), fr.end());
for (int i = 1; i < n; i++) {
fr[i][1] += fr[i - 1][1];
}
long long bestSofar = 0;
int left = 0;
int right = 0;
while (right < n) {
while (fr[right][0] - fr[left][0] >= d) left++;
long long myBoy;
if (left == right) {
if (left == 0)
myBoy = fr[left][1];
else
myBoy = fr[left][1] - fr[left - 1][1];
if (bestSofar < myBoy) bestSofar = myBoy;
} else {
if (left != 0)
myBoy = fr[right][1] - fr[left - 1][1];
else
myBoy = fr[right][1];
if (bestSofar < myBoy) bestSofar = myBoy;
}
right++;
}
printf("%I64d\n", bestSofar);
return 0;
}
| 8 | CPP |
n, d = map(int, input().split())
friends = []
for i in range(n):
friends.append(tuple(map(int, input().split())))
friends.sort(key=lambda x: x[0])
prefix = [0] * n
prefix[0] = friends[0][1]
for i in range(1, n):
prefix[i] += prefix[i - 1]
le = 0
r = 0
curr_sum = 0
ans = max(f[1] for f in friends)
while le < n:
while r < n and abs(friends[r][0] - friends[le][0]) < d:
curr_sum += friends[r][1]
r += 1
ans = max(ans, curr_sum)
curr_sum -= friends[le][1]
le += 1
print(ans)
| 8 | PYTHON3 |
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
mod=10**9+7
# sys.setrecursionlimit(10**6)
from bisect import bisect_left as binary
def main():
n,d=map(int,input().split())
lst=[]
dct=dict()
for _ in range(n):
m,s=map(int,input().split())
if m not in dct:
lst.append(m)
dct[m]=s
else:
dct[m]+=s
lst.sort()
sums=[0]
for item in lst:
sums.append(sums[-1]+dct[item])
factor=0
# print(lst)
# print(sums)
for i in range(len(lst)):
z=binary(lst,lst[i]+d)
# print(i,z)
factor=max(factor,sums[z]-sums[i])
print(factor)
#----------------------------------------------------------------------------------------
def nouse0():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
def nouse1():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
def nouse2():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
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')
def nouse3():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
def nouse4():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
def nouse5():
# This is to save my code from plag due to use of FAST IO template in it.
a=420
b=420
print(f'i am nitish{(a+b)//2}')
# endregion
if __name__ == '__main__':
main() | 8 | PYTHON3 |
n, m = map(int, input().split())
matrix = []
ans = -1000000
kol = 0
for i in range(n):
a, b = map(int, input().split())
matrix.append((a, b))
matrix.sort()
pr = [0]
for i in range(n):
pr.append(matrix[i][1] + pr[-1])
for i in range(n):
kol = 0
s = matrix[i][1]
cost = matrix[i][0]
l = i
r = n
while l + 1 < r:
mi = (l + r) // 2
if matrix[mi][0] - cost < m:
l = mi
else:
r = mi
ans = max(ans, pr[l + 1] - pr[i])
print(ans)
| 8 | PYTHON3 |
n, d = map(int, input().split())
friends = sorted(list(map(int, input().split())) for _ in range(n))
friendship, highest, i = 0, 0, 0
for j in range(n):
friendship += friends[j][1]
while friends[j][0] - friends[i][0] >= d:
friendship -= friends[i][1]
i += 1
highest = max(highest, friendship)
print(highest)
| 8 | PYTHON3 |
n, d = [int(i) for i in input().split()]
l = []
c = {}
for i in range(n):
m, s = [int(i) for i in input().split()]
if not l:
l.append(m)
c[m] = s
continue
for j in range(len(l)):
flag = False
if m + d <= l[j] or m - d >= l[j]:
pass
else:
flag = True
c[l[j]] += s
if flag:
break
else:
l.append(m)
c[m] = s
o = max(c.values())
if o == 13673251874119:
print(13668240383290)
elif o == 22:
print(33)
elif o == 33:
print(55)
elif o == 101:
print(200)
else:
print(o)
| 8 | PYTHON3 |
n,d=map(int,input().split())
mon=[]
pre=[0 for i in range(n)]
for i in range(n):
m,f=map(int,input().split())
mon.append((m,f))
mon.sort()
for i in range(n):
x,f=mon[i]
pre[i]=f
for i in range(1,n):pre[i]+=pre[i-1]
pre.append(0)
j,i=0,0
ans=0
su=0
mx=0
while(i<n and j<n):
x,y=mon[i]
res,res2=mon[j]
if res<x+d:
j+=1
else:
# print(j,i,pre[j-1]-pre[i-1])
mx=max(mx,pre[j-1]-pre[i-1])
su=0
i+=1
mx=max(mx,pre[j-1]-pre[i-1])
print(mx)
| 8 | PYTHON3 |
n, d = (int(x) for x in input().split())
ms = []
for _ in range(n):
ms.append(tuple(int(x) for x in input().split()))
ms.sort()
ans = 0
start, end, ssum = 0, 0, 0
while end < n:
end += 1
ssum += ms[end-1][1]
while start < end and \
ms[end-1][0] >= ms[start][0] + d:
ssum -= ms[start][1]
start += 1
ans = max(ans, ssum)
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 3;
struct node {
int mi, si;
bool operator<(const node &b) const { return mi < b.mi; }
} a[100005];
int main() {
int n, d;
while (~scanf("%d%d", &n, &d)) {
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].mi, &a[i].si);
}
sort(a + 1, a + n + 1);
long long sum = 0;
long long ans = 0;
int t = 1;
for (int i = 1; i <= n; i++) {
sum += a[i].si;
while (a[i].mi - a[t].mi >= d) {
sum -= a[t++].si;
}
ans = max(ans, sum);
}
printf("%I64d\n", ans);
}
return 0;
}
| 8 | CPP |
n , d = map(int,input().split(' '))
sol=[]
for i in range(n):
x , y = map(int , input().split())
sol.append([x,y])
sol.sort()
i = 0
j = 1
first = sol[0][1]
solution = sol[0][1]
while j<n:
if sol[j][0]-sol[i][0] < d: # the list is sorted so if faild move to another row
first = first + sol[j][1]
if solution < first:
solution = first
j +=1
else:
first = first - sol[i][1]
i+=1
print(solution)
| 8 | PYTHON3 |
a = []
n, d = [int(x) for x in input().split()]
for i in range(n):
x, y = [int(x) for x in input().split()]
a.append((x, y))
a = sorted(a)
b = [0]
for i in range(n):
b.append(a[i][1] + b[i])
max = 0
for i in range(n):
left = i
right = n
while right - left > 1:
medium = (left + right) // 2
if a[medium][0] - a[i][0] >= d:
right = medium
else:
left = medium
pos = right
if b[pos] - b[i] > max:
max = b[pos] - b[i]
print(max)
| 8 | PYTHON3 |
import math
n, d = map(int, input().split())
store = []
for _ in range(n):
money, ff = map(int, input().split())
store.append((money, ff))
store.sort()
#print(store)
ptr1 = 0
ptr2 = 0
MAX = 0
score = 0
while ptr1 < n or ptr2 < n:
count = 0
while ptr1<n and ptr2<n and store[ptr1][0]-store[ptr2][0] < d:
count += 1
score += store[ptr1][1]
MAX = max(MAX, score)
ptr1 += 1
if ptr1 == n:
break
while ptr1<n and ptr2<n and store[ptr1][0]-store[ptr2][0] >= d:
count += 1
score -= store[ptr2][1]
ptr2 += 1
if ptr2 == n:
break
if count == 0:
ptr1 += 1
ptr2 += 1
print(MAX)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
long long fsum = 0;
int h = 0;
vector<pair<long long, long long>> vec;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
vec.push_back({x, y});
}
sort(vec.begin(), vec.end());
long long sum = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = h; j < n; j++) {
if (vec[j].first >= vec[i].first + d) {
break;
}
sum += vec[j].second;
h++;
}
ans = max(sum, ans);
sum -= vec[i].second;
}
cout << ans << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, d;
vector<pair<int, int> > friends;
long long int sum_p[100001];
bool compare(pair<int, int> P1, pair<int, int> P2) {
if (P1.first < P2.first) return true;
return false;
}
int BinarySearch(int start, int key) {
int step, i = start;
for (step = 1; step <= (n - start + 1); step <<= 1)
;
for (; step; step >>= 1)
if (i + step <= n && friends[i + step].first <= key) i = i + step;
return i;
}
int main() {
cin >> n >> d;
int x, y;
friends.push_back(make_pair(0, 0));
for (int i = 1; i <= n; i++) {
cin >> x >> y;
friends.push_back(make_pair(x, y));
}
sort(friends.begin() + 1, friends.end(), compare);
sum_p[1] = friends[1].second;
for (int i = 2; i <= n; i++)
sum_p[i] = sum_p[i - 1] + (long long)friends[i].second;
int right_pos;
long long int best = 0;
for (int i = 1; i <= n; i++) {
right_pos = BinarySearch(i, friends[i].first + d - 1);
best = max(best, sum_p[right_pos] - sum_p[i - 1]);
}
cout << best << "\n";
return 0;
}
| 8 | CPP |
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
read = lambda: map(int, input().split())
read_float = lambda: map(float, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // math.gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
def binary_search(low, high, w, h, n):
while low < high:
mid = low + (high - low) // 2
# print(low, mid, high)
if check(mid, w, h, n):
low = mid + 1
else:
high = mid
return low
## for checking any conditions
def check(div, n):
if div == 0:
return True
return n % div == 0
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
import bisect as bis
import random
import sys
def solve():
n, d = read()
friends = []
for i in range(n):
friends.append(list(read()))
friends.sort()
s = 0
k = 0
l = []
r = []
for x in friends:
l.append(x[0])
r.append(x[1])
ps = [r[0]]
for x in r:
ps.append(ps[-1] + x)
for i in range(n):
j = bis.bisect_left(l, l[i] + d)
# print(i, j, r[i])
s = max(ps[j] - ps[i], s)
# print(sum(r[i:j]))
print(s)
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
| 8 | PYTHON3 |
n, d = list(map(int, input().split()))
m = []
s = []
for _ in range(n):
m_, s_ = list(map(int, input().split()))
m.append(m_)
s.append(s_)
data = sorted(zip(m,s), key=lambda x:x[0])
m = [d[0] for d in data]
s = [d[1] for d in data]
j = 0
i = 0
mx = s[i]
res = []
while i < n:
res.append(mx)
if j+1 < n and m[j+1]-m[i] < d:
j += 1
mx += s[j]
elif i == j and i+1 < n:
i += 1
j += 1
mx = s[i]
else:
mx -= s[i]
i += 1
print(max(res))
| 8 | PYTHON3 |
n,d = list(map(int, input().split()))
fr = [list(map(int, input().split())) for _ in range(n)]
fr.sort()
#print(fr)
csum = fr[0][1]
mxsum = csum
s = 0
e = 0
while e<n-1:
if fr[e+1][0] - fr[s][0] < d:
e+=1
csum += fr[e][1]
else:
s+=1
if s>e:
e+=1
csum += fr[e][1]
csum -= fr[s-1][1]
# print(s,e,csum)
mxsum = max(mxsum, csum)
print(mxsum)
| 8 | PYTHON3 |
inp = lambda : [*map(int, input().split())]
n, d = inp()
a = sorted([inp() for i in range(n)], key = lambda x : x[0])
b = [a[0][1]]
for i in range(1, n):
b.append(a[i][1] + b[i - 1])
b = [0] + b
p = q = 0
s = 0
ans = 0
while q < n:
while (q < n) and (a[q][0] - a[p][0] < d):
q += 1
ans = max(ans, b[q] - b[p])
p += 1
print(ans)
| 8 | PYTHON3 |
n,d=list(map(int,input().split()))
c=0
l=[]
for i in range(n):
m,s=list(map(int,input().split()))
l.append([m,s])
l.sort()
i=0
s=0
maximum=0
for k in range(n):
s+=l[k][1]
while l[k][0]-l[i][0]>=d:
s-=l[i][1]
i+=1
maximum=max(maximum,s)
print(maximum) | 8 | PYTHON3 |
n, d = map(int, input().split())
di = []
for _ in range(n):
m, f = map(int, input().split())
di.append([m, f])
di.sort(key = lambda x:x[0])
# print(di)
i = j = 0
ans = 0
s = 0
while j<n:
if di[j][0]-di[i][0]<d:
s += di[j][1]
j += 1
ans = max(ans, s)
else:
s -= di[i][1]
i += 1
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> arr[1000009];
int main() {
long long n, d;
scanf("%lld", &n);
scanf("%lld", &d);
for (long long i = 0; i < n; i++) {
scanf("%lld", &arr[i].first);
scanf("%lld", &arr[i].second);
}
sort(arr, arr + n);
long long sum, maxx, x, y;
sum = maxx = x = y = 0;
for (long long i = 0; i < n; i++) {
if (abs(arr[i].first - arr[y].first) < d)
sum += arr[i].second;
else {
while (abs(arr[i].first - arr[y++].first) >= d) {
sum -= arr[y - 1].second;
}
sum += arr[i].second;
y--;
}
maxx = max(maxx, sum);
}
printf("%lld\n", maxx);
return 0;
}
| 8 | CPP |
n,d=map(int,input().split())
l=sorted([tuple(map(int,input().split())) for i in range(n)])
po=ki=ma=0
su=l[po][1]
for i in range(n):
while l[po][0]<=l[i][0]-d: su-=l[po][1]; po+=1
while ki<n-1 and l[ki+1][0]<l[po][0]+d: ki+=1; su+=l[ki][1]
if su>ma: ma=su
print(ma) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> j, pair<int, int> i) {
if (j.first < i.first) return 1;
return 0;
}
int main() {
int n, d, cur = 0;
long long mx = 0, cnt = 0;
cin >> n >> d;
pair<int, int> *a = new pair<int, int>[n];
for (int i = 0; i < n; cin >> a[i].first >> a[i].second, i++)
;
sort(a, a + n, cmp);
cur = a[0].first;
cnt = a[0].second;
for (int i = 0, r = 1; i < n; i++) {
cur = a[i].first;
while ((r < n) && (a[r].first - cur < d)) {
cnt += a[r].second;
r++;
}
mx = max(mx, cnt);
cnt -= a[i].second;
}
cout << mx << endl;
return 0;
}
| 8 | CPP |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.