Learning from Successful and Failed Demonstrations via Optimization