1989D - Smithing Skill - CodeForces Solution


brute force data structures dp greedy math sortings two pointers *1900

Please click on ads to support us..

Python Code:

import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s); sys.stdout.write('\n')
def wi(n): sys.stdout.write(str(n)); sys.stdout.write('\n')
def wia(a, sep=' '): sys.stdout.write(sep.join([str(x) for x in a])); sys.stdout.write('\n')


def solve(n, m, a, b, c):
    max_a = max(a)
    M = max_a + 1

    best = [M] * M
    for i in range(n):
        diff = a[i] - b[i]
        best[a[i]] = min(best[a[i]], diff)
        
    ans = [0] * M
    waste = M
    for i in range(M):
        waste = min(waste, best[i])
        if waste < M:
            ans[i] = 2 + ans[i - waste]

    res = 0
    for ci in c:
        if ci < M:
            res += ans[ci]
        else:
            v = 1 + (ci - M) // waste
            ci -= v * waste
            res += ans[ci] + 2 * v

    return res


def main():
    n, m = ria()
    a = ria()
    b = ria()
    c = ria()
    wi(solve(n, m, a, b, c))


if __name__ == '__main__':
    main()


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush