7 int time_cost_in_road_switching;
10 ProblemSolver(
int max_section,
int time_cost_in_road_switching,
11 String[] first_road, String[] second_road) {
12 this.max_section = max_section;
13 this.time_cost_in_road_switching = time_cost_in_road_switching;
14 this.first_road = first_road;
15 this.second_road = second_road;
21 public int minimum_time_recursively(
int time_costed,
int current_section, ArrayList<Road> roads) {
22 if (current_section == max_section + 1) {
27 Road current_road = roads.get(current_section);
28 if (current_road == Road.TuenMunRoad) {
29 int time_costed_in_TuenMunRoad = Integer.parseInt(first_road[current_section]);
30 int time_costed_in_CastlePeakRoad =
31 Integer.parseInt(second_road[current_section]) + time_cost_in_road_switching;
32 ArrayList<Road> first_copy =
new ArrayList<>(List.copyOf(roads));
33 ArrayList<Road> second_copy =
new ArrayList<>(List.copyOf(roads));
34 first_copy.add(Road.TuenMunRoad);
35 second_copy.add(Road.CastlePeakRoad);
37 minimum_time_recursively(
38 time_costed + time_costed_in_TuenMunRoad,
41 minimum_time_recursively(
42 time_costed + time_costed_in_CastlePeakRoad,
47 if (current_road == Road.CastlePeakRoad) {
48 int time_costed_in_TuenMunRoad =
49 Integer.parseInt(first_road[current_section]) + time_cost_in_road_switching;
50 int time_costed_in_CastlePeakRoad = Integer.parseInt(second_road[current_section]);
51 ArrayList<Road> first_copy =
new ArrayList<>(List.copyOf(roads));
52 ArrayList<Road> second_copy =
new ArrayList<>(List.copyOf(roads));
53 first_copy.add(Road.TuenMunRoad);
54 second_copy.add(Road.CastlePeakRoad);
56 minimum_time_recursively(
57 time_costed + time_costed_in_TuenMunRoad,
60 minimum_time_recursively(
61 time_costed + time_costed_in_CastlePeakRoad,
73 InputStreamReader reader =
new InputStreamReader(System.in);
74 BufferedReader buffer =
new BufferedReader(reader);
75 int num_road_interchanges = Integer.parseInt(buffer.readLine());
76 int time_cost_in_road_switching = Integer.parseInt(buffer.readLine());
77 String[] time_TuenMunRoads = buffer.readLine().split(
" ");
78 String[] time_CastlePeakRoads = buffer.readLine().split(
" ");
80 return new ProblemSolver(num_road_interchanges ,
81 time_cost_in_road_switching, time_TuenMunRoads, time_CastlePeakRoads);
83 public static void main(String[] args)
throws IOException {
85 System.out.println(
"The minimum time needed is "
87 problem.minimum_time_recursively(0,
88 0,
new ArrayList<>(List.of(ProblemSolver.Road.TuenMunRoad))),
89 problem.minimum_time_recursively(0,
90 0,
new ArrayList<>(List.of(ProblemSolver.Road.CastlePeakRoad)))
static void main(String[] args)
static ProblemSolver getProblemSolver()