broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=max(per.get(cat,0),v)
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Only the last credit or debit in a category counts.

Input premium, items [[category, per mille; negative is a credit]], caps {category: max abs per mille}, max_credit, max_debit. Unknown category -> 'unknown category <name>'. Items are summed per category, each category clamped to its cap, the total clamped to [-max_credit, max_debit], and premium*(1000+total)/1000 rounded half-up. Return [total, premium].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: continue
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps'] and v>0: return 'unknown category '+cat
        if cat not in x['caps']: continue
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Unfiled schedule categories pass without an error.

Input premium, items [[category, per mille; negative is a credit]], caps {category: max abs per mille}, max_credit, max_debit. Unknown category -> 'unknown category <name>'. Items are summed per category, each category clamped to its cap, the total clamped to [-max_credit, max_debit], and premium*(1000+total)/1000 rounded half-up. Return [total, premium].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000-tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_credit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000,1000+tot)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A net credit raises the premium.

Input premium, items [[category, per mille; negative is a credit]], caps {category: max abs per mille}, max_credit, max_debit. Unknown category -> 'unknown category <name>'. Items are summed per category, each category clamped to its cap, the total clamped to [-max_credit, max_debit], and premium*(1000+total)/1000 rounded half-up. Return [total, premium].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=min(x['max_debit'],tot)
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    per={}
    for cat,v in x['items']:
        if cat not in x['caps']: return 'unknown category '+cat
        per[cat]=per.get(cat,0)+v
    tot=0
    for cat,v in sorted(per.items()):
        c=x['caps'][cat]
        tot+=max(-c,min(c,v))
    tot=max(-x['max_debit'],min(x['max_debit'],tot))
    return [tot, math.floor(x['premium']*Fraction(1000+tot,1000)+Fraction(1,2))]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['split credits in one category', {'premium': 589010, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 553669]], ['category cap binds on debit', {'premium': 872720, 'items': [['equipment', 30], ['equipment', 26], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 898902]], ['overall credit cap binds', {'premium': 194515, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 155612]], ['overall debit cap binds', {'premium': 730552, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 818218]], ['unknown credit category', {'premium': 820316, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 179351, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 275874, 'items': [['premises', -7], ['employees', -2], ['management', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-26, 268701]], ['no items', {'premium': 289888, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 289888]], ['credit within caps', {'premium': 802858, 'items': [['management', -31], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-38, 772349]]], [['split credits in one category', {'premium': 366527, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 344535]], ['category cap binds on debit', {'premium': 688814, 'items': [['equipment', 30], ['equipment', 27], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 709478]], ['overall credit cap binds', {'premium': 391467, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 313174]], ['overall debit cap binds', {'premium': 605730, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 678418]], ['unknown credit category', {'premium': 554487, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 377083, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 645018, 'items': [['premises', 12], ['employees', -6], ['management', 11]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [17, 655983]], ['no items', {'premium': 197679, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 197679]], ['credit within caps', {'premium': 746783, 'items': [['management', -30], ['employees', -7]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-37, 719152]]], [['split credits in one category', {'premium': 149318, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 140359]], ['category cap binds on debit', {'premium': 776580, 'items': [['equipment', 30], ['equipment', 28], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 799877]], ['overall credit cap binds', {'premium': 205836, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 164669]], ['overall debit cap binds', {'premium': 685528, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 767791]], ['unknown credit category', {'premium': 672687, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 231208, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 466715, 'items': [['premises', -26], ['employees', 7], ['management', 25]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [6, 469515]], ['no items', {'premium': 280573, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 280573]], ['credit within caps', {'premium': 847372, 'items': [['management', -15], ['employees', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-20, 830425]]], [['split credits in one category', {'premium': 259668, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 244088]], ['category cap binds on debit', {'premium': 544757, 'items': [['equipment', 30], ['equipment', 29], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 561100]], ['overall credit cap binds', {'premium': 105294, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 84235]], ['overall debit cap binds', {'premium': 228463, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 255879]], ['unknown credit category', {'premium': 336609, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 528562, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 111054, 'items': [['premises', -5], ['employees', -3], ['management', -26]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-34, 107278]], ['no items', {'premium': 432026, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 432026]], ['credit within caps', {'premium': 578818, 'items': [['management', -23], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-33, 559717]]], [['split credits in one category', {'premium': 838683, 'items': [['management', -50], ['management', -45], ['premises', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-60, 788362]], ['category cap binds on debit', {'premium': 190937, 'items': [['equipment', 30], ['equipment', 30], ['employees', -10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [30, 196665]], ['overall credit cap binds', {'premium': 878341, 'items': [['premises', -50], ['employees', -60], ['management', -80], ['equipment', -40], ['premises', -5]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 200, 'max_debit': 150}, [-200, 702673]], ['overall debit cap binds', {'premium': 467322, 'items': [['premises', 50], ['employees', 60], ['management', 60]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 120}, [120, 523401]], ['unknown credit category', {'premium': 352112, 'items': [['weather', -20], ['premises', 10]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['unknown debit category', {'premium': 280978, 'items': [['weather', 20]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, 'unknown category weather'], ['mixed small items', {'premium': 150014, 'items': [['premises', 12], ['employees', -23], ['management', 12]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [1, 150164]], ['no items', {'premium': 788931, 'items': [], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [0, 788931]], ['credit within caps', {'premium': 263345, 'items': [['management', -19], ['employees', -17]], 'caps': {'premises': 50, 'employees': 60, 'management': 80, 'equipment': 40}, 'max_credit': 250, 'max_debit': 150}, [-36, 253865]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Large combined credits exceed the plan's maximum schedule credit.

Input premium, items [[category, per mille; negative is a credit]], caps {category: max abs per mille}, max_credit, max_debit. Unknown category -> 'unknown category <name>'. Items are summed per category, each category clamped to its cap, the total clamped to [-max_credit, max_debit], and premium*(1000+total)/1000 rounded half-up. Return [total, premium].

split: open-access


prompt:
A long claim-free driver drops to class 0 and is rated with the worst coefficient via negative indexing.

Input start class 1..18 (else 'invalid class'), claims per year, coeffs (18 per mille values, class 1 best), base. Per year: 0 claims -> class-1 (floor 1); 3+ claims -> class 18; otherwise class+3*claims (ceiling 18). Return [class after each year, floor(base*coeff/1000+1/2) for the final class].

split: open-access


prompt:
Two claims in a year only move the policy up three classes.

Input start class 1..18 (else 'invalid class'), claims per year, coeffs (18 per mille values, class 1 best), base. Per year: 0 claims -> class-1 (floor 1); 3+ claims -> class 18; otherwise class+3*claims (ceiling 18). Return [class after each year, floor(base*coeff/1000+1/2) for the final class].

split: open-access


prompt:
A year with exactly three claims is rated as an ordinary penalty.

Input start class 1..18 (else 'invalid class'), claims per year, coeffs (18 per mille values, class 1 best), base. Per year: 0 claims -> class-1 (floor 1); 3+ claims -> class 18; otherwise class+3*claims (ceiling 18). Return [class after each year, floor(base*coeff/1000+1/2) for the final class].

split: open-access


prompt:
Renewals starting in class 18 are refused.

Input start class 1..18 (else 'invalid class'), claims per year, coeffs (18 per mille values, class 1 best), base. Per year: 0 claims -> class-1 (floor 1); 3+ claims -> class 18; otherwise class+3*claims (ceiling 18). Return [class after each year, floor(base*coeff/1000+1/2) for the final class].

split: open-access


prompt:
The reported class history lags one year behind the transitions.

Input start class 1..18 (else 'invalid class'), claims per year, coeffs (18 per mille values, class 1 best), base. Per year: 0 claims -> class-1 (floor 1); 3+ claims -> class 18; otherwise class+3*claims (ceiling 18). Return [class after each year, floor(base*coeff/1000+1/2) for the final class].

split: open-access


prompt:
The first installment is larger than the rest instead of the final one.

Input premium, n total payments including the down payment (n<1 -> 'invalid'; n==1 -> [premium], no fee), down (per mille), fee per installment. Down = premium*down/1000 rounded half-up; the rest splits over n-1 installments by floor division with the remainder added to the last installment; each installment after the down payment carries the fee.

split: open-access


prompt:
A 12-pay plan produces 13 payments.

Input premium, n total payments including the down payment (n<1 -> 'invalid'; n==1 -> [premium], no fee), down (per mille), fee per installment. Down = premium*down/1000 rounded half-up; the rest splits over n-1 installments by floor division with the remainder added to the last installment; each installment after the down payment carries the fee.

split: open-access


prompt:
The down payment is surcharged with an installment fee.

Input premium, n total payments including the down payment (n<1 -> 'invalid'; n==1 -> [premium], no fee), down (per mille), fee per installment. Down = premium*down/1000 rounded half-up; the rest splits over n-1 installments by floor division with the remainder added to the last installment; each installment after the down payment carries the fee.

split: open-access


prompt:
A single-payment plan bills premium plus an installment fee.

Input premium, n total payments including the down payment (n<1 -> 'invalid'; n==1 -> [premium], no fee), down (per mille), fee per installment. Down = premium*down/1000 rounded half-up; the rest splits over n-1 installments by floor division with the remainder added to the last installment; each installment after the down payment carries the fee.

split: open-access