using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; class contestant { static int n; static int[] lev; static int[] counts; public string name; public int sum = 0; public long uniscore = 0; public long preuni = 0; public static void init(int _n, int[] _lev) { n = _n; lev = _lev; counts = new int[n]; } public contestant(string name) { this.name = name; } public long submit(char c, int t) { int p = c - 'A'; sum += lev[p] * 50 + (int)(lev[p] * 50 / (1 + 0.2 * counts[p]) + 1e-9); ++counts[p]; preuni = uniscore; return uniscore = (long)sum * 100000 - t; } } class Program { static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; static void Main() { var sc = new Scan(); int n = sc.Int; var lev = sc.IntArr; contestant.init(n, lev); var id = new SortedDictionary(); var lis = new List(); var uniscore = new List(); var zip = new SortedDictionary(); int t = sc.Int; var np = new string[t][]; var unis = new long[t]; var preuni = new long[t]; uniscore.Add(0); for (int i = 0; i < t; i++) { np[i] = sc.StrArr; if (np[i][1] == "?") { unis[i] = lis[id[np[i][0]]].uniscore; continue; } if (!id.ContainsKey(np[i][0])) { id.Add(np[i][0], lis.Count); lis.Add(new contestant(np[i][0])); } else { preuni[i] = lis[id[np[i][0]]].uniscore; } unis[i] = lis[id[np[i][0]]].submit(np[i][1][0], i); uniscore.Add(unis[i]); } uniscore.Sort(); int lim = uniscore.Count; for (int i = 0; i < lim; i++) { zip.Add(uniscore[i], i + 1); } var sg = new Segtree(lim + 1, (i, j) => i + j, 0); for (int i = 0; i < t; ++i) { int zuni = zip[unis[i]]; if (np[i][1] == "?") { sw.WriteLine(sg.run(zuni, lim + 1)); continue; } sg.update(zip[preuni[i]], 0); sg.update(zuni, 1); } sw.Flush(); } static void Prt(IEnumerable a) => sw.WriteLine(string.Join(" ", a)); } class Scan { public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public string Str => Console.ReadLine().Trim(); public int[] IntArr => StrArr.Select(int.Parse).ToArray(); public long[] LongArr => StrArr.Select(long.Parse).ToArray(); public double[] DoubleArr => StrArr.Select(double.Parse).ToArray(); public string[] StrArr => Str.Split(); } class Segtree { int n, s, t; T[] tr; Func f; T exnum; public Segtree(int m, Func f, T ex) { this.f = f; this.exnum = ex; n = 1; while (n < m) n <<= 1; tr = new T[(n << 1) - 1]; for (int i = 0; i < tr.Length; i++) tr[i] = ex; } public Segtree(int m, T ini, Func f, T ex) : this(m, f, ex) { for (int i = 0; i < m; ++i) tr[i + n - 1] = ini; all_update(); } public void assign(int j, T x) => tr[j + n - 1] = x; public void update(int j, T x) { int i = j + n - 1; tr[i] = x; while (i > 0) { i = i - 1 >> 1; tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); } } public void all_update() { for (int i = n - 2; i >= 0; i--) tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); } public T look(int i) => tr[i + n - 1]; // [s, t) public T run(int s, int t) { this.s = s; this.t = t; return q(0, 0, n); } T q(int k, int l, int r) => r <= s || t <= l ? exnum : s <= l && r <= t ? tr[k] : f(q(k << 1 | 1, l, l + r >> 1), q(k + 1 << 1, l + r >> 1, r)); }