Bestcoder 1st Anniversary ($) 1001 Souvenir

Source: Internet
Author: User

Souveniraccepts:901submissions:2743Time limit:2000/1000 MS (java/others)Memory limit:262144/262144 K (java/others)Problem Description

Today is the 1st anniversary of Bestcoder. Soda, the contest manager, wants to buy a souvenir for each contestant. You can buy the souvenir one by one or set by set in the shop. The price of a souvenir isp p yuan and the price for a set of souvenirs if& nbsp q q yuan. There ' S m< Span class= "Strut bottom" >m souvenirs in one Set.

There ' s nN contestants in the contest today. Soda wants to know the minimum cost needed to buy a souvenir for each contestant.

Input

There is multiple test cases. The first line of input contains an integerTT   (1 \le T \le 10^5) (1≤t≤1 0 ? 5 ??

There ' s a line containing 4 integersN, M, p, qN,M,P,Q(1 \le N, M, p, Q \le 10^4)(1≤n,m,p,q≤10? ) 4?? ).

Output

For each test case, the output of the minimum cost needed.

Sample Input
21 2 2 11 2 3 4
Sample Output
13
Hintfor The first case, Soda can use 1 yuan to buy a set of 2 souvenirs. For the second case, Soda can use 3 yuan to buy a souvenir.
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 intMain ()6 {7     intt,n,m,p,q,i,j,k;8scanf"%d",&T);9      while(t--)Ten     { Onescanf"%d %d%d%d",&n,&m,&p,&q); A         if((Double) p<= (Double) q/m) -         { -printf"%d\n"Np); the         } -         Else -         { -             intx=0; +              for(i=1;; i++) -             { +x=x+m; A                 if(x>=N) at                      Break; -             } -printf"%d\n", Min (I*q, (i-1) *q+ (n (x-m)) *p)); -         } -     } -     return 0; in}
View Code

Bestcoder 1st Anniversary ($) 1001 Souvenir

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.