Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
for i in range(xkd[0] // xkd[2]):
    xkd[0] -= xkd[2]
    xkd[1] -= 1
    if xkd[1] < 1:
      break
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if ans > xkd[0] + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

### FIXED CODE ###

X,K,D=map(int,input().split())
X=abs(X)
straight=min(K,X//D)
K-=straight
X-=straight*D
if K%2==0:
    print(X)
else:
    print(D-X)
### BUGGY CODE ###

ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
import sys
sys.setrecursionlimit(10**6)

n = ni()
def idx(u,v):
    return u*n+v
tree = [[] for i in range(n**2)]
go=[False]*n**2
back=[False]*n**2
memo = [1]*n**2 #memo[i] iからの深さ
start = []
for i in range(n):
    A1 = lma()
    pu,pv = i,A1[0]-1
    if pu>pv:pu,pv=pv,pu
    start.append(idx(pu,pv))
    for j in range(1,n-1):
        u,v = i,A1[j]-1
        if u>v:u,v=v,u
        tree[idx(pu,pv)].append(idx(u,v))
        pu,pv=u,v
        start.append(idx(pu,pv))
#print(tree)
def dfs(s):
    go[s]=True
    for node in tree[s]:
        if not go[node]:
            memo[s] = max(dfs(node)+1,memo[s])
        else:
            if back[node]:
                memo[s] = max(memo[node]+1,memo[s])
            else:
                print(-1)
                exit()
    back[s]=True
    return memo[s]
ans = 0
for s in start:
    if not go[s]:
        ans = max(ans,dfs(s))
print(ans)


### FIXED CODE ###

ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
import sys
sys.setrecursionlimit(10**6)

n = ni()
def idx(u,v):
    return u*n+v
tree = [[] for i in range(n**2)]
go=[False]*n**2
back=[False]*n**2
memo = [1]*n**2 #memo[i] iからの深さ
start = []
for i in range(n):
    A1 = lma()
    pu,pv = i,A1[0]-1
    if pu>pv:pu,pv=pv,pu
    start.append(idx(pu,pv))
    for j in range(1,n-1):
        u,v = i,A1[j]-1
        if u>v:u,v=v,u
        tree[idx(pu,pv)].append(idx(u,v))
        pu,pv=u,v
        start.append(idx(pu,pv))
#print(tree)
def dfs(s):
    go[s]=True
    for node in tree[s]:
        if not go[node]:
            memo[s] = max(dfs(node)+1,memo[s])
        else:
            if back[node]:
                memo[s] = max(memo[node]+1,memo[s])
            else:
                print(-1)
                exit()
    back[s]=True
    return memo[s]
ans = 0
for s in start:
    if not go[s]:
        ans = max(ans,dfs(s))
print(ans)
### BUGGY CODE ###

n = int(input())
pList = list(map(int,input().split()))

count = 1
for i in range(1,n-1):
  if min(pList[:i]) >= pList[i]:
    count += 1

if len(pList) != 1:
    if min(pList[:-1]) >= pList[-1]:
      count += 1

print(count)


### FIXED CODE ###

n = int(input())
pList = list(map(int,input().split()))

count = 0
minL = pList[0] 
for i in range(n):
  if minL >= pList[i]:
      count += 1
      minL = pList[i] 
      if minL == 1:
        break

print(count)
### BUGGY CODE ###

from collections import deque

def main():    
    nums = {}
    N = int(input())
    count = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            nums[(i, j)] = count
            count += 1
    
    graph = {i:deque() for i in range(count)}
    input_num = [0] * count
    
    for i in range(1, N+1):
        A = list(map(int, input().split()))
    
        for j in range(N-2):
            a, b = i, A[j]
            if a > b: a, b = b, a
            
            c, d = i, A[j+1]
            if c > d: c, d = d, c
        
            n = nums[(c, d)]        
            graph[nums[(a, b)]].append(n)
            input_num[n] += 1
        
    q = deque([(i, 1) for i in range(count) if not input_num[i]])
    size = len(q)
    dist = [0] * count
    
    while q:
        v, c = q.popleft()
        c += 1
        for u in graph[v]:
            input_num[u] -= 1
            dist[u] = max(dist[u], c)
            if not input_num[u]:
                q.append((u, c))
                size += 1
    
    if size != count: ans = -1
    else: ans = max(dist)
    
    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()
### BUGGY CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 073 C
def getInt(): return int(input())


def getIntLines(n): return [int(input()) for i in range(n)]


def zeros(n): return [0]*n


def genBits(n):  # n個の0/1のリストを生成　[0]から変化
    bits = zeros(n)
    for i in range(2**n):
        for k in range(n):
            bits[k] = i % 2
            i //= 2
        yield bits


def dmp(x):
    global debug
    if debug:
        print(x)


def probC():
    N = getInt()
    S = getIntLines(N)
    dmp((N, S))
    maxPoint = 0
    for bit in genBits(N):
        point = 0
        for i in range(N):
            point += S[i]*bit[i]
        if point % 10 == 0:
            point = 0
        maxPoint = max(point, maxPoint)
        dmp((point, maxPoint))
    return maxPoint


debug = False
print(probC())


### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 073 C
def getInt(): return int(input())


def getIntLines(n): return [int(input()) for i in range(n)]


def zeros(n): return [0]*n


def genBits(n):  # n個の0/1のリストを生成　[0]から変化
    bits = zeros(n)
    for i in range(2**n):
        for k in range(n):
            bits[k] = i % 2
            i //= 2
        yield bits


def dmp(x):
    global debug
    if debug:
        print(x)


def probC_TLE():
    N = getInt()
    S = getIntLines(N)
    dmp((N, S))
    maxPoint = 0
    for bit in genBits(N):
        point = 0
        for i in range(N):
            point += S[i]*bit[i]
        if point % 10 == 0:
            point = 0
        maxPoint = max(point, maxPoint)
        dmp((point, maxPoint))
    return maxPoint


def probC():
    N = getInt()
    S = getIntLines(N)
    dmp((N, S))
    tens = []
    nonTens = []
    for i in range(N):
        if S[i] % 10 == 0:
            tens.append(S[i])
        else:
            nonTens.append(S[i])
    total = sum(nonTens)
    dmp(total)
    if total > 0 and total % 10 == 0:
        total -= min(nonTens)
    dmp(total)
    if total > 0:
        total += sum(tens)
    dmp(total)
    return total


debug = False
print(probC())
### BUGGY CODE ###

N, K = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
dp = [0]*N

for i in range(1,N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(i - K, 0), i))

print(dp[-1])


### FIXED CODE ###

def solve():
  N, K = [int(i) for i in input().split()]
  H = [int(i) for i in input().split()]
  dp = [0] * N
 
  for i in range(1, N):
    dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i - K, 0), i))
 
  return dp[N - 1]
 
