#include using namespace std; using ll = long long; #define TYPE_OF( VAR ) remove_const::type >::type #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define CIN( LL , A ) LL A; cin >> A #define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) #define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define QUIT return 0 #define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT int main() { UNTIE; constexpr const ll bound = 10000000; CIN_ASSERT( T , - bound , bound ); assert( T != 0 ); CIN_ASSERT( X , 1 , bound ); CIN_ASSERT( A , X , bound ); CIN_ASSERT( Y , 1 , bound ); CIN_ASSERT( B , 1 , bound ); ll y_lb = ( T >= 0 ? 0 : ( - T + B - 1 ) / B ); ll t_min = y_lb * Y + ( T + y_lb * B ); ll y_ub = t_min / Y; ll t; FOREQ( y , y_lb , y_ub ){ t = T + y * B; t = y * Y + ( t / A ) * X + ( t % A ); if( t_min > t ){ t_min = t; } } RETURN( t_min ); }