#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].second; // Points
for (int i = 0; i < 3; ++i) cin >> ads[i].first; // 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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIHRvdGFsIHBvaW50cyBiYXNlZCBvbiBjdXN0b21lciBkYXRhIGFuZCBhZFRpbWVzCmludCBjYWxjdWxhdGVQb2ludHModmVjdG9yPGludD4mIGFkVGltZXMsIGNvbnN0IHZlY3RvcjxwYWlyPGludCwgaW50Pj4mIGFkcywgY29uc3QgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiYgY3VzdG9tZXJzKSB7CiAgICBpbnQgdG90YWxQb2ludHMgPSAwOwoKICAgIGZvciAoYXV0byBjdXN0b21lciA6IGN1c3RvbWVycykgewogICAgICAgIGludCBhcnJpdmFsID0gY3VzdG9tZXIuZmlyc3Q7CiAgICAgICAgaW50IGR1cmF0aW9uID0gY3VzdG9tZXIuc2Vjb25kOwoKICAgICAgICBpbnQgbWF4UG9pbnRzID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGFkVGltZXMuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaW50IGFkU3RhcnQgPSBhZFRpbWVzW2ldOwogICAgICAgICAgICBpbnQgYWREdXJhdGlvbiA9IGFkc1tpXS5maXJzdDsKICAgICAgICAgICAgaW50IGFkUG9pbnRzID0gYWRzW2ldLnNlY29uZDsKCiAgICAgICAgICAgIC8vIENoZWNrIGlmIHRoZSBjdXN0b21lciBjYW4gZnVsb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AgICBmb3IgKGludCB0ID0gc3RhcnRUaW1lOyB0IDwgc3RhcnRUaW1lICsgYWREdXJhdGlvbjsgKyt0KSB7CiAgICAgICAgICAgIGlmICh0ID4gbWF4VGltZSB8fCB0aW1lbGluZVt0XSkgewogICAgICAgICAgICAgICAgY2FuUGxhY2UgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBpZiAoY2FuUGxhY2UpIHsKICAgICAgICAgICAgLy8gUGxhY2UgdGhlIGFkCiAgICAgICAgICAgIGFkVGltZXNbYWRJbmRleF0gPSBzdGFydFRpbWU7CiAgICAgICAgICAgIGZvciAoaW50IHQgPSBzdGFydFRpbWU7IHQgPCBzdGFydFRpbWUgKyBhZER1cmF0aW9uOyArK3QpIHsKICAgICAgICAgICAgICAgIHRpbWVsaW5lW3RdID0gdHJ1ZTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gUmVjdXJzZSBmb3IgdGhlIG5leHQgYWQKICAgICAgICAgICAgYXNzaWduQWRzKGFkSW5kZXggKyAxLCBhZFRpbWVzLCB0aW1lbGluZSwgbWF4VGltZSwgYWRzLCBjdXN0b21lcnMsIG1heFBvaW50cyk7CgogICAgICAgICAgICAvLyBCYWNrdHJhY2sKICAgICAgICAgICAgZm9yIChpbnQgdCA9IHN0YXJ0VGltZTsgdCA8IHN0YXJ0VGltZSArIGFkRHVyYXRpb247ICsrdCkgewogICAgICAgICAgICAgICAgdGltZWxpbmVbdF0gPSBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhZFRpbWVzW2FkSW5kZXhdID0gLTE7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBUOwogICAgY2luID4+IFQ7IC8vIE51bWJlciBvZiB0ZXN0IGNhc2VzCgogICAgd2hpbGUgKFQtLSkgewogICAgICAgIGludCBDOyAvLyBOdW1iZXIgb2YgY3VzdG9tZXJzCiAgICAgICAg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