File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import math
2+ import sys
3+
4+ read = lambda : sys .stdin .readline ().rstrip ()
5+
6+
7+ class Problem :
8+ def __init__ (self ):
9+ self .c , self .n = map (int , read ().split ())
10+ self .cases = [list (map (int , read ().split ())) for _ in range (self .n )]
11+
12+ def solve (self ) -> None :
13+ dp = [0 if idx == 0 else math .inf for idx in range (self .c + 101 )]
14+
15+ for cost , customer in self .cases :
16+ for idx in range (customer , self .c + 101 ):
17+ dp [idx ] = min (dp [idx ], dp [idx - customer ] + cost )
18+
19+ print (min (dp [self .c :]))
20+
21+
22+ if __name__ == "__main__" :
23+ Problem ().solve ()
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "input" : [
4+ " 12 2" ,
5+ " 3 5" ,
6+ " 1 1"
7+ ],
8+ "expected" : [
9+ " 8"
10+ ]
11+ },
12+ {
13+ "input" : [
14+ " 10 3" ,
15+ " 3 1" ,
16+ " 2 2" ,
17+ " 1 3"
18+ ],
19+ "expected" : [
20+ " 4"
21+ ]
22+ },
23+ {
24+ "input" : [
25+ " 10 10" ,
26+ " 1 1" ,
27+ " 2 2" ,
28+ " 3 3" ,
29+ " 4 4" ,
30+ " 5 5" ,
31+ " 6 6" ,
32+ " 7 7" ,
33+ " 8 8" ,
34+ " 9 9" ,
35+ " 10 10"
36+ ],
37+ "expected" : [
38+ " 10"
39+ ]
40+ },
41+ {
42+ "input" : [
43+ " 100 6" ,
44+ " 4 9" ,
45+ " 9 11" ,
46+ " 3 4" ,
47+ " 8 7" ,
48+ " 1 2" ,
49+ " 9 8"
50+ ],
51+ "expected" : [
52+ " 45"
53+ ]
54+ }
55+ ]
Original file line number Diff line number Diff line change 1+ import json
2+ import os .path
3+ import unittest
4+ from io import StringIO
5+ from unittest .mock import patch
6+
7+ from parameterized import parameterized
8+
9+ from main import Problem
10+
11+
12+ def load_sample (filename : str ):
13+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), filename )
14+
15+ with open (path , "r" ) as file :
16+ return [(case ["input" ], case ["expected" ]) for case in json .load (file )]
17+
18+
19+ class TestCase (unittest .TestCase ):
20+ @parameterized .expand (load_sample ("sample.json" ))
21+ def test_case (self , case : str , expected : list [str ]):
22+ # When
23+ with (
24+ patch ("sys.stdin.readline" , side_effect = case ),
25+ patch ("sys.stdout" , new_callable = StringIO ) as output ,
26+ ):
27+ Problem ().solve ()
28+
29+ result = output .getvalue ().rstrip ()
30+
31+ # Then
32+ self .assertEqual ("\n " .join (expected ), result )
33+
34+
35+ if __name__ == "__main__" :
36+ unittest .main ()
You can’t perform that action at this time.
0 commit comments