print(solve())
### BUGGY CODE ###

import math

def main():
    # tt = int(input())
    
    tt = 1
    for _ in range(0, tt):
        n = int(input())
        
        arr = input().split(' ')
        ans = 0
        mult = 1
        j = 0

        for i in range(0, n):
            mult *= int(arr[i])

        if mult > 10**18:
            mult = -1
        print(mult)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import math

def main():
    # tt = int(input())
    
    tt = 1
    for _ in range(0, tt):
        n = int(input())
        
        arr = input().split(' ')
        arr = sorted(arr)
        mult = 1
        for i in range(0, n):
            mult *= int(arr[i])

            if mult > 10**18:
                mult = -1
                break

        print(mult)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]

min = sum(a)

for i in range(1,len(a)):
    
    if min > abs(sum(a[:i]) - sum(a[i:])):
        min = abs(sum(a[:i]) - sum(a[i:]))

print(min)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]

len_f = sum(a)
sum_a = 0
min = 2020202021

for i in range(n):
    sum_a += a[i]
    len_f -= a[i]
    if min > abs(sum_a - len_f):
        min = abs(sum_a - len_f)

print(min)
### BUGGY CODE ###

import sys
import itertools
import numpy as np
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
n = int(readline())
A = list(map(lambda x: x.split(), read().decode().split('\n')))
 
ids = [[0 for j in range(n)] for i in range(n)]
 
v = 0
for i in range(n):
    for j in range(i, n):
        ids[i][j] = v
        v += 1
 
 
# convert match to index-node num
V = []
for i in range(n):
    for j in range(n - 1):
        a = i
        b = int(A[i][j]) - 1
        if a > b:
            a, b = b, a
        A[i][j] = ids[a][b]
        V.append(A[i][j])
        
