Problem details

https://codeforces.com/contest/1955/problem/A

Problem - A - Codeforces

codeforces.com

 

Ideas

  1. If 2a > b, then buy as much as you can at the b price.
    If 2a < b, then buy as much as you can at the a price.
  2. If n is odd, then you should buy at least one at the a price.

 

Answer code (Java)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int t, n, a, b, ans;
		
		t = sc.nextInt();
		for (int i = 0; i < t; i++) {
			ans = 0;
			n = sc.nextInt();
			a = sc.nextInt();
			b = sc.nextInt();
			
			// Idea2 : If n is odd, then you should buy at least one at the a price.
			if (n % 2 == 1) {
				ans += a;
				n--;
			}
			
			// Idea1 : If 2a > b, then buy as much as you can at the b price.
			//         If 2a < b, then buy as much as you can at the a price.
			if (2 * a > b) {
				ans += n / 2 * b;
			} else {
				ans += n * a;
			}
			System.out.println(ans);
		}
	}
}

 

+ Recent posts