A simple greedy template title
http://poj.org/problem?id=2393
The daily prices to the lowest on the list
The core idea: the lowest price on day i = min (i-1 th lowest price day + s, the original price of the i-th day)
With ideas, code, casual play
#include <iostream> #include <algorithm> using namespace std; #define maxn 10005 typedef long long ll; ll c[maxn], y[maxn]; int main() { ll cost = 0; ll n, s; cin >> n >> s; for (ll i = 0; i < n; ++i) cin >> c[i] >> y[i]; for (ll i = 1; i < n;++i) c[i] = min(c[i], c[i - 1] + s); for (ll i = 0; i < n; ++i) cost += c[i] * y[i]; cout << cost << endl; return 0; }