Real-time Bidding for Time Constrained Impression Contracts in First and Second Price Auctions - Theory and Algorithms