现在的位置: 首页 > 综合 > 正文

Codeforces 466 A. Cheap Travel

2017年11月23日 ⁄ 综合 ⁄ 共 1186字 ⁄ 字号 评论关闭

A. Cheap Travel
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket
for m rides (she can buy it several times). It costs b rubles.
Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?

Input

The single line contains four space-separated integers nmab (1 ≤ n, m, a, b ≤ 1000)
— the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride
ticket.

Output

Print a single integer — the minimum sum in rubles that Ann will need to spend.

Sample test(s)
input
6 2 1 2
output
6
input
5 2 2 3
output
8
Note

In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy threem ride tickets.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    long long int n,m,a,b;
    cin>>n>>m>>a>>b;
    long long int k=n/m;
    long long int ans=n*a;
    for(int i=0;i*m<2*n;i++)
    {
        int r=n-m*i;
        if(r<0) r=0;
        ans=min(ans,r*a+i*b);
    }
    if(n%m==0)
        ans=min(ans,k*b);
    else
        ans=min(ans,(k+1)*b);
    cout<<ans<<endl;
    return 0;
}

抱歉!评论已关闭.