Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static int[] maintenance;
- static int[] exchange;
- static int[] sell;
- static int[] profit;
- static int[] curr_engine;
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- int months = sc.nextInt();
- int engine = sc.nextInt();
- maintenance = new int[months+engine];
- exchange = new int[months+engine - 1];
- sell = new int[months+engine];
- for(int i = 0; i < months+engine; i++)
- {
- maintenance[i] = sc.nextInt();
- }
- for(int i = 0; i < months+engine- 1; i++)
- {
- exchange[i] = sc.nextInt();
- }
- for(int i = 0; i < months+engine; i++)
- {
- sell[i] = sc.nextInt();
- }
- sc.close();
- int routes = (int) Math.pow(2, months);
- profit = new int[routes];
- curr_engine = new int[routes];
- for(int i = 0; i < routes; i++)
- {
- curr_engine[i] = engine;
- }
- int max = route(routes, routes/2);
- Runtime runtime = Runtime.getRuntime();
- System.out.println("free memory: " + runtime.freeMemory() / 1024);
- System.out.println(max);
- }
- public static int route(int nCombinations, int nTypeCombinations)
- {
- int newMax = -1000;
- for(int i = 0; i < nCombinations; i++)
- {
- int checkType = Math.floorDiv(i,nTypeCombinations);
- if(checkType%2 == 0)
- {
- profit[i]+=exchange[curr_engine[i] - 1] + maintenance[0];
- curr_engine[i] = 1;
- }
- else
- {
- profit[i]+=maintenance[curr_engine[i]];
- curr_engine[i] += 1;
- }
- if(nTypeCombinations == 1)
- {
- int currentMax = sell[curr_engine[i] - 1] - profit[i];
- if(currentMax > newMax)
- {
- newMax = currentMax;
- }
- }
- }
- if(nTypeCombinations != 1)
- {
- newMax = route(nCombinations, nTypeCombinations/2);
- }
- return newMax;
- }
- }
Add Comment
Please, Sign In to add comment