Pagini recente » Cod sursa (job #2030013) | Cod sursa (job #2578388) | Cod sursa (job #634703) | Cod sursa (job #973638) | Cod sursa (job #3127265)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
const int MAXN = 1e5 + 10;
int cost[MAXN];
int main() {
ifstream inFile("branza.in");
ofstream outFile("branza.out");
int n;
int storageCost, maxWeeks;
inFile >> n >> storageCost >> maxWeeks;
long long totalCost = 0;
for (int i = 1; i <= n; i++) {
int curCost, curDemand;
inFile >> curCost >> curDemand;
cost[i] = curCost;
while (!m.empty() && cost[i] <= cost[m.back()] + storageCost * (i - m.back())) {
m.pop_back();
}
m.push_back(i);
while (!m.empty() && i - m.front() > maxWeeks) {
m.pop_front();
}
totalCost += curDemand * (cost[m.front()] + storageCost * (i - m.front()));
}
outFile << totalCost;
inFile.close();
outFile.close();
return 0;
}