Combinatorics Homework (Codeforces 1574B)
Tags: Math, combinatorics

You are given four integer values $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$.Check if there exists a string that contains:   $$$a$$$ letters 'A';  $$$b$$$ letters 'B';  $$$c$$$ letters 'C';  no other letters;  exactly $$$m$$$ pairs of adjacent equal letters (exactly $$$m$$$ such positions $$$i$$$ that the $$$i$$$-th letter is equal to the $$$(i+1)$$$-th one). 

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.Each of the next $$$t$$$ lines contains the description of the testcase — four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$ ($$$1 \le a, b, c \le 10^8$$$; $$$0 \le m \le 10^8$$$).

Output: For each testcase print "YES" if there exists a string that satisfies all the requirements. Print "NO" if there are no such strings.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Note: In the first testcase strings "ABCAB" or "BCABA" satisfy the requirements. There exist other possible strings.In the second testcase there's no way to put adjacent equal letters if there's no letter that appears at least twice.In the third testcase string "CABBCC" satisfies the requirements. There exist other possible strings.

Sample Inputs:
3
2 2 1 0
1 1 1 1
1 2 3 2

Sample Outputs:
YES
NO
YES

Solution (Python):
import math
import sys
input = sys.stdin.readline
t = int(input())
for j in range(t):
    (a, b, c, d) = [int(x) for x in input().split()]
    if a <= b + c + 1 and b <= a + c + 1 and (c <= a + b + 1):
        m = a - 1
        n = b - 1
        o = c - 1
        if m + n + o >= d:
            print('YES')
        else:
            print('NO')
    else:
        if a > b and a > c:
            m = a - b - c - 1
            if d < m or d > a + b + c - 3:
                print('NO')
            else:
                print('YES')
        if b > a and b > c:
            m = b - a - c - 1
            if d < m or d > a + b + c - 3:
                print('NO')
            else:
                print('YES')
        if c > b and c > a:
            m = c - b - a - 1
            if d < m or d > a + b + c - 3:
                print('NO')
            else:
                print('YES')

Solution (Python):
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
    (a, b, c, m) = map(int, readline().split())
    (a, b, c) = sorted([a, b, c])
    if c - 1 - (a + b) <= m <= a + b + c - 3:
        print('YES')
    else:
        print('NO')

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
for s in [*open(0)][1:]:
    (*a, m) = map(int, s.split())
    print('NYOE S'[2 * max(a) - m - 2 < sum(a) > m + 2::2])