1236A. Sweet Problem

一、Problem

You have three piles of candies: red, green and blue candies:

  • the first pile contains only red candies and there are r candies in it,
  • the second pile contains only green candies and there are g candies in it,
  • the third pile contains only blue candies and there are b candies in it.

Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.

Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.

Input
The first line contains integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.

Each test case is given as a separate line of the input. It contains three integers r, g and b (1≤r,g,b≤108) — the number of red, green and blue candies, respectively.

Output
Print t integers: the i-th printed integer is the answer on the i-th test case in the input.

二、Example

input

6
1 1 1
1 2 1
4 1 1
7 4 10
8 1 4
8 2 8

output

1
2
2
10
5
9

三、Code

package com.codeforces.contests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class SweetProblem {
    static BufferedReader br;
    static StringTokenizer st = new StringTokenizer("");

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int wheel = nextInt();
        for (int i = 0; i < wheel; i++) {
            int r = nextInt();
            int g = nextInt();
            int b = nextInt();
            int min = min(r, min(g, b));
            int max = max(r, max(g, b));
            int sum = r + g + b;
            int mid = sum - max - min;
            int diff = max - mid;
            if (diff > min) {
                pw.println(min(max, sum - max));
            } else {
                min -= diff;
                mid += diff;
                int plus = min / 2;
                pw.println(mid + plus);
            }
        }
        pw.close();
    }

    private static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    private static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103331041