solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
import sys def solve(input_path=None): if input_path is None: f = sys.stdin else: f = open(input_path, 'r') n, d = map(int, f.readline().split()) candidates = list() for _ in range(n): ab, at = map(int, f.readline().split()) candidates.append((ab, at)) candidates = sorted(candidates) res = 0 temp = 0 i, j = 0, 0 while i < n and j < n: if candidates[j][0] - candidates[i][0] < d: temp += candidates[j][1] res = max(res, temp) j += 1 else: temp -= candidates[i][1] i += 1 return [f"{res}"] def main(): for line in solve(): print(f"{line}") if __name__ == '__main__': main()
8
PYTHON3
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): pass # 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") # endregion if __name__ == "__main__": main() n,d = map(int,input().split()) l = [] for _ in range(n): l.append(tuple(map(int,input().split()))) l.sort() ans=0 a=[] i=0 j=0 while i<n: if l[i][0]-l[j][0]>=d: a.append(ans) ans-=l[j][1] j+=1 else: ans+=l[i][1] a.append(ans) i+=1 print(max(a))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long maxm; int main() { long long n, d; cin >> n >> d; vector<pair<long long, long long> > p; long long x, y; for (long long i = 0; i < n; i++) { cin >> x >> y; p.push_back(make_pair(x, y)); } sort(p.begin(), p.end()); for (int i = 1; i < n; i++) p[i].second += p[i - 1].second; long long l, r; l = r = -1; long long ans = 0, tmp = 0; while (l < n && r < n) { r++; if (r == n) break; if (l == -1) l = 0; while (p[l].first + d <= p[r].first) l++; if (l > 0) ans = max(ans, p[r].second - p[l - 1].second); else ans = max(ans, p[r].second); } cout << ans << "\n"; return 0; }
8
CPP
n,d=map(int,input().split()) l=[] for i in range(n): m,s=map(int,input().split()) l.append((m,s)) l.sort() r=[] j=0 s=0 d1=l[0][0] for i in range(n): if l[i][0]-d1<d: s+=l[i][1] else: r.append(s) for k in range(j,i): if l[i][0]-l[k][0]>=d: s-=l[k][1] d1=l[k+1][0] j=k+1 s+=l[i][1] r.append(s) print(max(r))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; pair<int, int> v1[100001]; int main() { long long n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> v1[i].first >> v1[i].second; } int j = 0; sort(v1, v1 + n); long long sum = 0, ans = 0; for (int i = 0; i < n; i++) { sum += v1[i].second; while (v1[i].first - v1[j].first >= t) { sum -= v1[j].second; j++; } ans = max(ans, sum); } cout << ans; return 0; }
8
CPP
from collections import defaultdict n, d = map(int, input().split(' ')) friends = [] for i in range(n): m, s = map(int, input().split(' ')) friends.append((m, s)) # sort in n * log n friends.sort() friendship_factor = 0 slow, fast = 0, 0 money_difference = 0 current_friendship = 0 while fast < n: current_friendship += friends[fast][1] while friends[fast][0] - friends[slow][0] >= d and slow < fast: current_friendship -= friends[slow][1] slow += 1 friendship_factor = max(friendship_factor, current_friendship) fast += 1 print(friendship_factor)
8
PYTHON3
n, d = map(int, input().split()) a = [] for i in range(n): a.append(list(map(int, input().split()))) a.sort(key=lambda x: x[0]) # print(a) # очередной кандидат на удаление = 0 # в цикле по всем друзьям # добавляем очередного ( = запоминаем уровень богатства + добавляем его степень дружбы) # пока очередной кандидат на удаление слишком беден # вычитаем его степень дружбы; # переходим к следующему кандидату на удаление j = 0 friendship = 0 max_friendship = 0 for i in range(n): friendship += a[i][1] while a[j][0] + d <= a[i][0]: friendship -= a[j][1] j += 1 max_friendship = max(friendship, max_friendship) print(max_friendship)
8
PYTHON3
# Description of the problem can be found at http://codeforces.com/problemset/problem/580/B n, d = map(int, input().split()) l_p = list() for _ in range(n): l_p.append(list(map(int, input().split()))) l_p.sort() l, h = 0, 0 f = 0 f_m = 0 while h < len(l_p): if l_p[h][0] - l_p[l][0] >= d: f -= l_p[l][1] l += 1 else: f += l_p[h][1] h += 1 f_m = max(f_m, f) print(f_m)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; int u, v; vector<pair<int, int> > a(n); for (int i = 0; i < n; i++) { cin >> u >> v; a[i] = pair<int, int>(u, v); } sort(a.begin(), a.end()); long long int ans = 0, mini = a[0].first; long long int sum = a[0].second; int l = 0, r = 1; while (r < n) { if (a[l].first + k <= a[r].first) { ans = max(ans, sum); sum += a[r].second; while (a[l].first + k <= a[r].first) { sum -= a[l].second; l++; } } else { sum += a[r].second; } r++; } ans = max(sum, ans); cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, d; pair<int, int> a[N]; int cmp(pair<int, int> a1, pair<int, int> a2) { return a1.first < a2.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } sort(a + 1, a + 1 + n, cmp); int i = 1, j = 1; long long sum = 0, ans = 0; for (int i = 1; i <= n; i++) { if (a[i].first - a[j].first < d) { sum += a[i].second; ans = max(ans, sum); } else { sum -= a[j].second; j++; i--; } } cout << ans; return 0; }
8
CPP
n, d = map(int, input().split()) friends = [] for i in range(n): m, f = map(int, input().split()) friends.append([m, f]) friends.sort(key=lambda x: x[0]) l, r, s = 0, 0, friends[0][1] sums = [] for i in range(1, n): r += 1 if friends[r][0] - friends[l][0] < d: s += friends[i][1] else: sums.append(s) while friends[r][0] - friends[l][0] >= d: s -= friends[l][1] l += 1 s += friends[r][1] sums.append(s) print(max(sums))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Node { int key; struct Node *left, *right; }; struct Node* newNode(int key) { struct Node* node = new Node; node->key = key; node->left = node->right = nullptr; return node; }; void inorder(struct Node* root) { if (root == nullptr) { return; } cout << root->key << " "; inorder(root->left); inorder(root->right); } void inputTree(struct Node*& root, int n) { struct Node *parent, *child; map<int, struct Node*> mp; int n1, n2; char lr; while (n--) { cin >> n1 >> n2 >> lr; if (mp.find(n1) == mp.end()) { parent = newNode(n1); mp[n1] = parent; if (root == nullptr) { root = parent; } } else { parent = mp[n1]; } child = newNode(n2); if (lr == 'L') { parent->left = child; } else { parent->right = child; } mp[n2] = child; } } bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } int main() { int n; long long d; cin >> n >> d; vector<pair<long long, long long>> a(n); vector<long long> sum(n + 1); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end(), cmp); sum[0] = 0; for (int i = 0; i < n + 1; ++i) { sum[i] = sum[i - 1] + a[i].second; } long long mx = -1; for (int i = 0; i < n; ++i) { auto it = lower_bound(a.begin() + i, a.end(), make_pair(a[i].first + d, LLONG_MIN)); int num = distance(a.begin(), it); mx = max(mx, sum[num - 1] - sum[i - 1]); } cout << mx << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct edge { long long int money, friendship; }; bool cmp(edge A, edge B) { return A.money < B.money; } vector<edge> vv; int main() { edge get; long long int i, j, n, m, d, s, sum = 0, maxx = 0; cin >> n >> d; for (i = 0; i < n; i++) { cin >> m >> s; get.money = m; get.friendship = s; vv.push_back(get); } sort(vv.begin(), vv.end(), cmp); i = 0, j = 0; if (n == 1) cout << vv[0].friendship << endl; else { for (i = 0; i < n;) { while (j < n && (vv[j].money - vv[i].money) < d) { sum += vv[j].friendship; j++; } maxx = max(maxx, sum); if (j >= n) break; sum = sum - vv[i].friendship; i++; } cout << maxx << endl; } return 0; }
8
CPP
n,d=map(int,input().split()) l=[] for i in range(n): j=tuple(map(int,input().split())) l.append(j) l.sort() ans=0 maxx=0 j=0 i=0 while(j<n): if(l[j][0]-l[i][0]<d): ans+=l[j][1] j+=1 else: maxx=max(ans,maxx) ans-=l[i][1] i+=1 maxx=max(ans,maxx) print(maxx)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, d; scanf("%d %d", &n, &d); vector<pair<int, int> > friends(n); for (int i = 0; i < n; ++i) { scanf("%d %d", &friends[i].first, &friends[i].second); } sort(friends.begin(), friends.end()); int left = 0; int right = 0; long long cur_sum = 0; long long max_sum = 0; while (right < n) { while (friends[right].first - friends[left].first >= d) { cur_sum -= friends[left].second; left++; } cur_sum += friends[right].second; right++; max_sum = max(max_sum, cur_sum); } cout << max_sum; return 0; }
8
CPP
n,d=map(int,input().split()) friends=[tuple(map(int,input().split())) for i in range(n)] friends=sorted(friends,key=lambda f:f[0]) sums=[] curr=0 for i in range(n): curr+=friends[i][1] sums.append(curr) res=0 for i in range(n): #binary search low=i high=n-1 curr=friends[i][0] while low<high: mid=(low+high+1)//2 if abs(curr-friends[mid][0])>=d: high=mid-1 else: low=mid res=max(res,sums[low]-(0 if i==0 else sums[i-1])) print(res)
8
PYTHON3
n,d=map(int,input().split()) f=0 m=0 l=0 lst=list() for _ in range(n): a,b=map(int,input().split()) lst.append([a,b]) lst.sort() for i in range(n): f=f+lst[i][1] while lst[i][0]-lst[l][0]>=d: f=f-lst[l][1] l=l+1 m=max(m,f) print(m)
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,d=zzz() li=[] for _ in range( n ): li.append((zz())) li=sorted(li) s=ans=li[0][1] j=0 for i in range( 1,n): while(li[i][0]-li[j][0])>=d: s-=li[j][1] j+=1 s+=li[i][1] ans=max(s,ans) print(ans)
8
PYTHON3
import math,sys,re,itertools,pprint rs,ri,rai=input,lambda:int(input()),lambda:list(map(int, input().split())) n, d = rai() a = [ rai() for i in range(n) ] a.sort(key=lambda x: x[0]) l, r = 0, 1 s = m = a[0][1] while r < n: s += a[r][1] while a[r][0] - a[l][0] >= d: s -= a[l][1] l += 1 m = max(s, m) r += 1 print(m)
8
PYTHON3
n, d = [int(x) for x in input().split()] s = [] while (n): s.append([int(x) for x in input().split()]) n -= 1 s = sorted(s, key=lambda x: x[0], reverse=True) st = 0 suma = 0 maxim = -1 for dr in range(len(s)): suma += s[dr][1] while (abs(s[st][0] - s[dr][0]) >= d): suma -= s[st][1] st += 1 if (suma > maxim): maxim = suma print (maxim)
8
PYTHON3
import math n, d = [int(x) for x in input().split()] friends = [] for i in range(n): friends.append(tuple(int(x) for x in input().split())) friends.sort() friendship_max = 0 friendship = 0 j = 0 for i in range(len(friends)): while j < n and friends[j][0] - friends[i][0] < d: friendship += friends[j][1] j += 1 if friendship > friendship_max: friendship_max = friendship friendship_argmax = (i, j) friendship -= friends[i][1] print(friendship_max)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5; struct ones { int a, b; } f[N]; int cmp(const ones &a, const ones &b) { return a.a < b.a; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d%d", &f[i].a, &f[i].b); sort(f + 1, f + n + 1, cmp); int zz = 1; long long ans = 0, now = 0; for (int i = 1; i <= n; i++) { while (f[i].a - f[zz].a >= m) { now = now - (long long)f[zz].b; zz++; } now += (long long)f[i].b; if (now > ans) ans = now; } cout << ans << endl; return 0; }
8
CPP
n, d = map(int, input().split()) f = [] for i in range(n): f.append(list(map(int, input().split()))) f.sort() pf = [0] for i in range(n): pf.append(pf[-1]+f[i][1]) l = 0 r = 0 mx = 0 while r < n: if f[r][0]-f[l][0] < d: mx = max(mx, pf[r+1]-pf[l]) r += 1 else: l += 1 print(mx)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, d; long long sums[100200]; long long k = 0; vector<pair<long long, long long> > v; long long mk = 0; long long s = 0; int main() { cin >> n >> d; for (int i = 0; i < n; i++) { long long x; long long y; cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { s += v[i].second; sums[i] = s; } for (int i = 0; i < n; i++) { int lo = 0; int hi = n; long long c = v[i].first + d; int mid = 0; while (lo < hi) { mid = (lo + hi) / 2; if (c <= v[mid].first) hi = mid; else lo = mid + 1; } k = sums[hi - 1] - sums[i - 1]; mk = max(k, mk); k = 0; } cout << mk; return 0; }
8
CPP
n,d=map(int,input().split()) ans=[list(map(int,input().split())) for _ in range(n)] ans.sort(key =lambda x:x[0]) maxi=-199999 st=0 end =0 sm1=0 sm=0 start=0 #print(ans) while end<n: sm+=ans[end][1] sm1=ans[end][0] # print(sm,sm1) while st<=end and sm1-ans[st][0]>=d: #sm1-=ans[st][0] sm-=ans[st][1] st+=1 maxi=max(maxi,sm) end+=1 print(maxi)
8
PYTHON3
from sys import stdin n, k = map(int, stdin.readline().rstrip().split(" ")) li = [0]*n for i in range(n): li[i] = list(map(int, stdin.readline().rstrip().split(" "))) li.sort() start = end = j = 0 t = 0 ma = 0 for i in range(0, n): t+= li[i][1] end = i while li[end][0]-li[start][0]>=k: t-=li[start][1] start += 1 ma = max(ma, t) print(ma)
8
PYTHON3
n, d = map(int, input().split()) arr = sorted(list(map(int, input().split())) for _ in range(n)) m, max_s, i = 0, 0, 0 for j in range(n): m += arr[j][1] while arr[j][0] - arr[i][0] >= d: m -= arr[i][1] i += 1 max_s = max(max_s, m) print(max_s)
8
PYTHON3
n,k=map(int,input().split()) l=[list(map(int,input().split())) for i in range(n)] l.sort(key=lambda x:x[0]) val=l[0][0] i=0 j=i+1 ans=0 temp=l[i][1] while(j<n and l[j][0]-l[i][0]<k): temp+=l[j][1] j+=1 ans=max(ans,temp) #print("ans = ",ans) while(j<n): if(j<n and i<n and l[j][0]-l[i][0]<k): temp+=l[j][1] ans=max(ans,temp) j+=1 elif(j<n and i<n): temp-=l[i][1] i+=1 ans=max(ans,temp) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long MAX = 1000000000; long long MOD = 1000000007; pair<long long, long long> p[100010]; void solve() { long long n; cin >> n; long long d; cin >> d; ; for (long long i = (0); i < (n); i++) { long long in1; cin >> in1; long long in2; cin >> in2; ; p[i] = {in1, in2}; } sort(p, p + n); long long j = 0, sum = 0, ans = 0; for (long long i = (0); i < (n); i++) { while (p[j].first - p[i].first < d and j < n) { sum += p[j].second; j++; } ans = max(ans, sum); sum -= p[i].second; } cout << ans; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long test = 1; while (test--) { solve(); } }
8
CPP
from operator import itemgetter n,d = input().split() n=int(n) d=int(d) arr=[] for i in range(n): temp=[] money,respect_valu = input().split() temp.append(int(money)) temp.append(int(respect_valu)) arr.append(temp) arr.sort(key=itemgetter(0)) def find_max(num1,num2): if num1>num2: return num1 else: return num2 i=0 j=0 tem= 0 ans=0 while i<n and j<n: if (arr[j][0]-arr[i][0])>=d: ans = find_max(ans,tem) tem-=arr[i][1] i+=1 else: tem+=arr[j][1] j+=1 print(find_max(tem,ans))
8
PYTHON3
from itertools import accumulate, takewhile from bisect import bisect_right I = lambda: list(map(int, input().split())) n, d = I() pair = sorted([I() for i in range(n)], key = lambda x: x[0]) salary = next(zip(*pair)) sums = tuple(accumulate([0] + pair, func = lambda s, x: s + x[1])) p1, p2, ans = 0, 0, 0 while p1 < n: p2 = bisect_right(salary, salary[p1] + d - 1, p1) ans = max(ans, sums[p2] - sums[p1]) p1 += 1 print(ans)
8
PYTHON3
def main(): n, d = map(int, input().split()) l = sorted(tuple(map(int, input().split())) for _ in range(n)) lo, res = l[0][0], [] i = j = ss = 0 while True: for j in range(j, n): hi, s = l[j] if hi - lo >= d: res.append(ss) break ss += s else: res.append(ss) break for i in range(i, j + 1): lo, s = l[i] if hi - lo < d: break ss -= s print(max(res)) if __name__ == '__main__': main()
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 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)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { 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; } long long int suma = 0, maxim = 0; sort(v.begin(), v.end()); long long int start = 0, stop = 0; while (stop < n) { while (stop < n && v[stop].first - v[start].first + 1 <= d) { suma += v[stop].second; stop++; } maxim = max(maxim, suma); suma -= v[start].second; start++; } cout << maxim; return 0; }
8
CPP
a,d=map(int,input().split()) ans=[] for i in range(a): z=list(map(int,input().split())) ans.append(z) ans.sort(key=lambda x: (x[0],x[1])) l=0 r=0 maxa=[] mi=0 i=0 while(l<=r and r<len(ans)): if(ans[r][0]-ans[l][0]<d): mi+=ans[i][1] r+=1 i+=1 else: maxa.append(mi) l+=1 mi=maxa[-1]-ans[l-1][1] maxa.append(mi) print(max(maxa))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> v; vector<int>::iterator it; vector<pair<int, int> > tmp; int main() { int n, k, x, y, id, j; long long mx = -1, c[100002]; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x >> y; tmp.push_back(make_pair(x, y)); } sort(tmp.begin(), tmp.end()); for (int i = 0; i < n; i++) { v.push_back(tmp[i].first); c[i + 1] = tmp[i].second; } for (int i = 1; i <= n; i++) c[i] = c[i] + c[i - 1]; j = 0; for (int i = 0; i < n; i++) { while ((j < n) && v[i] + k > v[j]) { j++; } mx = max(mx, c[j] - c[i]); } cout << mx; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool cmp(const vector<pair<int, int> > &a1, const pair<int, int> &a2) { for (int i = 0; i < a1.size(); i++) { if (a1[i] < a2) return true; } return false; } int main() { ios_base::sync_with_stdio(0); int n, d, i, j, a, b; cin >> n >> d; vector<pair<int, int> > F; long long int Ff[n]; for (i = 0; i < n; i++) { cin >> a >> b; F.push_back(pair<int, int>(a, b)); } sort(F.begin(), F.end()); Ff[-1] = 0; Ff[0] = F[0].second; for (i = 1; i < n; i++) { Ff[i] = F[i].second + Ff[i - 1]; } int pos; long long int maxi = -1; for (i = 0; i < n; i++) { pos = upper_bound(F.begin(), F.end(), make_pair(F[i].first + d, 0)) - F.begin(); maxi = max(maxi, (Ff[pos - 1] - Ff[i - 1])); } cout << maxi << "\n"; }
8
CPP
def ans(f,n,d): if n == 1: return f[0][1] ans = f[0][1] tmp_ans = f[0][1] i = 0 j = 1 while j < n: if i == j: tmp_ans = f[j][1] j += 1 if tmp_ans > ans: ans = tmp_ans elif abs(f[i][0]-f[j][0]) < d: tmp_ans += f[j][1] j+=1 if tmp_ans > ans: ans = tmp_ans else: while abs(f[i][0]-f[j][0]) >= d and i < j: tmp_ans-=f[i][1] i+=1 return ans n,d = map(int,input().split()) friends = [] for _ in range(n): tmp = [int(i) for i in input().split()] friends.append(tmp) friends.sort() print(ans(friends,n,d))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, d, x, y, i = 0, j = 0; long long unsigned f = 0, maxx = 0; cin >> n >> d; vector<pair<int, int> > arr; for (i = 0; i < n; i++) { cin >> x >> y; arr.push_back(make_pair(x, y)); } sort(arr.begin(), arr.end()); for (i = 0; i < n; i++) { if (arr[i].first - arr[j].first >= d) { maxx = max(maxx, f); while (arr[i].first - arr[j].first >= d) { f -= arr[j].second; j++; } } f += arr[i].second; } maxx = max(maxx, f); cout << maxx; }
8
CPP
n,k = map(int,input().split()) a = [list(map(int,input().split())) for _ in range(n)] a.sort() s = 0 ans = 0 r = 0 for l in range(n): while r<n and a[r][0]-a[l][0]<k: s+=a[r][1] r+=1 ans=max(ans,s) s-=a[l][1] print(ans)
8
PYTHON3
def sol(f,n,d): i,j,ans,tmp_ans = 0,0,0,0 while i < n: if f[i][0] - f[j][0] < d: tmp_ans+=f[i][1] i+=1 if ans < tmp_ans: ans = tmp_ans else: while f[i][0] - f[j][0] >= d: tmp_ans-=f[j][1] j+=1 if ans < tmp_ans: ans = tmp_ans return ans n,d = map(int,input().split()) friends = [] for _ in range(n): m,s = map(int,input().split()) friends.append([m,s]) friends.sort() print(sol(friends,n,d))
8
PYTHON3
n, d = map(int, input().split()) lis1 = [] #lis2 = [] for i in range(n): m, f = map(int, input().split()) lis1.append([m, f]) lis1.sort() #lis1.append([1e10, 1e10]) ans = [] for i in range(1, len(lis1)): lis1[i][1] += lis1[i - 1][1] for i in range(len(lis1)): #cnt = lis1[i][1] lo, hi = i + 1, len(lis1) - 1 idx = i while lo <= hi: mid = lo + (hi - lo) // 2 if lis1[mid][0] - lis1[i][0] < d: idx = mid lo = mid + 1 else: hi = mid - 1 if i != 0: ans.append(abs(lis1[idx][1] - lis1[i - 1][1])) else: ans.append(lis1[idx][1]) print(max(ans))
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 mx=max(mx,temp_mx) print(mx)
8
PYTHON3
def solve(): n, d = map(int, input().split()) friends = [list(map(int, input().split())) for _ in range(n)] friends = sorted(friends, key=lambda f: f[0]) friendship = 0 max_friendship = friendship i = 0 for j in range(n): friendship += friends[j][1] while friends[j][0] - friends[i][0] >= d: friendship -= friends[i][1] i += 1 max_friendship = max(max_friendship, friendship) print(max_friendship) if __name__ == "__main__": solve()
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 # print("#",friends) curr = 0 while(ind < n): last = 0 miner = friends[s][0] while(s < n and abs(friends[ind][0] - friends[s][0]) >= d): # last += friends[s][1] curr -= friends[s][1] s += 1 miner = friends[s][0] # print("$",s) flag = True while(ind < n and abs(friends[ind][0] - miner) < d): curr += friends[ind][1] ind += 1 flag = False if flag: curr += friends[ind][1] + last ind += 1 # print(ind,curr,maxer,last) maxer = max(maxer,curr - last) 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> using namespace std; using lli = long long int; void solve() { int n, d; cin >> n >> d; vector<pair<lli, lli>> v(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); cout << endl; lli ans = 0, var = 0; int i = 0, j = 0; while (i < n && j < n) { if (v[i].first + d > v[j].first) { var += v[j++].second; } else { ans = max(ans, var); var -= v[i++].second; } } ans = max(ans, var); cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
8
CPP
# ��� � ���� ���������!!!!!! # ���� # ����������� �� ��������� �(logn * n) # ������� ��������� � �������� ��� ����� ������ # ������ ����� ����� ������ �� ������ ������� �� ����������� ���������� �������� # ������ ������������ �� ���� ���� # # # ��� ���???? # ������ ���� ���������� �������� � ������� ���������!!! # � ����� ��� ������?????? # ������ ����� ����� � ����� ���� ��������� � ���������� # � � ������� ��� �������� ��������� ����� ����� �� �(1) !!!! n, d = tuple(map(int, input().split())) m, s = [0] * n, [0] * n ms = [0] * n for i in range(n): m[i], s[i] = tuple(map(int, input().split())) ms[i] = m[i], s[i] suffix = [0] * n postfix = [0] * n alsum = sum(s) ms.sort() for i in range(1, n): postfix[i] = postfix[i - 1] + ms[i - 1][1] for i in range(n - 2, -1, -1): suffix[i] = suffix[i + 1] + ms[i + 1][1] friends = [] for i in range(n): # �������� �������� left = i - 1 right = n - 1 while right - left > 1: middle = (right + left) // 2 if ms[middle][0] >= ms[i][0] + d: right = middle else: left = middle rich = right if ms[rich][0] >= ms[i][0] + d: rich -= 1 #print(rich) friends.append(alsum - postfix[i] - suffix[rich]) print(max(friends))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, one, two; cin >> n >> m; vector<pair<int, int> > V; pair<int, int> P; for (int i = 0; i < n; i++) { cin >> P.first; cin >> P.second; V.push_back(P); } sort(V.begin(), V.end()); int i = 0, j = 0; long long sum = 0, ans = 0; for (i = 0; i < n; i++) { if (abs(V[i].first - V[j].first) >= m) { ans = max(ans, sum); sum -= V[j].second; j++; i--; } else { sum += V[i].second; } } ans = max(ans, sum); cout << ans << endl; return 0; }
8
CPP
I = lambda :map(int, input().split()) n,d=I() a=sorted([list(I()) for i in range(n)]) s=ans=a[0][1] j=0 for i in range(1,n): while(a[i][0]-a[j][0]>=d): s-=a[j][1] j+=1 s+=a[i][1] ans=max(s,ans) print(ans) # Made By Mostafa_Khaled
8
PYTHON3
n,f = input().split() n = int(n) f = int(f) a = [] for i in range(n): x,y = input().split() a.append((int(x),int(y))) a = sorted(a) s = a[0][1] mx = s k = 0 for i in range(1,len(a)): if a[i][0] - a[k][0] < f: s = s + a[i][1] else: s = s - a[k][1] + a[i][1] for j in range(k+1,i+1): if a[i][0] - a[j][0]<f: s = s k = j break else: s = s - a[j][1] k = j if s>mx: mx = s print (mx)
8
PYTHON3
from collections import OrderedDict n,d = map(int,input().split()) a = dict() for _ in range(n): m,s = map(int,input().split()) if m in a: a[m] += s else: a[m] = s a = OrderedDict(sorted(a.items())) a = [item for item in a.items()] k = 0 #starting element su = 0 #current sum mu = 0 #current maximum friendship for i in range(len(a)): su += a[i][1] while a[i][0] - a[k][0] >= d: su -= a[k][1] k += 1 mu = max(mu,su) print(mu)
8
PYTHON3
scan = lambda: map(int, input().split()) n, d = scan() arr = [] for i in range(n): m, r = scan() arr.append([m, r]) arr.sort() p1, p2 = 0, 1 tmp = arr[0][1] ans = tmp while p1<n: while p2<n and arr[p2][0]-arr[p1][0]<d: tmp += arr[p2][1] p2 += 1 ans = max(tmp, ans) tmp -= arr[p1][1] p1 +=1 print(ans)
8
PYTHON3
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Dec 6 13:41:04 2016 @author: kostiantyn.omelianchuk """ from sys import stdin, stdout lines = stdin.readlines() n, d = int(lines[0].split()[0]), int(lines[0].split()[1]) a = [[int(x.split()[0]),int(x.split()[1])] for x in lines[1:]] a = sorted(a) max_val = 0 start_pointer = 0 cur_val = 0 for end_pointer in range(n): if (a[end_pointer][0] - a[start_pointer][0])<d: cur_val += a[end_pointer][1] else: 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, start_pointer, end_pointer) print(max_val)
8
PYTHON3
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) # Trial one: # # sum_f = [0] # for i in range(1, n + 1): # sum_f.append(sum_f[i-1] + p[i - 1][1]) # # def bin_search(l, r, val): # mid = int((l + r) / 2) # if r < l: # return r # else: # if val < p[mid][0]: # return bin_search(l, mid - 1, val) # elif val > p[mid][0]: # return bin_search(mid + 1, r, val) # else: # return mid # # max_f = 0 # f = 0 # for i in range(n): # x = bin_search(i, n - 1, p[i][0] + d - 1) # f = sum_f[x + 1] - sum_f[i] # if f > max_f: # max_f = f # # print(max_f)
8
PYTHON3
n,k=map(int,input().split()) l=[] for i in range(n): l.append(list(map(int,input().split()))) l.sort(key=lambda x: x[0]) ans,maxx=0,0 ans=0 i,j=0,0 while i<n: while j<n and l[j][0]-l[i][0]<k: ans+=l[j][1] j+=1 maxx=max(ans,maxx) ans-=l[i][1] i+=1 print(maxx)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct kefa { int amount; long factor; bool operator<(const kefa &kefas) const { if (amount < kefas.amount) return true; if (amount > kefas.amount) return false; if (factor < kefas.factor) return true; return false; } }; kefa ar[100000]; long long cum_sum[100001], d; int n; int main() { cin >> n >> d; for (int i = 0; i < n; i++) { cin >> ar[i].amount >> ar[i].factor; } sort(ar, ar + n); cum_sum[0] = 0; for (int i = 0; i < n; i++) { cum_sum[i + 1] = cum_sum[i] + ar[i].factor; } long long y = 0; for (int i = 0; i < n; i++) { kefa arfriend; arfriend.amount = ar[i].amount + d; arfriend.factor = 0; kefa *p = lower_bound(ar + i, ar + n, arfriend) - 1; int j = p - (ar + i); long long x = cum_sum[i + j + 1] - cum_sum[i]; y = max(y, x); cerr << i << " " << j << " " << x << "\n"; } cout << y; return 0; }
8
CPP
n,d=[int(i) for i in input().split()] dr=[] for i in range(n): dr.append([int(i) for i in input().split()]) dr.sort() fri=[] l=0 r=1 fr=0 go=dr[0][1] for l in range(n): #print(dr[l],go) fr=dr[l][0] #print(fr) #go=dr[l][0] while r<n and dr[r][0]-fr<d: go+=dr[r][1] r+=1 fri.append(go) go-=dr[l][1] print(max(fri))
8
PYTHON3
def readnums(): return list(map(lambda x: int(x), input().split(" "))) n, d = readnums() fr = [] for i in range(n): fr.append(tuple(readnums())) fr.sort() 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
R=lambda:list(map(int,input().split())) n,m=R() x=range(n) c=0 d=0 l=0 r=0 f=sorted([R() for i in range(n)]) while(n>l): while(n>r and f[r][0]-f[l][0]<m): c+=f[r][1] r+=1 d=max(d,c) c-=f[l][1] l+=1 print(d)
8
PYTHON3
n,d = map(int, input().split()) friends = [] s, ans, ind = 0, 0, 0 for i in range(n): friends.append((list(map(int, input().split())))) friends.sort(key=lambda numb: numb[0]) for j in range(n): s += friends[j][1] while friends[j][0] - friends[ind][0] >= d: s -= friends[ind][1] ind += 1 ans = max(ans, s) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; vector<pair<int, int>> v(n); for (int i = 0; i < n; i++) { int m, s; cin >> m >> s; v[i] = make_pair(m, s); } sort(v.begin(), v.end()); int l = 0; unsigned long long res = 0; unsigned long long sum = 0; for (int r = 0; r < v.size(); r++) { sum += v[r].second; while (v[l].first + d <= v[r].first) { sum -= v[l].second; l++; } res = max(sum, res); } cout << res; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct g { long long m, f; }; bool comp(g g1, g g2) { return g1.m < g2.m; } int main() { long long n, d; cin >> n >> d; g g1; vector<g> k; for (long long i = 0; i < n; i++) { cin >> g1.m >> g1.f; k.push_back(g1); } sort(k.begin(), k.end(), comp); if (n == 1) { cout << k[0].f << endl; return 0; } long long j = 0; long long i = 0; long long temp = 0; long long ans = -1; while (j != n && i != n) { while (k[i].m > k[j].m - d) { temp += k[j].f; j++; if (j == n) break; } ans = max(ans, temp); temp -= k[i].f; i++; } cout << ans << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool compFirst(pair<long long, long long> a, pair<long long, long long> b) { return a.first < b.first; } int main() { long long n, d; vector<pair<long long, long long> > friends; cin >> n >> d; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; friends.push_back(make_pair(a, b)); } sort(friends.begin(), friends.end(), compFirst); vector<long long> sums; sums.push_back(0); for (int i = 0; i < n; ++i) sums.push_back(sums[i] + friends[i].second); long long index = 0, max = 0; for (int i = 0; i < n; ++i) { for (; index == n - 1 || friends[index + 1].first < friends[i].first + d; ++index) { if (index == n - 1) break; } if (sums[index + 1] - sums[i] > max) max = sums[index + 1] - sums[i]; } cout << max << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; pair<long long, long long> ps[100005]; int n; int main() { long long sum[100005]; long long m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> ps[i].first >> ps[i].second; } sort(ps, ps + n); for (int i = 0; i < n; i++) { sum[i + 1] = sum[i] + ps[i].second; } int loc = 0; long long ans = 0; for (int i = 0; i < n; i++) { while (loc < n && ps[loc].first < ps[i].first + m) loc++; ans = max(ans, sum[loc] - sum[i]); } cout << ans; return 0; }
8
CPP
# coding: utf-8 # In[ ]: n, d = [int(x) for x in input().split()] friends = [] for _n in range(n): friends.append([int(x) for x in input().split()]) friends.sort(key=lambda x:x[0]) maxFriend = 0 curFriend = 0 start = 0 end = 0 while start < len(friends) and end < len(friends): if friends[end][0] - friends[start][0] >= d: curFriend -= friends[start][1] start += 1 else: curFriend += friends[end][1] end += 1 maxFriend = max(maxFriend, curFriend) print(maxFriend) # In[ ]:
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, d; cin >> n >> d; vector<pair<long long, long long> > v(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long x = 0; long long y = 0, m = 0; for (int i = 0; i < n; i++) { while (v[i].first - v[x].first >= d) { y -= v[x].second; x++; } y += v[i].second; m = max(m, y); } cout << m; }
8
CPP
n,d=list(map(int,input().split())) arr=[] for i in range(n): x,y=list(map(int,input().split())) arr.append([x,y]) arr.sort() s=0 temp=0 maxVal=0 for i in range(n): s+=arr[i][1] while (arr[i][0]-arr[temp][0]>=d): s-=arr[temp][1] temp+=1 maxVal=max(maxVal,s) print(maxVal)
8
PYTHON3
n,d = map(int,input().split()) friend = [list(map(int,input().split())) for _ in range(n)] 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(' ')) lst = [tuple(map(int, input().split(' '))) for _ in range(n)] lst.sort(key=lambda x: x[0]) left_index = 0 cur_frd = lst[left_index][1] mx = cur_frd cur_index = 1 while cur_index < n: while lst[cur_index][0]-lst[left_index][0] < d: cur_frd += lst[cur_index][1] cur_index += 1 if cur_frd > mx: mx = cur_frd if cur_index == n: break cur_frd -= lst[left_index][1] left_index += 1 print(mx)
8
PYTHON3
def byF(p1): x, y = p1 return x n, d = map(int, input().split()) friends = [] iSt = 0 s = 0 m = 0 for i in range(n): x, y = map(int, input().split()) friends.append((x, y)) friends.sort(key=byF) for i in range(n): s += friends[i][1] while friends[i][0] - friends[iSt][0] >= d: s -= friends[iSt][1] iSt += 1 if s > m: m = s print(m)
8
PYTHON3
n, d = map(int, input().split()) arr = sorted([list(map(int, input().split())) for i in range(n)]) j, s, res = 0, arr[0][1], arr[0][1] for i in range(1, n): while(arr[i][0] - arr[j][0] >= d): s -= arr[j][1] j += 1 s += arr[i][1] res = max(s, res) print(res)
8
PYTHON3
n ,m = map(int,input().split()) arr = list() for i in range(n): a,b = map(int,input().split()) arr.append([a,b]) arr.sort() res = arr[0][1] g = 0 M = -float('inf') i=1 while i<n: if arr[i][0]-arr[g][0] >= m: M = max(M,res) res-=arr[g][1] g+=1 else: res+=arr[i][1] i+=1 print(max(res,M))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 24; const int mod = 1e9 + 7; long long n, m, d; int main() { cin >> n >> d; pair<long long, long long> a[n]; for (int i = 0; i < n; i++) { long long m, f; cin >> m >> f; a[i].first = m; a[i].second = f; } sort(a, a + n); int i = 0; int j = n - 1; int l, r; long long ans = 0, maxi = 0; for (l = 0, r = 0; r < n;) { if (abs(a[l].first - a[r].first) < d) { ans += a[r].second; r++; } else { long long maxi = max(ans, maxi); ans -= a[l].second; l++; } maxi = max(maxi, ans); } cout << maxi << "\n"; ; }
8
CPP
import sys n, d = map(int, sys.stdin.readline().split()) f = dict() for _ in range(n): m, s = map(int, sys.stdin.readline().split()) if m in f: f[m] += s else: f[m] = s fkey = sorted(f.keys()) fkey_min = fkey[0] fkey_max = fkey[-1] fkey_tmp = [] for i in fkey: if i <= min(fkey_min+d-1,fkey_max): fkey_tmp.append(i) ans = sum([f[i] for i in fkey_tmp]) tmp = ans for i in range(len(fkey_tmp),len(fkey)): fkey_tmp.append(fkey[i]) tmp += f[fkey_tmp[-1]] while fkey_tmp[0] <= fkey_tmp[-1]-d: tmp -= f[fkey_tmp[0]] fkey_tmp.pop(0) ans = max(ans,tmp) print(ans)
8
PYTHON3
#------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase 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") #-------------------game starts now----------------------------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z n,k=map(int,input().split()) a=[0]*n b=[0]*n for i in range(n): a[i],b[i]=map(int,input().split()) b=sort_list(b,a) a.sort() start=0 ans=b[0] ma=0 i=1 while (i<n): if a[i]-a[start]<k: ans+=b[i] i+=1 else: ma=max(ma,ans) ans-=b[start] start+=1 ma=max(ma,ans) print(ma)
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] #print(l[k][0],l[i][0]) 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()) m = [0 for i in range(n)] s = [0 for i in range(n)] hash = [0 for i in range(n)] mod = 10**9 + 1 for i in range(n): m[i], s[i] = map(int,input().split()) hash[i] = m[i] * mod + s[i] # print(m,s) # print(hash) hash = sorted(hash) # print(hash) def get_value(x): return x%(mod) def get_key(x): return int(x/(mod)) i_max = n-1 i_min = n-1 S = 0 S_max = S while((i_min>=0)&(i_max>=0)): while ((get_key(hash[i_min]) > (get_key(hash[i_max]) - d)) & (i_min>=0)): S += get_value(hash[i_min]) i_min -= 1 if (S > S_max): S_max = S S -= get_value(hash[i_max]) i_max -= 1 print(S_max)
8
PYTHON3
n, d = map(int, input().split()) a = sorted([list(map(int, input().split())) for i in range(n)]) left = 0 cur_sum = 0 max_sum = 0 for right in range(n): cur_sum += a[right][1] while a[right][0] - a[left][0] >= d: cur_sum -= a[left][1] left += 1 if cur_sum > max_sum: max_sum = cur_sum print(max_sum)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, d, i, j; long long int xam = 0, sum = 0; cin >> n >> d; vector<pair<long long int, long long int> > v(n); for (i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; v[i].second = -v[i].second; } sort(v.begin(), v.end()); for (i = 0, j = 0; i < n && j < n;) { if ((v[j].first - v[i].first) < d) { sum += (-v[j].second); j++; xam = max(xam, sum); } else { sum -= (-v[i].second); i++; } } cout << xam; return 0; }
8
CPP
n,d=map(int,input().split(" ")) mymap=dict() v=[] t=[] for i in range(n): p,q=map(int,input().split(" ")) if(mymap.get(p,-1)==-1): v.append(p) t.append(int(0)) mymap[p]=q+mymap.get(p,int(0)) v.sort() sum=0 en=int(0) for i in range(len(v)): if i!=0 : t[i]=t[i-1]-mymap[v[i-1]] while en<len(v) and (v[en]<v[i]+d) : t[i]+= mymap[v[en]] en+=1 sum=max(sum,t[i]) print(sum)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { long long n; long long d; cin >> n >> d; vector<pair<long long, long long>> friends; long long money; long long factor; for (long long i = 0; i < n; i++) { cin >> money >> factor; friends.push_back(make_pair(money, factor)); } sort(friends.rbegin(), friends.rend()); long long total_factor = friends[0].second; long long temp_factor = friends[0].second; long long firstPointer = 0; long long secondPointer = 1; while (secondPointer < friends.size()) { if (friends[firstPointer].first - friends[secondPointer].first < d) { temp_factor = temp_factor + friends[secondPointer].second; secondPointer = secondPointer + 1; ; } else { temp_factor = temp_factor - friends[firstPointer].second; firstPointer = firstPointer + 1; } total_factor = max(total_factor, temp_factor); } cout << total_factor << '\n'; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long n, d, start, sum, mx; pair<long long, long long> a[100100]; int main() { cin >> n >> d; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); sum = mx = a[0].second; for (int i = 1; i < n; i++) { if (a[i].first - a[start].first < d) { sum += a[i].second; mx = max(mx, sum); } else { while (a[i].first - a[start].first >= d) { sum -= a[start].second; start++; } sum += a[i].second; mx = max(mx, sum); } } cout << mx << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void Solve() { vector<pair<int, int>> vec; long long n, d, m, s, cr = 0, l = 0, r = 0, mx = 0; cin >> n >> d; for (long long i = 0; i < n; i++) { cin >> m >> s; vec.push_back(make_pair(m, s)); } sort(vec.begin(), vec.end()); for (long long l = 0; l < n && r < n; l++) { r = max(r, l); while (vec[r].first - vec[l].first < d && r < n) { cr += vec[r].second; mx = max(mx, cr); r++; } cr -= vec[l].second; } cout << mx; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { Solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int d; int n; struct Pon { long long x, y; } a[100100]; bool cmp(Pon A, Pon B) { if (A.x == B.x) return A.y < B.y; return A.x < B.x; } int main() { while (scanf("%d%d", &n, &d) != EOF) { long long mmax = 0; long long tot = 0; int pis = 0; for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &a[i].x, &a[i].y); } sort(a, a + n, cmp); long long pos = a[0].x; for (int i = 0; i < n; i++) { if (a[i].x == pos) { tot += a[i].y; } else { pos = a[i].x; mmax = max(tot, mmax); tot = a[i].y; } } mmax = max(tot, mmax); int first = a[0].x; int i = 0, j = 0; long long ans = 0; while (i < n && j < n) { while (a[i].x - first < d && a[i].x - first >= 0 && i < n) { ans += a[i].y; i++; } mmax = max(ans, mmax); while (a[i].x - a[j].x >= d && j < n) { ans -= a[j].y; j++; } first = a[j].x; } printf("%I64d\n", mmax); } return 0; }
8
CPP
import sys from bisect import bisect_right from itertools import accumulate def main(): n, d = map(int, input().split()) friends = sorted(tuple(int(c) for c in line.split()) for line in sys.stdin) inf = 1000000001 friendship_sum = [0] + list(accumulate(e for _, e in friends)) start = 0 _min, ans = friends[0] for i in range(1, n): cur = friends[i][0] if cur - _min >= d: start = bisect_right(friends, (cur - d, inf)) _min = friends[start][0] friendship = friendship_sum[i + 1] - friendship_sum[start] ans = max(ans, friendship) print(ans) if __name__ == "__main__": main()
8
PYTHON3
from collections import deque n,d = map(int,input().split()) a = [list(map(int, input().split())) for i in range(n)] a.sort() q = deque() ans = s = 0 for x,f in a: while q and x >= q[0][0] + d : s -= q[0][1] q.popleft() q.append((x,f)) s += f ans = max(s,ans) print(ans)
8
PYTHON3
n,de=map(int,input().split()) d={} for i in range(n): m,f=map(int,input().split()) d[m]=f if m not in d else d[m]+f # print(d) l=list(d.keys()) l.sort() a=0 maxf=0 curf=0 for b in range(len(l)): if l[b]>=de+l[a]: while(1): if l[b]-l[a]<de: break else: curf -= d[l[a]] a += 1 curf+=d[l[b]] maxf=max(maxf,curf) print(maxf)
8
PYTHON3
# take the input and split it in number of friends # and maximum difference firstLine = input().split(" ") noOfFriends = int(firstLine[0]) diff = int(firstLine[1]) friends = [] # place the amount of money and the friendship factor # in an array of pairs for index in range(noOfFriends): desc = input().split(" ") friends.append((int(desc[0]), int(desc[1]))) # sort the array by the amount of money friends.sort() # initialise pointers, friendship counter and maximum left = 0 right = 1 maxFriendship = friends[0][1] friendship = friends[0][1] # go through all subsequences of friends with similar amounts # of money and find the one with the biggest friendship factor while (right <= noOfFriends - 1): friendship += friends[right][1] while(friends[right][0] - friends[left][0] >= diff): friendship -= friends[left][1] left += 1 if(maxFriendship < friendship): maxFriendship = friendship right += 1 print(maxFriendship)
8
PYTHON3
class Friend: def __init__(self, m, s): self.m = m self.s = s n, d = list(map(int, input().split())) friends = [] for i in range(n): m, s = list(map(int, input().split())) friends.append(Friend(m, s)) friends.sort(key=lambda f: f.m) max_score = 0 right = 0 curr = 0 for left in range(n): while right < n and friends[right].m < d + friends[left].m: curr += friends[right].s right += 1 max_score = max(max_score, curr) curr -= friends[left].s print(max_score)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const long long INF = (long long)(1e17); const double PI = 3.141592653589793; long long dx[4] = {-1, 0, 1, 0}; long long dy[4] = {0, 1, 0, -1}; string path = "URDL"; void solve() { long long n, d; cin >> n >> d; vector<pair<long long, long long> > arr(n); for (auto& e : arr) cin >> e.first >> e.second; sort(arr.begin(), arr.end()); long long l = 0, r = 0; long long temp = 0, ans = 0; for (; l < n; temp -= arr[l].second, l++) { while (r < n and (arr[r].first - arr[l].first) < d) temp += arr[r].second, r++; ans = max(ans, temp); } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct peo { int m; long long s; } a[100005]; bool cmp(peo a, peo b) { return a.m < b.m; } int main() { int n, d, pos1, pos2; long long sum = 0, mx; scanf("%d%d", &n, &d); for (int i = 0; i < n; i++) { scanf("%d%I64d", &a[i].m, &a[i].s); } sort(a, a + n, cmp); pos1 = 0; pos2 = 0; mx = -1; while (pos2 <= n) { sum += a[pos2].s; while (a[pos2].m - a[pos1].m >= d) { sum -= a[pos1].s; pos1++; } pos2++; if (sum > mx) { mx = sum; } } printf("%I64d\n", mx); }
8
CPP
import bisect n,d=map(int,input().split()) ms=[list(map(int,input().split())) for i in range(n)] ms.sort() m=[ms[i][0] for i in range(n)] s=[0]+[ms[i][1] for i in range(n)] ss=s for i in range(1,n+1): ss[i]+=ss[i-1] ans=0 for i in range(n): idx=bisect.bisect_left(m,m[i]+d) ans=max(ans,ss[min(idx,n)]-ss[i]) print(ans)
8
PYTHON3
n, d = [int(x) for x in input().rstrip().split(' ')] friends = [] for i in range(n): friends.append([int(x) for x in input().rstrip().split(' ')]) friends.sort(key=lambda x: x[0]) s_max = friends[0][1] i_begin = 0 i_end = 1 s_curr = s_max while i_end < n: d_max = friends[i_begin][0] + d while (i_end < n and friends[i_end][0] < d_max): s_curr += friends[i_end][1] i_end += 1 s_max = max(s_max,s_curr) s_curr -= friends[i_begin][1] i_begin += 1 print(s_max)
8
PYTHON3
''' CodeForces 580B Kefa and Company Tags: Sortings, dp, Maximum Consecutive Sum ''' from sys import stdin def solve(people, d): ls = sorted(people, key=lambda p: p[0]) ans = ls[0][1] acc = 0 last = -1 for (idx, (m, s)) in enumerate(ls): while last >= 0 and idx - last > 0 and m - ls[last][0] >= d: acc -= ls[last][1] last += 1 acc += s if last < 0 or idx - last <= 0: last = idx ans = max(ans, acc) return ans n, d = tuple(map(int, input().split())) friends = [ tuple(map(int, line.split())) for line in stdin.readlines() ] ans = solve(friends, d) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, d, m, s; cin >> n >> d; vector<pair<int, int> > v; vector<int> money; for (int i = 0; i < n; i++) { cin >> m >> s; pair<int, int> p = make_pair(m, s); v.push_back(p); money.push_back(m); } sort(v.begin(), v.end()); sort(money.begin(), money.end()); vector<unsigned long long> pref; pref.push_back(0); int i; for (i = 0; i < n; i++) { pref.push_back(v[i].second + pref[i]); } for (i = 0; i <= n; i++) { } unsigned long long max = 0; int index; for (i = 0; i < n; i++) { vector<int>::iterator up; up = upper_bound(money.begin(), money.end(), v[i].first + d - 1); if (pref[up - money.begin()] - pref[i] > max) max = pref[up - money.begin()] - pref[i]; } cout << max << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, d, ff = 1, bb = 1; long long mx; pair<long long, long long> f[100005]; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d%d", &f[i].first, &f[i].second); sort(f + 1, f + n + 1); for (int i = 2; i <= n; i++) f[i].second += f[i - 1].second; while (true) { while (f[bb].first - f[ff].first < d && bb != n + 1) bb++; mx = max(mx, f[bb - 1].second - f[ff - 1].second); ff++; if (ff == n + 1) break; } printf("%I64d", mx); return 0; }
8
CPP
#include <bits/stdc++.h> template <class T> struct greater { bool operator()(const T& x, const T& y) const { return x > y; } }; using namespace std; int n, d; long long int res, ii, s; vector<pair<long long int, long long int>> a; 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()); s = 0, ii = 0; for (int i = 0; i < n; i++) { s += a[i].second; while (a[i].first - a[ii].first >= d) { s -= a[ii].second; ii++; if (ii == n) break; } res = max(res, s); } cout << res << endl; return 0; }
8
CPP
import operator class PII(): def __init__(self,F,S): self.F=F self.S=S if __name__=="__main__": n,d=[int(x) for x in input().split()] a=[] for i in range(n): F,S=[int(x) for x in input().split()] a.append(PII(F,S)) a.sort(key=operator.attrgetter('F')) idx=0 mx=a[0].S amt=a[0].S for i in range(1,n): if(a[i].F-a[idx].F<d): amt+=a[i].S else: mx=max(mx,amt) j=idx while(a[i].F-a[j].F>=d): amt-=a[j].S j+=1 idx=j amt+=a[i].S mx=max(mx,amt) print(mx)
8
PYTHON3
##2 pointers in sequence n, d = map(int, input().split()) m_s = sorted(tuple(map(int, input().split())) for _ in range(n)) s, max_s, i = 0, 0, 0 for j in range(n): s += m_s[j][1] while m_s[j][0] - m_s[i][0] >= d: s -= m_s[i][1] i += 1 max_s = max(max_s, s) print(max_s)
8
PYTHON3
n = input().split(' ') n,d = int(n[0]), int(n[1]) a = [0]*n for i in range(n): m = input().split(' ') a[i] = [int(m[0]), int(m[1])] a.sort() maxD = 0 count = a[0][1] i,j = 0, 1 #print(a) while j < n: #print(count) if a[j][0] - a[i][0] < d: count += a[j][1] j+=1 else: maxD = max(count, maxD) count -= a[i][1] i+=1 print( max(count, maxD))
8
PYTHON3