2844:http://acm.tju.edu.cn/toj/showp2844.html
int n, m, k, ans; vector<pii> v; void solve() { v.clear(); ans = 0; inc(i, 0, n+m) { int a, b, c; cii(a, b); cit(c); ans += (c+k-1)/k*b; if(i >= n) c = -c; v.puck(pii(a, c)); } sort(v.begin(), v.end()); int t, t1, t2; t1 = t2 = 0; inc(i, 0, v.size()) { t = (abs(t2)+k-1)/k; if(t == 0) t++; ans += t*(v[i].first-t1); t2 += v[i].second; t1 = v[i].first; } pit(ans<<1); } int main() { while(~scanf("%d%d%d", &n, &m, &k), n+m+k) { solve(); } }