fn main() { let mut nk = String::new(); std::io::stdin().read_line(&mut nk).ok(); let nk: Vec = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nk[0]; let k = nk[1]; let mut patterns = (0..k).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec<&str> = temp.trim().split_whitespace().collect(); (temp[0].parse::().unwrap(), temp[1].chars().nth(0).unwrap()) }) .collect::>(); patterns.sort_by_key(|&(a, _)| a); let mut result = vec![]; let mut cnt = vec![0usize; 3]; let rgb = "RGB".chars().collect::>(); for i in 0..k { let (a, c) = patterns[i]; let cval = if c == 'R' { 0 } else if c == 'G' { 1 } else { 2 }; let mut addval = vec![0usize; 3]; let mut diff = a - cnt.iter().sum::(); let maxval = *cnt.iter().max().unwrap(); let minval = *cnt.iter().min().unwrap(); if minval < maxval { if cnt[cval] == minval { diff -= 1; addval[cval] += 1; } for i in 0..3 { if cnt[i] + addval[i] == minval && diff > 0 { addval[i] += 1; diff -= 1; } } } addval[0] += diff/3; addval[1] += diff/3; addval[2] += diff/3; diff %= 3; if diff == 2 { addval[cval] += 1; addval[(cval + 1) % 3] += 1; } else if diff == 1 { addval[cval] += 1; } for i in 0..3 { result.push(rgb[i].to_string().repeat(addval[i])); cnt[i] += addval[i]; } let maxval = *cnt.iter().max().unwrap(); if maxval != cnt[cval] { println!("-1"); return; } } if cnt.iter().sum::() < n { result.push("R".repeat(n - cnt.iter().sum::())); } println!("{}", result.join("")); }