Solo mid Oracle (Codeforces 1435E)
Tags: Greedy, ternary search

Meka-Naruto plays a computer game. His character has the following ability: given an enemy hero, deal $$$a$$$ instant damage to him, and then heal that enemy $$$b$$$ health points at the end of every second, for exactly $$$c$$$ seconds, starting one second after the ability is used. That means that if the ability is used at time $$$t$$$, the enemy's health decreases by $$$a$$$ at time $$$t$$$, and then increases by $$$b$$$ at time points $$$t + 1$$$, $$$t + 2$$$, ..., $$$t + c$$$ due to this ability.The ability has a cooldown of $$$d$$$ seconds, i. e. if Meka-Naruto uses it at time moment $$$t$$$, next time he can use it is the time $$$t + d$$$. Please note that he can only use the ability at integer points in time, so all changes to the enemy's health also occur at integer times only.The effects from different uses of the ability may stack with each other; that is, the enemy which is currently under $$$k$$$ spells gets $$$k\cdot b$$$ amount of heal this time. Also, if several health changes occur at the same moment, they are all counted at once.Now Meka-Naruto wonders if he can kill the enemy by just using the ability each time he can (that is, every $$$d$$$ seconds). The enemy is killed if their health points become $$$0$$$ or less. Assume that the enemy's health is not affected in any way other than by Meka-Naruto's character ability. What is the maximal number of health points the enemy can have so that Meka-Naruto is able to kill them?

Input: The first line contains an integer $$$t$$$ ($$$1\leq t\leq 10^5$$$) standing for the number of testcases.Each test case is described with one line containing four numbers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1\leq a, b, c, d\leq 10^6$$$) denoting the amount of instant damage, the amount of heal per second, the number of heals and the ability cooldown, respectively.

Output: For each testcase in a separate line print $$$-1$$$ if the skill can kill an enemy hero with an arbitrary number of health points, otherwise print the maximal number of health points of the enemy that can be killed.

Note: In the first test case of the example each unit of damage is cancelled in a second, so Meka-Naruto cannot deal more than 1 damage.In the fourth test case of the example the enemy gets:  $$$4$$$ damage ($$$1$$$-st spell cast) at time $$$0$$$;  $$$4$$$ damage ($$$2$$$-nd spell cast) and $$$3$$$ heal ($$$1$$$-st spell cast) at time $$$1$$$ (the total of $$$5$$$ damage to the initial health);  $$$4$$$ damage ($$$3$$$-nd spell cast) and $$$6$$$ heal ($$$1$$$-st and $$$2$$$-nd spell casts) at time $$$2$$$ (the total of $$$3$$$ damage to the initial health);  and so on. One can prove that there is no time where the enemy gets the total of $$$6$$$ damage or more, so the answer is $$$5$$$. Please note how the health is recalculated: for example, $$$8$$$-health enemy would not die at time $$$1$$$, as if we first subtracted $$$4$$$ damage from his health and then considered him dead, before adding $$$3$$$ heal.In the sixth test case an arbitrarily healthy enemy can be killed in a sufficient amount of time.In the seventh test case the answer does not fit into a 32-bit integer type.

Sample Inputs:
7
1 1 1 1
2 2 2 2
1 2 3 4
4 3 2 1
228 21 11 3
239 21 11 3
1000000 1 1000000 1

Sample Outputs:
1
2
1
5
534
-1
500000500000

Solution (Python):
import sys
input = sys.stdin.readline
T = int(input())
for case in range(T):
    (a, b, c, d) = list(map(int, input().split()))
    if a > b * c:
        print(-1)
        continue
    else:
        p = (a - 1) // (b * d)
        if p % 2 == 0:
            ans = (p + 1) * a - p // 2 * (p + 1) * d * b
            print(ans)
        else:
            ans = (p + 1) * a - p * ((p + 1) // 2) * d * b
            print(ans)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase

def main():
    for _ in range(inp()):
        (a, b, c, d) = invr()
        if a > b * c:
            print('-1')
            continue
        e = a // b
        f = e // d
        g = b * d
        print(a * (f + 1) - g * (f * (f + 1)) // 2)
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 inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    return input().strip()

def invr():
    return map(int, input().split())
if __name__ == '__main__':
    main()

Solution (Python):
def f(k, a, b, d):
    return a * (k + 1) - k * (k + 1) * b * d // 2

def main():
    t = int(input())
    for _ in range(t):
        (a, b, c, d) = [int(s) for s in input().split()]
        if a > c * b:
            print(-1)
        else:
            k = a // (b * d)
            print(f(k, a, b, d))
if __name__ == '__main__':
    main()