Skip to content

Commit

Permalink
Merge pull request #46 from Ankur-Dwivedi22/day_4hard
Browse files Browse the repository at this point in the history
chore: completed day 4 hard task author: Ankur-Dwivedi22
  • Loading branch information
opabhijeet authored Apr 7, 2024
2 parents adefdd1 + 83db78e commit 6064247
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion Hard/Day4/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
// Write Your Code Here
#include <iostream>
#include <vector>

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<int> 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<long long> pair(n + 1);
vector<long long> 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;
}

0 comments on commit 6064247

Please sign in to comment.