#include #include #include using namespace std; typedef long long ll; template inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } typedef pair pll; vector v[2]; #define MP make_pair #define PB push_back int main() { ll n,k; scanf("%lld%lld",&n,&k); for (ll i=0;i=0;i--) { ll x=v[z][i].first,t=v[z][i].second; ll from_overflow=min(ovf,t); t-=from_overflow; ovf-=from_overflow; ll trips=t/k; ll left=t%k; if(left) { trips++; ovf+=(k-left); } if (trips) chkmax(last,x); final+=trips*2*x; } } //final-=last; printf("%lld\n",final); }