to = [[] for i in range(v)]
for i in range(n):
    for j in range(n - 2):
        to[A[i][j]].append(A[i][j + 1])
 
 
visited = [False for i in range(v)]
calculated = [False for i in range(v)]
dp = [0 for i in range(v)]
 
def dfs(node):
    if visited[node]:
        if not calculated[node]:
            print(-1)
            exit()
        return dp[node]
 
    visited[node] = True
    dp[node] = 1
    for u in to[node]:
        dp[node] = max(dfs(u) + 1, dp[node])
 
    calculated[node] = True
    return dp[node]
 
ans = 0
for i in V:
    ans = max(ans, dfs(i))
print(ans)

### FIXED CODE ###

import sys
import itertools
import numpy as np

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N = int(readline())
A = [[-1] + [int(x) - 1 for x in row.split()][::-1] for row in readlines()]

# A.append([-1])

def f(cand):
    cand1 = set()
    can_use = []
    for n in cand:
        if A[A[n][-1]][-1] == n:
            can_use.append(n)
    for n in can_use:
        A[n].pop()
        cand1.add(n)
        if A[n][-1] != -1:
            cand1.add(A[n][-1])
    return len(can_use), cand1

answer = 0
rest = N*(N-1)
cand = set(range(N))
while True:
    answer += 1
    x, cand = f(cand)
    if x == 0:
        answer = -1
        break
    rest -= x
    if rest == 0:
        break

print(answer)
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int , input().split()))

class Cumulative_Sum:
    def __init__(self, a):
        la = len(a)
        self.s = [0] * (la + 1)
        for i in range(la):
            self.s[i+1] = self.s[i] + a[i]
    # s[l]からs[r]までの和
    def sum(self, l, r):
        return self.s[r] - self.s[l]

cs = Cumulative_Sum(a)

ans = 0
for i in range(0, n):
    for j in range(i+1, n+1):
        if cs.sum(i, j) % m == 0:
            ans += 1
print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int , input().split()))

class Cumulative_Sum:
    def __init__(self, a):
        la = len(a)
        self.s = [0] * (la + 1)
        for i in range(la):
            self.s[i+1] = self.s[i] + a[i]
    # s[l]からs[r]までの和
    def sum(self, l, r):
        return self.s[r] - self.s[l]

def nCr(n, r, mod=67280421310721):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod


cs = Cumulative_Sum(a)
from collections import defaultdict
b = defaultdict(int)

for i in range(n+1):
    b[cs.s[i] % m] += 1
#print(cs.s)
#print(b)
ans = 0
for bi in b:
    if b[bi] > 1:
        ans += nCr(b[bi], 2)
        #print(bi, b[bi])
    #    ans += b[bi]
print(ans)

"""
for i in range(n+1):
    if cs.s[i] % m == 0:
        ans += 1
    print(cs.s[i] % m)
print(ans)
"""
"""
print('---')
ans = 0
for i in range(n):
    for j in range(i+1, n+1):
        if cs.sum(i, j) % m == 0:
            print(i, j, cs.s[i]%m, cs.s[j]%m)
            #ans += 1
#print(ans)
"""
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import deque
H,W,K = map(int,input().split())
sx,sy,gx,gy = map(int,input().split())
sx-=1;sy-=1;gx-=1;gy-=1
Pond = []; INF = float("inf")
for i in range(H):
  temp = str(input());temp = list(temp)
  Pond.append(temp)
#print(Pond)
Q = deque([])
Q.append([sx,sy])
reach = [[INF]*W for _ in range(H)]
reach[sx][sy] = 0
dxdy = [(1,0),(-1,0),(0,1),(0,-1)]
while Q:
  nx,ny = Q.popleft()
  step = K
  for i in range(4):
    tx,ty = nx,ny
    for fac in range(1,K+1):
      tx = nx + fac*dxdy[i][0]
      ty = ny + fac*dxdy[i][1]
      #print(i,tx,ty)
      if 0>tx or tx>=H or 0>ty or ty>=W or Pond[tx][ty] == "@": #or reach[tx][ty] != INF:
        #print(i,tx,ty,reach[2][0])
        break #範囲外やハスがあった、到達済みの場合には終了
      #print(i,tx,ty)
      if reach[nx][ny] + 1 > reach[tx][ty]: #もし現時点よりもステップが大きい場合はだめ。
        #同じ場合は向き的に得している場合もあるので残す。
        break
      reach[tx][ty] = reach[nx][ny] + 1
      Q.append([tx,ty])
