From 83db78e6301b14c87d55869c56d1d49b11d71c5d Mon Sep 17 00:00:00 2001 From: Ankur-Dwivedi22 Date: Sat, 6 Apr 2024 13:17:23 +0530 Subject: [PATCH] chore: completed day 4 hard task author: Ankur-Dwivedi22 --- Hard/Day4/Solution.cpp | 66 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/Hard/Day4/Solution.cpp b/Hard/Day4/Solution.cpp index 5223d7e..031864d 100644 --- a/Hard/Day4/Solution.cpp +++ b/Hard/Day4/Solution.cpp @@ -1 +1,65 @@ -// Write Your Code Here +#include +#include + +using namespace std; +using ll = long long; + +// Function to calculate the number of pairs +ll calculatePairs(ll n, ll k) { + if (n == 0 || k == 0) { + return 0; + } + ll x = n / k; + ll l = n % k; + ll r = k - l; + ll L = (x + 1) * l, R = x * r; + return R * L + (R - x) * R / 2 + L * (L - x - 1) / 2; +} + +void solve() { + // Input the number of machines, cost per brick, and additional cost + int num_machines; + long long brick_cost, additional_cost; + cin >> num_machines >> brick_cost >> additional_cost; + + int n = 0; + vector machine_count(num_machines); + // Input machine counts and find the maximum count + for (int i = 0; i < num_machines; ++i) { + cin >> machine_count[i]; + n = max(n, machine_count[i]); + } + + // Initialize vectors to store pairs and additional costs + vector pair(n + 1); + vector add(n + 1); + + // Calculate pairs and additional costs for each machine + for (int i = 0; i < num_machines; ++i) { + for (int j = 1; j <= machine_count[i]; ++j) { + pair[j] += calculatePairs(machine_count[i], j); + } + add[machine_count[i]] += calculatePairs(machine_count[i], machine_count[i]); + } + + // Calculate the maximum profit + long long max_profit = 0; + long long other = 0; + for (int i = 1; i <= n; ++i) { + max_profit = max(max_profit, brick_cost * (pair[i] + other) - additional_cost * (i - 1)); + other += add[i]; + } + // Output the maximum profit + cout << max_profit << endl; +} + +int main() { + // Input the number of test cases + int num_test_cases; + cin >> num_test_cases; + // Solve each test case + while (num_test_cases--) { + solve(); + } + return 0; +}