#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to calculate total points based on customer data and adTimes
int calculatePoints(vector<int>& adTimes, const vector<pair<int, int>>& ads, const vector<pair<int, int>>& customers) {
int totalPoints = 0;
for (auto customer : customers) {
int arrival = customer.first;
int duration = customer.second;
int maxPoints = 0;
for (int i = 0; i < adTimes.size(); i++) {
int adStart = adTimes[i];
int adDuration = ads[i].first;
int adPoints = ads[i].second;
// Check if the customer can fully watch the ad
if (adStart >= arrival && adStart + adDuration <= arrival + duration) {
maxPoints = max(maxPoints, adPoints);
}
}
totalPoints += maxPoints;
}
return totalPoints;
}
// Backtracking function to assign ads
void assignAds(int adIndex, vector<int>& adTimes, vector<bool>& timeline, int maxTime, const vector<pair<int, int>>& ads, const vector<pair<int, int>>& customers, int& maxPoints) {
if (adIndex == ads.size()) { // All ads assigned
maxPoints = max(maxPoints, calculatePoints(adTimes, ads, customers));
return;
}
int adDuration = ads[adIndex].first;
for (int startTime = 1; startTime <= maxTime; ++startTime) {
bool canPlace = true;
// Check if the ad can be placed without overlapping
for (int t = startTime; t < startTime + adDuration; ++t) {
if (t > maxTime || timeline[t]) {
canPlace = false;
break;
}
}
if (canPlace) {
// Place the ad
adTimes[adIndex] = startTime;
for (int t = startTime; t < startTime + adDuration; ++t) {
timeline[t] = true;
}
// Recurse for the next ad
assignAds(adIndex + 1, adTimes, timeline, maxTime, ads, customers, maxPoints);
// Backtrack
for (int t = startTime; t < startTime + adDuration; ++t) {
timeline[t] = false;
}
adTimes[adIndex] = -1;
}
}
}
int main() {
int T;
cin >> T; // Number of test cases
while (T--) {
int C; // Number of customers
cin >> C;
// Input the ads' points and durations
vector<pair<int, int>> ads(3); // {duration, points}
for (int i = 0; i < 3; ++i) cin >> ads[i].first; // Points
for (int i = 0; i < 3; ++i) cin >> ads[i].second; // Durations
// Input the customers' data
vector<pair<int, int>> customers(C); // {arrival, duration}
for (int i = 0; i < C; ++i) cin >> customers[i].first >> customers[i].second;
// Calculate maximum time (from customers' durations)
int maxTime = 0;
for (auto customer : customers) {
maxTime = max(maxTime, customer.first + customer.second);
}
// Initialize variables
vector<int> adTimes(ads.size(), -1); // Start times for each ad
vector<bool> timeline(maxTime + 1, false); // Track occupied time slots
int maxPoints = 0;
// Start backtracking
assignAds(0, adTimes, timeline, maxTime, ads, customers, maxPoints);
// Output the result for this test case
cout << "Maximum Points: " << maxPoints << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCi8vIEZ1bmN0aW9uIHRvIGNhbGN1bGF0ZSB0b3RhbCBwb2ludHMgYmFzZWQgb24gY3VzdG9tZXIgZGF0YSBhbmQgYWRUaW1lcwppbnQgY2FsY3VsYXRlUG9pbnRzKHZlY3RvcjxpbnQ+JiBhZFRpbWVzLCBjb25zdCB2ZWN0b3I8cGFpcjxpbnQsIGludD4+JiBhZHMsIGNvbnN0IHZlY3RvcjxwYWlyPGludCwgaW50Pj4mIGN1c3RvbWVycykgewogICAgaW50IHRvdGFsUG9pbnRzID0gMDsKIAogICAgZm9yIChhdXRvIGN1c3RvbWVyIDogY3VzdG9tZXJzKSB7CiAgICAgICAgaW50IGFycml2YWwgPSBjdXN0b21lci5maXJzdDsKICAgICAgICBpbnQgZHVyYXRpb24gPSBjdXN0b21lci5zZWNvbmQ7CiAKICAgICAgICBpbnQgbWF4UG9pbnRzID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGFkVGltZXMuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaW50IGFkU3RhcnQgPSBhZFRpbWVzW2ldOwogICAgICAgICAgICBpbnQgYWREdXJhdGlvbiA9IGFkc1tpXS5maXJzdDsKICAgICAgICAgICAgaW50IGFkUG9pbnRzID0gYWRzW2ldLnNlY29uZDsKIAogICAgICAgICAgICAvLyBDaGVjayBpZiB0aGUgY3VzdG9tZXI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cKICAgICAgICBmb3IgKGludCB0ID0gc3RhcnRUaW1lOyB0IDwgc3RhcnRUaW1lICsgYWREdXJhdGlvbjsgKyt0KSB7CiAgICAgICAgICAgIGlmICh0ID4gbWF4VGltZSB8fCB0aW1lbGluZVt0XSkgewogICAgICAgICAgICAgICAgY2FuUGxhY2UgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQogCiAgICAgICAgaWYgKGNhblBsYWNlKSB7CiAgICAgICAgICAgIC8vIFBsYWNlIHRoZSBhZAogICAgICAgICAgICBhZFRpbWVzW2FkSW5kZXhdID0gc3RhcnRUaW1lOwogICAgICAgICAgICBmb3IgKGludCB0ID0gc3RhcnRUaW1lOyB0IDwgc3RhcnRUaW1lICsgYWREdXJhdGlvbjsgKyt0KSB7CiAgICAgICAgICAgICAgICB0aW1lbGluZVt0XSA9IHRydWU7CiAgICAgICAgICAgIH0KIAogICAgICAgICAgICAvLyBSZWN1cnNlIGZvciB0aGUgbmV4dCBhZAogICAgICAgICAgICBhc3NpZ25BZHMoYWRJbmRleCArIDEsIGFkVGltZXMsIHRpbWVsaW5lLCBtYXhUaW1lLCBhZHMsIGN1c3RvbWVycywgbWF4UG9pbnRzKTsKIAogICAgICAgICAgICAvLyBCYWNrdHJhY2sKICAgICAgICAgICAgZm9yIChpbnQgdCA9IHN0YXJ0VGltZTsgdCA8IHN0YXJ0VGltZSArIGFkRHVyYXRpb247ICsrdCkgewogICAgICAgICAgICAgICAgdGltZWxpbmVbdF0gPSBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhZFRpbWVzW2FkSW5kZXhdID0gLTE7CiAgICAgICAgfQogICAgfQp9CiAKaW50IG1haW4oKSB7CiAgICBpbnQgVDsKICAgIGNpbiA+PiBUOyAvLyBOdW1iZXIgb2YgdGVzdCBjYXNlcwogCiAgICB3aGlsZSAoVC0tKSB7CiAgICAgICAgaW50IEM7IC8vIE51bWJlciBvZiBjdXN0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