#print(reach)
ans = reach[gx][gy]
if ans == INF:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

from collections import deque
H,W,K= map(int,input().split())
sx,sy,gx,gy = map(int,input().split())
sx-=1;sy-=1;gx-=1;gy-=1 #0index
Pond = []
for i in range(H):
  temp = str(input())
  temp = list(temp)
  Pond.append(temp)
#print(Pond)
INF = float("inf")
reach = [[INF for _ in range(W)] for _ in range(H)]
reach[sx][sy] = 0
dxdy = [(1,0),(-1,0),(0,1),(0,-1)]
Q = deque([(sx,sy)])
while Q:
  x,y = Q.popleft()
  #print(x,y)
  #step = reach[x][y]
  for j in range(4):
    for i in range(1,K+1):
      fac = i
      nx = x+fac*dxdy[j][0]
      ny = y+fac*dxdy[j][1]
      #print(nx,ny)
      if nx<0 or nx>=H or ny<0 or ny>= W or Pond[nx][ny] == "@":
        break
      if reach[x][y]+1 > reach[nx][ny]: #すでに余裕をもって到達済み
        break
      if reach[x][y]+1 < reach[nx][ny]:
        reach[nx][ny] = min(reach[x][y]+1,reach[nx][ny])
        Q.append((nx,ny))
      if nx == gx and ny == gy:
        #print(reach)
        print(reach[nx][ny])
        exit()
print(-1)
### BUGGY CODE ###

'''
Created on 2020/08/26

@author: harurun
'''
def main():
  import math
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N,P=map(int,pin().split())
  t=int(P**(1/N))+1
  if N==1:
    print(P)
    return 
  while t>0:
    s=P
    for i in range(N):
      if s%t!=0:
        break
      s=s//t
    else:
      print(t)
      return
    t-=1
  print(1)
  return 

main()

### FIXED CODE ###

'''
Created on 2020/08/26

@author: harurun
'''
def factorize(n):
    b = 2
    fct = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            fct.append(b)
        b = b + 1
    if n > 1:
        fct.append(n)
    return fct

def main():
  import math
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  N,P=map(int,pin().split())
  l=factorize(P)
  d=list(set(l))
  ans=1
  for i in d:
    t=l.count(i)
    if t>=N:
      ans*=i**(int(t/N))
  print(ans)
  return

main()
### BUGGY CODE ###

import collections

h, n = map(int, input().split())

INF = 10 ** 20

dp = collections.defaultdict(lambda : INF)
dp[0] = 0

magic = []

for i in range(n):
    magic.append(list(map(int, input().split())))



que = [0]
cc = [0]
m = 0
c = 0
while que:
    next_que = set()
    for now_dam in que:
        for a, b in magic:
            dam = now_dam + a
            if dp[dam] > dp[now_dam] + b:
                dp[dam] = dp[now_dam] + b
                if h > dam:
                    next_que.add(dam)
    que = list(next_que)
ans = INF

for k, v in dp.items():
    if k >= h:
        ans = min([v, ans])

print(ans)

### FIXED CODE ###

def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))
 
 
main()
### BUGGY CODE ###

import math
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}



n, m = map(int, input().split())

a, b = [0] * m, [0] * m
for i in range(m):
    a[i],b[i] = map(int,input().split())

uni = UnionFind(n)
def C(n,k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n-k)

f =[C(n,2)]
for i in range(m-1,-1,-1):
    com = C(n,2)
    na, nb = a[i]-1,b[i]-1

    uni.union(na, nb)
    members = uni.all_group_members()
    for v,w in members.items():
        if len(w)>1:
            com -= C(len(w),2)

    f.append(com)

f.reverse()
print(*f[1:])


### FIXED CODE ###

import math
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}



n, m = map(int, input().split())

a, b = [0] * m, [0] * m

for i in range(m):
    a[i],b[i] = map(int,input().split())

uni = UnionFind(n)

