Give Me a Lot of Triangles
Source: aizu

Give Me a Lot of Triangles
Problem Statement

You have $A_1$ sticks of length $1$, $A_2$ sticks of length $2$, and $A_3$ sticks of length $3$. You can perform the following operation any number of times:

Choose 3 sticks such that they can form a triangle. Use these 3 sticks to make a triangle. Once used, these sticks cannot be used to form other triangles.

To "form a triangle", the lengths of the chosen sticks $a$, $b$, and $c$ must satisfy the triangle inequality: $a + b > c, b + c > a,$ and $c + a > b$.

Determine the maximum number of triangles that can be made.

Given $T$ test cases, compute the answer for each.

Input

The input is given in the following format:

$T$
$case_1$
$case_2$
$\vdots$
$case_T$


Here, $case_i$ denotes the $i$-th test case.

Each test case is given in the following format:

$A_1$ $A_2$ $A_3$

$1 \leq T \leq 10,000$
$0 \leq A_i \leq 10^8$
All input values are integers.
Output

Output $T$ lines. On the $i$-th line, output the answer for the $i$-th test case.

Sample Input
4
3 1 2
4 1 1
0 0 0
31415926 535897 93238462

Sample Output
2
1
0
41730095


C++ solution:
// AOJ #3629 Give Me a Lot of Triangles
// 2025.3.10

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
    int n = 0, c = gc();
    do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
    return n;
}

void Cout(ll n) { // 整数の表示（出力）
    char b[30];
    if (!n) pc('0');
    else {
        if (n < 0) pc('-'), n = -n;
        int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
        while (i--) pc(b[i]);
    }
    pc('\n');
}

int main(){
    int T = Cin();
    while(T--){
        int a = Cin(), b = Cin(), c = Cin();
        ll ans = 0;
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                for (int k = 0; k < 3; k++){
                    for (int l = 0; l < 3; l++){
                        ll ra = a - (i + j);
                        ll rb = b - (2LL*i + 2LL*k + l);
                        ll rc = c - (2LL*j + k + 2LL*l);
                        if(ra < 0 || rb < 0 || rc < 0) continue;
                        ll eq = ra/3 + rb/3 + rc/3;
                        ans = max(ans, (ll)(i+j+k+l) + eq);
                    }
                }
            }
        }
        Cout(ans);
    }
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	// Read the number of test cases
	scanner.Scan()
	T, _ := strconv.Atoi(scanner.Text())

	for t := 0; t < T; t++ {
		// Read A1, A2, A3
		scanner.Scan()
		a1, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		a2, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		a3, _ := strconv.Atoi(scanner.Text())

		maxTriangles := 0

		// Iterate all possible i, j, k, l (each from 0 to 2)
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					for l := 0; l < 3; l++ {
						ra := a1 - (i + j)
						rb := a2 - (2*i + 2*k + l)
						rc := a3 - (2*j + k + 2*l)

						if ra >= 0 && rb >= 0 && rc >= 0 {
							triangles := i + j + k + l
							triangles += ra / 3
							triangles += rb / 3
							triangles += rc / 3

							if triangles > maxTriangles {
								maxTriangles = triangles
							}
						}
					}
				}
			}
		}

		fmt.Println(maxTriangles)
	}
}

Java solution:
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int T = Integer.parseInt(br.readLine());
        while (T-- > 0) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            long a = Long.parseLong(st.nextToken());
            long b = Long.parseLong(st.nextToken());
            long c = Long.parseLong(st.nextToken());
            
            long maxTriangles = 0;
            
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    for (int k = 0; k < 3; k++) {
                        for (int l = 0; l < 3; l++) {
                            long ra = a - (i + j);
                            long rb = b - (2L * i + 2L * k + l);
                            long rc = c - (2L * j + k + 2L * l);
                            
                            if (ra >= 0 && rb >= 0 && rc >= 0) {
                                long triangles = i + j + k + l + (ra / 3) + (rb / 3) + (rc / 3);
                                if (triangles > maxTriangles) {
                                    maxTriangles = triangles;
                                }
                            }
                        }
                    }
                }
            }
            
            bw.write(maxTriangles + "\n");
        }
        bw.flush();
    }
}

JavaScript solution:
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];
let currentLine = 0;

rl.on('line', (line) => {
    input.push(line);
});

rl.on('close', () => {
    solve();
});

function solve() {
    const lines = input;
    const T = parseInt(lines[0]);
    for (let t = 1; t <= T; t++) {
        const [a, b, c] = lines[t].split(' ').map(Number);
        let maxTriangles = 0;
        
        // Iterate all possible combinations of i, j, k, l (each up to 2)
        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 3; j++) {
                for (let k = 0; k < 3; k++) {
                    for (let l = 0; l < 3; l++) {
                        const ra = a - (i + j);
                        const rb = b - (2 * i + 2 * k + l);
                        const rc = c - (2 * j + k + 2 * l);
                        
                        if (ra >= 0 && rb >= 0 && rc >= 0) {
                            const eq = Math.floor(ra / 3) + Math.floor(rb / 3) + Math.floor(rc / 3);
                            const total = i + j + k + l + eq;
                            if (total > maxTriangles) {
                                maxTriangles = total;
                            }
                        }
                    }
                }
            }
        }
        console.log(maxTriangles);
    }
}

Python solution:
import sys

def solve():
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    results = []
    for _ in range(T):
        a = int(input[ptr])
        b = int(input[ptr + 1])
        c = int(input[ptr + 2])
        ptr += 3
        max_triangles = 0
        # Iterate all possible i, j, k, l (each up to 2)
        for i in range(0, 3):
            for j in range(0, 3):
                for k in range(0, 3):
                    for l in range(0, 3):
                        ra = a - (i + j)
                        rb = b - (2 * i + 2 * k + l)
                        rc = c - (2 * j + k + 2 * l)
                        if ra >= 0 and rb >= 0 and rc >= 0:
                            eq = ra // 3 + rb // 3 + rc // 3
                            total = i + j + k + l + eq
                            if total > max_triangles:
                                max_triangles = total
        results.append(max_triangles)
    print('\n'.join(map(str, results)))

solve()

Ruby solution:
def solve
  input = $stdin.read.split.map(&:to_i)
  idx = 0
  t = input[idx]
  idx += 1
  results = []
  t.times do
    a = input[idx]
    b = input[idx + 1]
    c = input[idx + 2]
    idx += 3
    max_triangles = 0

    # Iterate all possible configurations (i, j, k, l)
    (0..2).each do |i|
      (0..2).each do |j|
        (0..2).each do |k|
          (0..2).each do |l|
            ra = a - (i + j)
            rb = b - (2 * i + 2 * k + l)
            rc = c - (2 * j + k + 2 * l)
            next if ra < 0 || rb < 0 || rc < 0

            # Additional triangles from remaining sticks (1,1,1), (2,2,2), (3,3,3)
            eq = ra / 3 + rb / 3 + rc / 3
            total = i + j + k + l + eq
            max_triangles = [max_triangles, total].max
          end
        end
      end
    end

    results << max_triangles
  end

  puts results.join("\n")
end

solve