Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
0
Indexable
Never
import java.util.Scanner;

public class Solution {
	
	static int n, ans, minT, maxT;
	static int[] len, point, arrive, leave;
	static int[] startAds, endAds;
	
	private static boolean check(int k, int i){
		int start = i, end = i + len[k];
		for(int j = 0; j < 3; j++){
			if(j != k){
				if(start >= startAds[j] && start < endAds[j]) return false;
				if(end > startAds[j] && end <= endAds[j]) return false;
				if(start <= startAds[j] && end>= endAds[j]) return false;
				
			}
		}
		
		return true;
	}
	
	private static int findPoint(){
		int sum = 0, max; boolean flag;
		for(int i = 0; i < n; i++){
			max = 0; flag = false;
			for(int j = 0; j < 3; j++){
				if(arrive[i] <= startAds[j] && leave[i] >= endAds[j]){
					if(!flag){
						flag = true;
						max = j;
					}
					else max = point[max] < point[j] ? j : max;
				}
			}
			if(flag) sum += point[max];
		}
		
		return sum;
		
	}
	
	private static void solve(int k){
		if(k == 3){
			int tmp = findPoint();
			ans = tmp > ans ? tmp : ans;
			return;
		}
		
		for(int i = minT; i <= maxT; i++){
			if(check(k, i)){
				startAds[k] = i;
				endAds[k] = i + len[k];
				
				solve(k + 1);
				
				startAds[k] = 0;
				endAds[k] = 0;
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		int testcase = sc.nextInt();
		for(int tc = 1; tc <= testcase; tc++){
			n = sc.nextInt();
			len = new int[3]; point = new int[3];
			len[0] = sc.nextInt(); len[1] = sc.nextInt(); len[2] = sc.nextInt();
			point[0] = sc.nextInt(); point[1] = sc.nextInt(); point[2] = sc.nextInt();
			
			arrive = new int[n]; leave = new int[n];
			int tmp; minT = 100; maxT = 0;
			for(int i = 0; i < n; i++){
				arrive[i] = sc.nextInt();
				minT = arrive[i] < minT ? arrive[i] : minT;
				tmp = sc.nextInt();
				leave[i] = arrive[i] + tmp;
				maxT = leave[i]> maxT ? leave[i] : maxT;
			}
			
			startAds = new int[3]; endAds = new int[3];
			ans = 0;
			solve(0);
			
			System.out.println("Case #" + tc);
			System.out.println(ans);
			
		}
		
	}

}