def C(n):
    return n*(n-1)//2

f =[C(n)]
com = C(n)
for i in range(m-1,-1,-1):

    na, nb = a[i]-1,b[i]-1
    if not uni.same(na,nb):
        n1 = uni.size(na)
        n2 = uni.size(nb)
        uni.union(na,nb)

        com -= n1*n2
    # print(n1, n2)

    f.append(com)

f.reverse()
print(*f[1:])
### BUGGY CODE ###

from fractions import gcd

def lcm(x, y):
    return (x // gcd(x, y)) * y

MOD = 10 ** 9 + 7  
n = int(input())
A = list(map(int, input().split()))

A.sort()

l = 1
for i in A:
  if i != l:
    l = lcm(l, i)

num = [0] * (10 ** 6 + 1)

ans = 0
for i in A:
  if num[i] == 0:
    v = l // i
    num[i] = v
  else:
    v = num[i]
  ans = (ans + v) % MOD

print(ans)

### FIXED CODE ###

from fractions import gcd

def lcm(x, y):
  return (x * y) // gcd(x, y)

MOD = 10 ** 9 + 7  
n = int(input())
A = list(map(int, input().split()))

l = 1
for i in A:
  l = lcm(l, i)
l %= MOD

ans = 0
for i in A:
  ans += l * pow(i, MOD - 2, MOD)
  ans %= MOD
print(ans)
### BUGGY CODE ###

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a = [False]*N
b = [False]*M

ans = 0
now = 0
bf = 0

for i in range(N):
    if now + A[i] > K:
        break
    now += A[i]
    ans += 1
    a[i] = True

for i in range(M):
    if now + B[i] > K:
        break
    now += B[i]
    ans += 1
    b[i] = True
    bf = i

if all(a) and all(b):
    print(ans)
    exit()

if not any(a) and not any(b):
    print(ans)
    exit()

n = K - now
ans2 = ans
for i in reversed(range(N)):
    if a[i]:
        a[i] = False
        n += A[i]
        ans2 -= 1
        for j in range(bf,M):
            if not b[j]:
                if n - B[j] < 0:
                    break
                b[j] = True
                n -= B[j]
                ans2 += 1
    if ans2 > ans:
        ans = ans2

print(ans)



### FIXED CODE ###

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a = [False]*N
b = [False]*M

ans = 0
now = 0
bf = 0

for i in range(N):
    if now + A[i] > K:
        break
    now += A[i]
    ans += 1
    a[i] = True

for i in range(M):
    if now + B[i] > K:
        break
    now += B[i]
    ans += 1
    b[i] = True
    bf = i

if all(a) and all(b):
    print(ans)
    exit()

if not any(a) and not any(b):
    print(ans)
    exit()

n = K - now
ans2 = ans
for i in reversed(range(N)):
    if a[i]:
        a[i] = False
        n += A[i]
        ans2 -= 1
        for j in range(bf,M):
            if not b[j]:
                if n - B[j] < 0:
                    break
                b[j] = True
                bf = j
                n -= B[j]
                ans2 += 1
    if ans2 > ans:
        ans = ans2

print(ans)
### BUGGY CODE ###

import bisect
import heapq

n, m = map(int, input().split())
a_list = []
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        a_list.append([a, b])
a_list.sort()
a_list_1 = [aa[0] for aa in a_list]
a_list_2 = [-aa[1] for aa in a_list]
ans = 0
heap_list = []
for i in range(1, m + 1):
    k = bisect.bisect_right(a_list_1, i) - 1
    if k >= 0:
        heap_list.extend(a_list_2[:k+1])
        del a_list_1[:k+1], a_list_2[:k+1]
    heapq.heapify(heap_list)
    if len(heap_list) > 0:
        ans -= heapq.heappop(heap_list)
print(ans)

### FIXED CODE ###

from collections import defaultdict
import heapq

a_dict = defaultdict(lambda: [])
n, m = map(int, input().split())
for _ in range(n):
    a, b = map(int, input().split())
    a_dict[a].append(-b)
ans = 0
heap_list = []
for i in range(1, m + 1):
    for aa in a_dict[i]:
        heapq.heappush(heap_list, aa)
    if len(heap_list) > 0:
        ans -= heapq.heappop(heap_list)
print(ans)