Home BOJ. Word Math (1339)
Post
Cancel

BOJ. Word Math (1339)

[Link] https://www.acmicpc.net/problem/1339


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
typedef long long ll;
const int INF = 987654321;

int n;
vector<string> str_vec;

bool cmp(pair<char, int> l, pair<char, int> r) {
  return l.second > r.second;
}

void solve() {
  map<char, int> val;
  for(string str: str_vec) {
    int ten = 1;
    for(int i = str.size() - 1; i >= 0; i--) {
      val[str[i]] += ten;
      ten *= 10;
    }
  }

  vector<pair<char, int>> vec(val.begin(), val.end());
  sort(vec.begin(), vec.end(), cmp);
  int sum = 0;
  int num = 9;
  for(pair<char, int> ploop: vec) sum += ploop.second * num--;
  cout << sum;
}

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
  cin >> n;
  for0(i, n) { string s; cin >> s; str_vec.push_back(s); }
  solve();
  return 0;
}
This post is licensed under CC BY 4.0 by the author.