import java.util.*;

public class Main {
    static int T;
    static int N;
    static int maxPoint;

    static int[] L = new int[4];
    static int[] P = new int[4];
    static int[] A = new int[105];
    static int[] D = new int[105];

    static void backtrack(int adIdx, int t1, int t2, int t3) {

        if (adIdx > 3) {
            int end1 = t1 + L[1];
            int end2 = t2 + L[2];
            int end3 = t3 + L[3];

            int total = 0;

            for (int i = 1; i <= N; i++) {
                int start = A[i];
                int end = A[i] + D[i];

                int point = 0;

                if (start <= t1 && end1 <= end) {
                    point = Math.max(point, P[1]);
                }

                if (start <= t2 && end2 <= end) {
                    point = Math.max(point, P[2]);
                }

                if (start <= t3 && end3 <= end) {
                    point = Math.max(point, P[3]);
                }

                total += point;
            }

            maxPoint = Math.max(maxPoint, total);
            return;
        }

        for (int t = 0; t <= 50; t++) {
            int e = t + L[adIdx];

            if (adIdx == 2 && !(e <= t1 || t >= t1 + L[1]))
                continue;

            if (adIdx == 3 &&
                    (!(e <= t1 || t >= t1 + L[1])
                    || !(e <= t2 || t >= t2 + L[2])))
                continue;

            if (adIdx == 1)
                backtrack(2, t, 0, 0);
            else if (adIdx == 2)
                backtrack(3, t1, t, 0);
            else
                backtrack(4, t1, t2, t);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        T = sc.nextInt();

        for (int tc = 1; tc <= T; tc++) {

            N = sc.nextInt();

            for (int i = 1; i <= 3; i++)
                L[i] = sc.nextInt();

            for (int i = 1; i <= 3; i++)
                P[i] = sc.nextInt();

            for (int i = 1; i <= N; i++) {
                A[i] = sc.nextInt();
                D[i] = sc.nextInt();
            }

            int[][] K = {
                    {1, 2, 3},
                    {1, 3, 2},
                    {2, 1, 3},
                    {2, 3, 1},
                    {3, 1, 2},
                    {3, 2, 1}
            };

            maxPoint = 0;

            backtrack(1, 0, 0, 0);

            System.out.println("Case #" + tc);
            System.out.println(maxPoint);
        }

        sc.close();
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: