using System.Collections.Generic;
using static System.Console;
public static void Main()
.Select(line => line.Split(" "))
.OrderBy(line => GetType(line[0]))
.ThenBy(line => line[0].Aggregate(0, (acc, c) => acc * 15 + GetStrength(c)))
.Select((line, i) => long.Parse(line[1]) * (i + 1))
private static int GetStrength(char label) => label switch
>= '2' and <= '9' => label - '0',
_ => throw new Exception($"Strength is not defined for {label}")
private static int GetType(string hand)
.ToDictionary(_ => _.Key, _ => _.Count());
if (freqs.TryGetValue('J', out var jokersCount))
.OrderByDescending(_ => _.Value)
freqs[bestLabel] += jokersCount;
if (freqs.Values.Max() == 4)
if (freqs.Values.Max() == 3)
private const string Input2 = """
private const string Input = """