offline algorithm working correctly