Problem:
G: AOR-String
問題
$N$ 個の文字列 $S_i$ が与えられる.
$S_i$ を任意の順に繋げて得られる文字列に含まれる "AOR" の数の最大値を求めよ.
制約
$1 \leq N \leq 10^5$
$1 \leq |S_i| \leq 20$
$S_i$ は大文字アルファベットのみからなる
入力形式
入力は以下の形式で与えられる.
$N$
$S_1$
…
$S_N$
出力
$S_i$ を任意の順に繋げて得られる文字列に含まれる "AOR" の数の最大値を出力せよ. また, 末尾に改行も出力せよ.
サンプル
サンプル入力 1
2
AORA
OR
サンプル出力 1
2
サンプル入力 2
5
AB
CA
ORA
XX
AOR
サンプル出力 2
2

Submitted solutions:

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 200100
#define MOD 1000000007  //998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
	Setup_io() {
		ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
		cout << fixed << setprecision(15);
	}
} setup_io;



int main() {
	int t;
	int n, m;
	int ans, s;
	char c[25];
	int a[2];//S,SA
	int b[2];//AD,D
	int d[2];
	int cnt;
	int e;
	int x;
	bool v;
	bool vv;
	bool vvv;
	t = 1;
	rep(tt, t) {
		rep(i, 2)a[i] = 0;
		rep(i, 2)b[i] = 0;
		rep(i, 2)d[i] = 0;
		cnt = 0;
		e = 0;
		ans = 0;
		s = 0;
		v = false;
		vv = false;
		vvv = false;
		cin >> n;
		rep(i, n) {
			rep(j, 25)c[j] = 0;
			cin >> c;
			m = strlen(c);
			rep(i, m){
			    if(c[i] == 'A') c[i] = 'S';
			    else if(c[i] == 'O') c[i] = 'A';
			    else if(c[i] == 'R') c[i] = 'D';
			    else c[i] = 'Z';
			}
			if (m == 1) {
				if (c[0] == 'S')a[0]++;
				if (c[0] == 'A')cnt++;
				if (c[0] == 'D')b[1]++;
				if ((c[0] == 'D') || (c[0] == 'S'))v = true;
			}
			else {
				if (c[m - 1] == 'S')a[0]++;
				if ((c[m - 2] == 'S') && (c[m - 1] == 'A'))a[1]++;
				if ((c[0] == 'A') && (c[1] == 'D'))b[0]++;
				if (c[0] == 'D')b[1]++;
				if ((c[m - 1] == 'S') && (c[0] == 'A') && (c[1] == 'D'))d[0]++;
				if ((c[m - 2] == 'S') && (c[m - 1] == 'A') && (c[0] == 'D'))d[1]++;
				if ((c[0] == 'D') && (!((c[m - 1] == 'S') || ((c[m - 2] == 'S') && (c[m - 1] == 'A')))))v = true;
				if (((c[0] == 'A') && (c[1] == 'D')) && (!((c[m - 1] == 'S') || ((c[m - 2] == 'S') && (c[m - 1] == 'A')))))v = true;
				if ((c[m - 1] == 'S') && (!((c[0] == 'D') || ((c[0] == 'A') && (c[1] == 'D')))))v = true;
				if (((c[m - 2] == 'S') && (c[m - 1] == 'A')) && (!((c[0] == 'D') || ((c[0] == 'A') && (c[1] == 'D')))))v = true;
			}
			rep(j, m - 2) {
				if ((c[j] == 'S') && (c[j + 1] == 'A') && (c[j + 2] == 'D'))ans++;
			}
		}
		x = min(a[0], b[0]);
		if ((x > 0) && (a[0] == d[0]) && (b[0] == d[0]))x--;
		s += x;
		a[0] -= x;
		b[0] -= x;

		x = min(a[1], b[1]);
		if ((x > 0) && (((a[1] == d[1]) && (b[1] == d[1]))||((!v)&&(a[0]==0)&&(b[0]==0))))x--;
		s += x;
		b[1] -= x;

		x = min(cnt, min(a[0], b[1]));
		if ((x > 0) && (x == a[0]) && (x == b[1]) && (!v))x--;
		s += x;
		ans += s;
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3464
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


template<typename T,bool directed>
struct Dinic{
  struct edge {
    int to;
    T cap;
    int rev;
    edge(){}
    edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}
  };

  vector<vector<edge> > G;
  vector<int> level,iter;

  Dinic(){}
  Dinic(int n):G(n),level(n),iter(n){}

  int add_edge(int from,int to,T cap){
    G[from].emplace_back(to,cap,G[to].size());
    G[to].emplace_back(from,directed?0:cap,G[from].size()-1);
    return G[to].back().rev;
  }

  void bfs(int s){
    fill(level.begin(),level.end(),-1);
    queue<int> que;
    level[s]=0;
    que.emplace(s);
    while(!que.empty()){
      int v=que.front();que.pop();
      for(int i=0;i<(int)G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[e.to]<0){
          level[e.to]=level[v]+1;
          que.emplace(e.to);
        }
      }
    }
  }

  T dfs(int v,int t,T f){
    if(v==t) return f;
    for(int &i=iter[v];i<(int)G[v].size();i++){
      edge &e=G[v][i];
      if(e.cap>0&&level[v]<level[e.to]){
        T d=dfs(e.to,t,min(f,e.cap));
        if(d==0) continue;
        e.cap-=d;
        G[e.to][e.rev].cap+=d;
        return d;
      }
    }
    return 0;
  }

  T flow(int s,int t,T lim){
    T fl=0;
    while(1){
      bfs(s);
      if(level[t]<0||lim==0) break;
      fill(iter.begin(),iter.end(),0);

      while(1){
        T f=dfs(s,t,lim);
        if(f==0) break;
        fl+=f;
        lim-=f;
      }
    }
    return fl;
  }

  T flow(int s,int t){
    return flow(s,t,numeric_limits<T>::max()/2);
  }

  T cut(int s,int t,int x,int a){
    static_assert(directed, "must be directed");
    auto &e=G[x][a];
    int y=e.to;
    T ce=e.cap,cr=G[y][e.rev].cap;
    if(cr==0) return 0;
    e.cap=G[y][e.rev].cap=0;
    T cap=cr-flow(x,y,cr);
    if(x!=s&&cap!=0) flow(x,s,cap);
    if(t!=y&&cap!=0) flow(t,y,cap);
    e.cap=ce+cr;
    return cap;
  }
};

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;

  vector<string> vs(n);
  for(int i=0;i<n;i++) cin>>vs[i];

  int cnt=0,ooo=0;
  for(string s:vs){
    int m=s.size();
    for(int i=0;i+2<m;i++)
      if(s[i]=='A'&&s[i+1]=='O'&&s[i+2]=='R') cnt++;
    if(s=="O") ooo++;
  }

  // 0: x?x
  // 1: x?A
  // 2: x?AO

  // 3: R?x
  // 4: R?A
  // 5: R?AO

  // 6: OR?x
  // 7: OR?A
  // 8: OR?AO
  vector<int> num(9,0);
  for(string s:vs){
    int m=s.size();
    int p=0,q=0;
    if(s[0]=='R'){
      p=1;
    }
    if(s[0]=='O'){
      if(1<m&&s[1]=='R') p=2;
    }

    if(s[m-1]=='A'){
      q=1;
    }
    if(s[m-1]=='O'){
      if(m-2>=0&&s[m-2]=='A') q=2;
    }
    num[p*3+q]++;
  }

  int sum=0;
  for(int i=1;i<9;i++) sum+=num[i];
  if(sum==0){
    cout<<cnt<<endl;
    return 0;
  }

  Dinic<int, true> G(22);
  int S=18,T=19,U=20,V=21;

  for(int i=0;i<9;i++){
    G.add_edge(S,i,num[i]);
    G.add_edge(9+i,T,num[i]);
  }

  for(int i=0;i<9;i++){
    for(int j=0;j<9;j++){
      int p=i%3,q=j/3;
      if((p==1&&q==2)||(p==2&&q==1))
        G.add_edge(i,9+j,num[i]-(i==j));

      if(p==1&&q==1){
        G.add_edge(i,U,num[i]-(i==j));
        G.add_edge(V,9+j,num[i]-(i==j));
      }
    }
  }
  G.add_edge(U,V,ooo);

  int res=G.flow(S,T);
  if(sum==res) res--;
  cout<<cnt+res<<endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8628
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

using cap_type = int;

struct edge {
    int to, rev;
    cap_type cap;
    edge(int t, cap_type c, int r) : to(t), rev(r), cap(c) {}
};
using graph = vector<vector<edge>>;

void add_edge(graph& g, int from, int to, cap_type cap) {
    g[from].emplace_back(to, cap, g[to].size());
    g[to].emplace_back(from, 0, g[from].size() - 1);
}

cap_type augment(graph& g, vector<int> const& level, vector<int>& iter, int v, int t, cap_type f) {
    if(v == t) return f;
    for(int& i = iter[v]; i < (int)g[v].size(); ++i) {
        auto& e = g[v][i];
        if(e.cap > 0 && level[v] < level[e.to]) {
            const auto d = augment(g, level, iter, e.to, t, min(f, e.cap));
            if(d > 0) {
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

cap_type max_flow(graph& g, int s, int t) {
    const auto inf = numeric_limits<cap_type>::max() / 2;
    cap_type flow = 0;
    while(true) {
        vector<int> level(g.size(), -1);
        level[s] = 0;
        queue<int> que;
        que.push(s);
        while(!que.empty()) {
            const int v = que.front();
            que.pop();
            for(auto const& e : g[v]) {
                if(e.cap > 0 && level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }

        if(level[t] < 0) return flow;
        vector<int> iter(g.size());
        cap_type f{0};
        while((f = augment(g, level, iter, s, t, inf)) > 0) {
            flow += f;
        }
    }
}

constexpr int inf = 1e9;

int main() {
    int n; cin >> n;
    vector<string> ss(n);
    int ans = 0;
    for(int i = 0; i < n; ++i) {
        cin >> ss[i];
    }

    map<string, int> idx, cnt;
    idx["-A"] = 0, idx["-AO"] = 1;
    idx["R-"] = 2, idx["R-A"] = 3, idx["R-AO"] = 4;
    idx["OR-"] = 5, idx["OR-A"] = 6, idx["OR-AO"] = 7;
    idx["O"] = 8;
    const int sz = idx.size();
    for(auto const& s : ss) {
        for(int j = 0; j + 3 <= (int)s.size(); ++j) {
            ans += s[j] == 'A' && s[j + 1] == 'O' && s[j + 2] == 'R';
        }
        if(s.size() >= 4 && s[0] == 'O' && s[1] == 'R' && s[s.size() - 2] == 'A' && s.back() == 'O') {
            cnt["OR-AO"] += 1;
        } else if(s.size() >= 3 && s[0] == 'O' && s[1] == 'R' && s.back() == 'A') {
            cnt["OR-A"] += 1;
        } else if(s.size() >= 3 && s[0] == 'R' && s[s.size() - 2] == 'A' && s.back() == 'O') {
            cnt["R-AO"] += 1;
        } else if(s.size() >= 2 && s[s.size() - 2] == 'A' && s.back() == 'O') {
            cnt["-AO"] += 1;
        } else if(s.size() >= 2 && s[0] == 'O' && s[1] == 'R') {
            cnt["OR-"] += 1;
        } else if(s.size() >= 2 && s[0] == 'R' && s.back() == 'A') {
            cnt["R-A"] += 1;
        } else if(s == "O") {
            cnt["O"] += 1;
        } else if(s.back() == 'A') {
            cnt["-A"] += 1;
        } else if(s[0] == 'R') {
            cnt["R-"] += 1;
        }
    }

    graph g(2 * sz + 2);
    const int src = 2 * sz, sink = src + 1;

    int cnt_sum = 0;
    for(auto const& p : idx) {
        if(p.first == "O") continue;
        add_edge(g, src, p.second, cnt[p.first]);
        add_edge(g, p.second + sz, sink, cnt[p.first]);
        cnt_sum += cnt[p.first];
    }
    add_edge(g, idx["-A"], idx["OR-"] + sz, cnt["-A"]);
    add_edge(g, idx["-A"], idx["OR-A"] + sz, cnt["-A"]);
    add_edge(g, idx["-A"], idx["OR-AO"] + sz, cnt["-A"]);
    add_edge(g, idx["-AO"], idx["R-"] + sz, cnt["-AO"]);
    add_edge(g, idx["-AO"], idx["R-A"] + sz, cnt["-AO"]);
    add_edge(g, idx["-AO"], idx["R-AO"] + sz, cnt["-AO"]);
    add_edge(g, idx["R-A"], idx["OR-"] + sz, cnt["R-A"]);
    add_edge(g, idx["R-A"], idx["OR-A"] + sz, cnt["R-A"]);
    add_edge(g, idx["R-A"], idx["OR-AO"] + sz, cnt["R-A"]);
    add_edge(g, idx["R-AO"], idx["R-"] + sz, cnt["R-AO"]);
    add_edge(g, idx["R-AO"], idx["R-A"] + sz, cnt["R-AO"]);
    add_edge(g, idx["R-AO"], idx["R-AO"] + sz, cnt["R-AO"] - 1);
    add_edge(g, idx["OR-A"], idx["OR-"] + sz, cnt["OR-A"]);
    add_edge(g, idx["OR-A"], idx["OR-A"] + sz, cnt["OR-A"] - 1);
    add_edge(g, idx["OR-A"], idx["OR-AO"] + sz, cnt["OR-A"]);
    add_edge(g, idx["OR-AO"], idx["R-"] + sz, cnt["OR-AO"]);
    add_edge(g, idx["OR-AO"], idx["R-A"] + sz, cnt["OR-AO"]);
    add_edge(g, idx["OR-AO"], idx["R-AO"] + sz, cnt["OR-AO"]);
    add_edge(g, idx["-A"], idx["O"], cnt["-A"]);
    add_edge(g, idx["R-A"], idx["O"], cnt["R-A"]);
    add_edge(g, idx["OR-A"], idx["O"], cnt["OR-A"]);
    add_edge(g, idx["O"] + sz, idx["R-"] + sz, cnt["O"]);
    add_edge(g, idx["O"] + sz, idx["R-A"] + sz, cnt["O"]);
    add_edge(g, idx["O"] + sz, idx["R-AO"] + sz, cnt["O"]);
    add_edge(g, idx["O"], idx["O"] + sz, cnt["O"]);

    const int flow = max_flow(g, src, sink);
    if(flow != 0 && flow == cnt_sum) { // cycle
        ans += flow - 1;
    } else {
        ans += flow;
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 9672
final_rank: 6

code_snippet: #include <iostream>
#include <string>

using namespace std;

int n;
string s[100005];

int main(void)
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> s[i];
	
	int ans = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j+2 < s[i].size(); j++){
			if(s[i].substr(j, 3) == "AOR") ans++;
		}
	}
	
	int Ocnt = 0, _A = 0, _AO = 0, R_ = 0, OR_ = 0, R_A = 0, OR_A = 0, R_AO = 0, OR_AO = 0;
	for(int i = 1; i <= n; i++){
		int l = s[i].size();
		if(s[i] == "O") Ocnt++;
		if(s[i].substr(l-1, 1) == "A")_A++;
		if(l >= 2 && s[i].substr(l-2, 2) == "AO") _AO++;
		if(s[i].substr(0, 1) == "R") R_++;
		if(l >= 2 && s[i].substr(0, 2) == "OR") OR_++;
		if(l >= 2 && s[i].substr(0, 1) == "R" && s[i].substr(l-1, 1) == "A") R_A++;
		if(l >= 3 && s[i].substr(0, 2) == "OR" && s[i].substr(l-1, 1) == "A") OR_A++;
		if(l >= 3 && s[i].substr(0, 1) == "R" && s[i].substr(l-2, 2) == "AO") R_AO++;
		if(l >= 4 && s[i].substr(0, 2) == "OR" && s[i].substr(l-2, 2) == "AO") OR_AO++;
	}
	int gomi = _A+_AO+R_+OR_ - (R_A+OR_A+R_AO+OR_AO) + Ocnt;
	gomi = n - gomi;
	
	int mx = 0;
	for(int i = 0; i <= min(Ocnt, _A); i++){
		int tmp1 = min(_A-i, OR_);
		if(tmp1 && OR_A == _A && OR_A == OR_ && i == 0) tmp1--;
		
		int tmp2 = min(_AO+i, R_);
		if(tmp2 && R_AO == _AO && R_AO == R_ && i == 0) tmp2--;
		
		mx = max(mx, tmp1+tmp2);
		//cout << tmp1 << " " << tmp2 << endl;
	}
	if(mx && mx == n-(Ocnt+gomi)) mx--;
	ans += mx;
	
	cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 9920
final_rank: 7

code_snippet: #include <iostream>
#include <string>
 
using namespace std;
 
int n;
string s[100005];
 
int main(void)
{
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> s[i];
     
    int ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j+2 < s[i].size(); j++){
            if(s[i].substr(j, 3) == "AOR") ans++;
        }
    }
     
    int Ocnt = 0, _A = 0, _AO = 0, R_ = 0, OR_ = 0, R_A = 0, OR_A = 0, R_AO = 0, OR_AO = 0;
    for(int i = 1; i <= n; i++){
        int l = s[i].size();
        if(s[i] == "O") Ocnt++;
        if(s[i].substr(l-1, 1) == "A")_A++;
        if(l >= 2 && s[i].substr(l-2, 2) == "AO") _AO++;
        if(s[i].substr(0, 1) == "R") R_++;
        if(l >= 2 && s[i].substr(0, 2) == "OR") OR_++;
        if(l >= 2 && s[i].substr(0, 1) == "R" && s[i].substr(l-1, 1) == "A") R_A++;
        if(l >= 3 && s[i].substr(0, 2) == "OR" && s[i].substr(l-1, 1) == "A") OR_A++;
        if(l >= 3 && s[i].substr(0, 1) == "R" && s[i].substr(l-2, 2) == "AO") R_AO++;
        if(l >= 4 && s[i].substr(0, 2) == "OR" && s[i].substr(l-2, 2) == "AO") OR_AO++;
    }
    int gomi = _A+_AO+R_+OR_ - (R_A+OR_A+R_AO+OR_AO) + Ocnt;
    gomi = n - gomi;
     
    int mx = 0;
    for(int i = 0; i <= min(Ocnt, _A); i++){
        int tmp1 = min(_A-i, OR_);
        if(tmp1 && OR_A == _A && OR_A == OR_ && i == 0) tmp1--;
         
        int tmp2 = min(_AO+i, R_);
        if(tmp2 && R_AO == _AO && R_AO == R_ && i == 0) tmp2--;
         
        mx = max(mx, tmp1+tmp2);
        //cout << tmp1 << " " << tmp2 << endl;
    }
    if(mx && mx == n-(Ocnt+gomi)) mx--;
    ans += mx;
     
    cout << ans << endl;
     
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 9936
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int> v(10);
    int ans = 0;
    while(n--){
        string s;
	    cin>>s;
	    for(int j=2;j<s.size();j++){
		    if(s[j-2]=='A'&&s[j-1]=='O'&&s[j]=='R')ans++;
	    }
	    if(s.size()==1&&s[0]=='O')v[9]++;
	    else if(s[0]=='R'){
		    if(s.back()=='A')v[1]++;
		    else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[2]++;
		    else v[3]++;
    	}
	    else if(s.size()>=2&&s[0]=='O'&&s[1]=='R'){
		    if(s.back()=='A')v[4]++;
    		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[5]++;
	    	else v[6]++;
	    }
    	else {
    		if(s.back()=='A')v[7]++;
    		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[8]++;
    	}
    }
    while(v[2]>=1&&v[1]+v[2]+v[3]+v[5]+v[8]>=2)--v[2],++ans;
    while(v[4]>=1&&v[4]+v[5]+v[6]+v[1]+v[7]>=2)--v[4],++ans;

    while(v[1]&&v[5]){
        --v[1],--v[5],++ans;
        if(v[5]+v[6]+v[1]+v[7]>=1)++ans;
        else if(v[1]+v[3]+v[5]+v[8]>=1)++ans;
        else ++v[4];
    }
    while(v[1]){
        if(v[1]&&v[6])--v[1],--v[6],++v[3],++ans;
        else if(v[1]&&v[8])--v[1],--v[8],++v[7],++ans;
        else break;
    }
    while(v[5]){
        if(v[5]&&v[3])--v[5],--v[3],++v[6],++ans;
        else if(v[5]&&v[7])--v[5],--v[7],++v[8],++ans;
        else break;
    }
    
    while(v[2]){
        if(v[2]&&v[3])--v[2],++ans;
        else if(v[2]&&v[8])--v[2],++ans;
        else break;
    }
    while(v[4]){
        if(v[4]&&v[6])--v[4],++ans;
        else if(v[4]&&v[7])--v[4],++ans;
        else break;
    }
    
    while(v[3]&&v[8])--v[3],--v[8],++ans;
    while(v[6]&&v[7])--v[6],--v[7],++ans;
    
    while(v[9]){
        if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[7])--v[2],--v[7],--v[9],++ans;
        else if(v[3]&&v[4])--v[3],--v[4],--v[9],++ans;
        else if(v[3]&&v[7])--v[3],--v[7],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[7])--v[1],--v[7],--v[9],++ans;
        else if(v[1]&&v[4])--v[1],--v[4],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[2])--v[1],--v[2],--v[9],++ans;
        else if(v[1]&&v[3])--v[1],--v[3],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]>=2)v[1]-=2,--v[9],++ans;
        else break;
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3212
final_rank: 3

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n;
    cin>>n;
    vector<int> v(10);
    int ans = 0;
    rep(_,n){
    string s;
	cin>>s;
	for(int j=2;j<s.size();j++){
		if(s[j-2]=='A'&&s[j-1]=='O'&&s[j]=='R')ans++;
	}
	if(s.size()==1&&s[0]=='O'){
		v[9]++;
	}
	else if(s[0]=='R'){
		if(s.back()=='A'){
			v[1]++;
		}
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[2]++;
		else v[3]++;
	}
	else if(s.size()>=2&&s[0]=='O'&&s[1]=='R'){
		if(s.back()=='A')v[4]++;
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[5]++;
		else v[6]++;
	}
	else {
		if(s.back()=='A')v[7]++;
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[8]++;
	}
    }
    while(v[2]>=1&&v[1]+v[2]+v[3]+v[5]+v[8]>=2){
        ans++;
        v[2]--;
    }
    while(v[4]>=1&&v[4]+v[5]+v[6]+v[1]+v[7]>=2){
        ans++;
        v[4]--;
    }
    while(v[1]&&v[5]){
        --v[1];
        --v[5];
        if(v[5]+v[6]+v[1]+v[7]>=1)++ans;
        else if(v[1]+v[3]+v[5]+v[8]>=1)++ans;
        else ++v[4];
        ++ans;
    }
    while(v[1]){
        if(v[1]&&v[6]){
            ++ans;
            --v[1];
            --v[6];
            ++v[3];
        }
        else if(v[1]&&v[8]){
            ++ans;
            --v[1];
            --v[8];
            ++v[7];
        }
        else break;
    }
    while(v[5]){
        if(v[5]&&v[3]){
            ++ans;
            --v[5];
            --v[3];
            ++v[6];
        }
        else if(v[5]&&v[7]){
            ++ans;
            --v[5];
            --v[7];
            ++v[8];
        }
        else break;
    }
    while(v[2]){
        if(v[2]&&v[3]){
            ++ans;
            --v[2];
        }
        else if(v[2]&&v[8]){
            ++ans;
            --v[2];
        }
        else break;
    }
    while(v[4]){
        if(v[4]&&v[6]){
            ++ans;
            --v[4];
        }
        else if(v[4]&&v[7]){
            ++ans;
            --v[4];
        }
        else break;
    }
    while(v[3]&&v[8]){
        --v[3];
        --v[8];
        ++ans;
    }
    while(v[6]&&v[7]){
        --v[6];
        --v[7];
        ++ans;
    }
    while(v[9]){
        if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[7])--v[2],--v[7],--v[9],++ans;
        else if(v[3]&&v[4])--v[3],--v[4],--v[9],++ans;
        else if(v[3]&&v[7])--v[3],--v[7],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[7])--v[1],--v[7],--v[9],++ans;
        else if(v[1]&&v[4])--v[1],--v[4],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[2])--v[1],--v[2],--v[9],++ans;
        else if(v[1]&&v[3])--v[1],--v[3],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]>=2)v[1]-=2,--v[9],++ans;
        else break;
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3212
final_rank: 3

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;



typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair

#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n;
char s[21];
int cnt[10],res,add;

typedef long long LL;
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
const int MAXN = 22222;
const int MAXM = 403;
struct Simplex{
  const double eps = 1E-10;
  double a[MAXN][MAXM], b[MAXN], c[MAXM], d[MAXN][MAXM];
  double x[MAXM];
  int ix[MAXN + MAXM]; // !!! array all indexed from 0
  // max{cx} subject to {Ax<=b,x>=0}
  // n: constraints, m: vars !!!
  // x[] is the optimal solution vector
  // usage : 
  // value = simplex(a, b, c, N, M);
  pair<double,bool> simplex(int n, int m){
    ++m;
    int r = n, s = m - 1;
    memset(d, 0, sizeof(d));
    for (int i = 0; i < n + m; ++i) ix[i] = i;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m - 1; ++j) d[i][j] = -a[i][j];
      d[i][m - 1] = 1;
      d[i][m] = b[i];
      if (d[r][m] > d[i][m]) r = i;
    }
    for (int j = 0; j < m - 1; ++j) d[n][j] = c[j];
    d[n + 1][m - 1] = -1;
    for (double dd;; ) {
      if (r < n) {
        int t = ix[s]; ix[s] = ix[r + m]; ix[r + m] = t;
        d[r][s] = 1.0 / d[r][s];
        for (int j = 0; j <= m; ++j)
          if (j != s) d[r][j] *= -d[r][s];
        for (int i = 0; i <= n + 1; ++i) if (i != r) {
          for (int j = 0; j <= m; ++j) if (j != s)
            d[i][j] += d[r][j] * d[i][s];
          d[i][s] *= d[r][s];
        }
      }
      r = -1; s = -1;
      for (int j = 0; j < m; ++j)
        if (s < 0 || ix[s] > ix[j]) {
          if (d[n + 1][j] > eps ||
              (d[n + 1][j] > -eps && d[n][j] > eps))
            s = j;
        }
      if (s < 0) break;
      for (int i = 0; i < n; ++i) if (d[i][s] < -eps) {
        if (r < 0 ||
            (dd = d[r][m] / d[r][s] - d[i][m] / d[i][s]) < -eps ||
            (dd < eps && ix[r + m] > ix[i + m]))
          r = i;
      }
      if (r < 0) return { 0 , false }; // not bounded
    }
    if (d[n + 1][m] < -eps) return { 0 , false }; // not executable
    double ans = 0;
    for(int i=0; i<m; i++) x[i] = 0;
    for (int i = m; i < n + m; ++i) { // the missing enumerated x[i] = 0
      if (ix[i] < m - 1){
        ans += d[i - m][m] * c[ix[i]];
        x[ix[i]] = d[i-m][m];
      }
    }
    return { ans , true }; 
  }
} solver;

int main(){
	scanf("%d",&n);
	rep(i,n){
		scanf("%s",&s);
		int n = strlen(s);
		if(n==1 && s[0] == 'O') cnt[8]++;
		int x=-1,y=-1;
		if(s[0] == 'R') x=2;
		if(n >= 2 && s[0] == 'O' && s[1] == 'R') x = 1;
		
		if(s[n-1] == 'A') y=1;
		if(n >= 2 && s[n-2] == 'A' && s[n-1] == 'O') y=2;
		
		if(x==1&&y==1) cnt[0]++;
		if(x==1&&y==2) cnt[1]++;
		if(x==2&&y==1) cnt[2]++;
		if(x==2&&y==2) cnt[3]++;
		if(x==-1&&y==1) cnt[4]++;
		if(x==-1&&y==2) cnt[5]++;
		if(x==1&&y==-1) cnt[6]++;
		if(x==2&&y==-1) cnt[7]++;
		for(int j=0;j<n-2;j++){
			if(s[j]=='A' && s[j+1]=='O' && s[j+2] == 'R') res++;
		}
	}
	int var = 64;
  	int con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    }
	    int nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = 1;
				}
			}
			 solver.b[ nxt++ ] = cnt[8];
	  pair<double,bool> ans = solver.simplex(con, var);
	  if(ans.sc) add = max(add,(int)floor(ans.fi+0.1));
	  
	  
	 var = 64;
  	con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	//if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 0;
	    }
	nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = -1;
				}
			}
			 solver.b[ nxt++ ] = -cnt[8];
	  ans = solver.simplex(con, var);
	  if(ans.sc)add = max(add,(int)floor(ans.fi+0.1)+cnt[8]);
	 /* int S = 0;
	  rep(i,8) S += cnt[i];
	  if(S == add){
	  	add--;
	  }
	  add = max(add,0);*/
	  cout << res+add << endl;
	 }
accuracy: 0.027522935779816515
time_ms: 20
memory_kb: 74268
final_rank: 11

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;



typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair

#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n;
char s[21];
int cnt[10],res,add;

typedef long long LL;
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
const int MAXN = 22222;
const int MAXM = 403;
struct Simplex{
  const double eps = 1E-10;
  double a[MAXN][MAXM], b[MAXN], c[MAXM], d[MAXN][MAXM];
  double x[MAXM];
  int ix[MAXN + MAXM]; // !!! array all indexed from 0
  // max{cx} subject to {Ax<=b,x>=0}
  // n: constraints, m: vars !!!
  // x[] is the optimal solution vector
  // usage : 
  // value = simplex(a, b, c, N, M);
  pair<double,bool> simplex(int n, int m){
    ++m;
    int r = n, s = m - 1;
    memset(d, 0, sizeof(d));
    for (int i = 0; i < n + m; ++i) ix[i] = i;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m - 1; ++j) d[i][j] = -a[i][j];
      d[i][m - 1] = 1;
      d[i][m] = b[i];
      if (d[r][m] > d[i][m]) r = i;
    }
    for (int j = 0; j < m - 1; ++j) d[n][j] = c[j];
    d[n + 1][m - 1] = -1;
    for (double dd;; ) {
      if (r < n) {
        int t = ix[s]; ix[s] = ix[r + m]; ix[r + m] = t;
        d[r][s] = 1.0 / d[r][s];
        for (int j = 0; j <= m; ++j)
          if (j != s) d[r][j] *= -d[r][s];
        for (int i = 0; i <= n + 1; ++i) if (i != r) {
          for (int j = 0; j <= m; ++j) if (j != s)
            d[i][j] += d[r][j] * d[i][s];
          d[i][s] *= d[r][s];
        }
      }
      r = -1; s = -1;
      for (int j = 0; j < m; ++j)
        if (s < 0 || ix[s] > ix[j]) {
          if (d[n + 1][j] > eps ||
              (d[n + 1][j] > -eps && d[n][j] > eps))
            s = j;
        }
      if (s < 0) break;
      for (int i = 0; i < n; ++i) if (d[i][s] < -eps) {
        if (r < 0 ||
            (dd = d[r][m] / d[r][s] - d[i][m] / d[i][s]) < -eps ||
            (dd < eps && ix[r + m] > ix[i + m]))
          r = i;
      }
      if (r < 0) return { 0 , false }; // not bounded
    }
    if (d[n + 1][m] < -eps) return { 0 , false }; // not executable
    double ans = 0;
    for(int i=0; i<m; i++) x[i] = 0;
    for (int i = m; i < n + m; ++i) { // the missing enumerated x[i] = 0
      if (ix[i] < m - 1){
        ans += d[i - m][m] * c[ix[i]];
        x[ix[i]] = d[i-m][m];
      }
    }
    return { ans , true }; 
  }
} solver;

int main(){
	scanf("%d",&n);
	rep(i,n){
		scanf("%s",&s); int n = strlen(s);
		if(n==1 && s[0] == 'O') cnt[8]++;
		int x=-1,y=-1;
		if(s[0] == 'R') x=2;
		if(n >= 2 && s[0] == 'O' && s[1] == 'R') x = 1;
		
		if(s[n-1] == 'A') y=1;
		if(n >= 2 && s[n-2] == 'A' && s[n-1] == 'O') y=2;
		
		if(x==1&&y==1) cnt[0]++;
		if(x==1&&y==2) cnt[1]++;
		if(x==2&&y==1) cnt[2]++;
		if(x==2&&y==2) cnt[3]++;
		if(x==-1&&y==1) cnt[4]++;
		if(x==-1&&y==2) cnt[5]++;
		if(x==1&&y==-1) cnt[6]++;
		if(x==2&&y==-1) cnt[7]++;
		for(int j=0;j<n-2;j++){
			if(s[j]=='A' && s[j+1]=='O' && s[j+2] == 'R') res++;
		}
	}
	int var = 64;
  	int con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    }
	    int nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = 1;
				}
			}
			 solver.b[ nxt++ ] = cnt[8];
	  pair<double,bool> ans = solver.simplex(con, var);
	  if(ans.sc) add = max(add,(int)floor(ans.fi+0.1));
	 var = 64;
  	con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 0;
	    }
	nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = -1;
				}
			}
			 solver.b[ nxt++ ] = -cnt[8];
	  ans = solver.simplex(con, var);
	  if(ans.sc)add = max(add,(int)floor(ans.fi+0.1)+cnt[8]);
	 
	  int S = 0;
	  rep(i,8) S += cnt[i];
	  if(S == add){
	  	add--;
	  }
	  add = max(add,0);
	  cout << res+add << endl;
	 }
accuracy: 0.05504587155963303
time_ms: 20
memory_kb: 74360
final_rank: 10

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;



typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair

#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n;
char s[21];
int cnt[10],res,add;

typedef long long LL;
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
const int MAXN = 22222;
const int MAXM = 403;
struct Simplex{
  const double eps = 1E-10;
  double a[MAXN][MAXM], b[MAXN], c[MAXM], d[MAXN][MAXM];
  double x[MAXM];
  int ix[MAXN + MAXM]; // !!! array all indexed from 0
  // max{cx} subject to {Ax<=b,x>=0}
  // n: constraints, m: vars !!!
  // x[] is the optimal solution vector
  // usage : 
  // value = simplex(a, b, c, N, M);
  pair<double,bool> simplex(int n, int m){
    ++m;
    int r = n, s = m - 1;
    memset(d, 0, sizeof(d));
    for (int i = 0; i < n + m; ++i) ix[i] = i;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m - 1; ++j) d[i][j] = -a[i][j];
      d[i][m - 1] = 1;
      d[i][m] = b[i];
      if (d[r][m] > d[i][m]) r = i;
    }
    for (int j = 0; j < m - 1; ++j) d[n][j] = c[j];
    d[n + 1][m - 1] = -1;
    for (double dd;; ) {
      if (r < n) {
        int t = ix[s]; ix[s] = ix[r + m]; ix[r + m] = t;
        d[r][s] = 1.0 / d[r][s];
        for (int j = 0; j <= m; ++j)
          if (j != s) d[r][j] *= -d[r][s];
        for (int i = 0; i <= n + 1; ++i) if (i != r) {
          for (int j = 0; j <= m; ++j) if (j != s)
            d[i][j] += d[r][j] * d[i][s];
          d[i][s] *= d[r][s];
        }
      }
      r = -1; s = -1;
      for (int j = 0; j < m; ++j)
        if (s < 0 || ix[s] > ix[j]) {
          if (d[n + 1][j] > eps ||
              (d[n + 1][j] > -eps && d[n][j] > eps))
            s = j;
        }
      if (s < 0) break;
      for (int i = 0; i < n; ++i) if (d[i][s] < -eps) {
        if (r < 0 ||
            (dd = d[r][m] / d[r][s] - d[i][m] / d[i][s]) < -eps ||
            (dd < eps && ix[r + m] > ix[i + m]))
          r = i;
      }
      if (r < 0) return { 0 , false }; // not bounded
    }
    if (d[n + 1][m] < -eps) return { 0 , false }; // not executable
    double ans = 0;
    for(int i=0; i<m; i++) x[i] = 0;
    for (int i = m; i < n + m; ++i) { // the missing enumerated x[i] = 0
      if (ix[i] < m - 1){
        ans += d[i - m][m] * c[ix[i]];
        x[ix[i]] = d[i-m][m];
      }
    }
    return { ans , true }; 
  }
} solver;

int main(){
	scanf("%d",&n);
	rep(i,n){
		scanf("%s",&s); int n = strlen(s);
		if(n==1 && s[0] == 'O') cnt[8]++;
		int x=-1,y=-1;
		if(s[0] == 'R') x=2;
		if(n >= 2 && s[0] == 'O' && s[1] == 'R') x = 1;
		
		if(s[n-1] == 'A') y=1;
		if(n >= 2 && s[n-2] == 'A' && s[n-1] == 'O') y=2;
		
		if(x==1&&y==1) cnt[0]++;
		if(x==1&&y==2) cnt[1]++;
		if(x==2&&y==1) cnt[2]++;
		if(x==2&&y==2) cnt[3]++;
		if(x==-1&&y==1) cnt[4]++;
		if(x==-1&&y==2) cnt[5]++;
		if(x==1&&y==-1) cnt[6]++;
		if(x==2&&y==-1) cnt[7]++;
		for(int j=0;j<n-2;j++){
			if(s[j]=='A' && s[j+1]=='O' && s[j+2] == 'R') res++;
		}
	}
	int var = 64;
  	int con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    }
	    int nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = 1;
				}
			}
			 solver.b[ nxt++ ] = cnt[8];
	  pair<double,bool> ans = solver.simplex(con, var);
	  if(ans.sc) add = max(add,(int)floor(ans.fi+0.5));
	  
	  
	 var = 64;
  	con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 0;
	    }
	nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = -1;
				}
			}
			 solver.b[ nxt++ ] = -cnt[8];
	  ans = solver.simplex(con, var);
	  if(ans.sc)add = max(add,(int)floor(ans.fi+0.5)+cnt[8]);
	  
	  int S = 0;
	  rep(i,8) S += cnt[i];
	  if(S == add){
	  	add--;
	  }
	  add = max(add,0);
	  cout << res+add << endl;
	 }
accuracy: 0.05504587155963303
time_ms: 20
memory_kb: 74272
final_rank: 9

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;



typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair

#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n;
char s[21];
int cnt[10],res,add;

typedef long long LL;
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
const int MAXN = 22222;
const int MAXM = 403;
struct Simplex{
  const double eps = 1E-10;
  double a[MAXN][MAXM], b[MAXN], c[MAXM], d[MAXN][MAXM];
  double x[MAXM];
  int ix[MAXN + MAXM]; // !!! array all indexed from 0
  // max{cx} subject to {Ax<=b,x>=0}
  // n: constraints, m: vars !!!
  // x[] is the optimal solution vector
  // usage : 
  // value = simplex(a, b, c, N, M);
  pair<double,bool> simplex(int n, int m){
    ++m;
    int r = n, s = m - 1;
    memset(d, 0, sizeof(d));
    for (int i = 0; i < n + m; ++i) ix[i] = i;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m - 1; ++j) d[i][j] = -a[i][j];
      d[i][m - 1] = 1;
      d[i][m] = b[i];
      if (d[r][m] > d[i][m]) r = i;
    }
    for (int j = 0; j < m - 1; ++j) d[n][j] = c[j];
    d[n + 1][m - 1] = -1;
    for (double dd;; ) {
      if (r < n) {
        int t = ix[s]; ix[s] = ix[r + m]; ix[r + m] = t;
        d[r][s] = 1.0 / d[r][s];
        for (int j = 0; j <= m; ++j)
          if (j != s) d[r][j] *= -d[r][s];
        for (int i = 0; i <= n + 1; ++i) if (i != r) {
          for (int j = 0; j <= m; ++j) if (j != s)
            d[i][j] += d[r][j] * d[i][s];
          d[i][s] *= d[r][s];
        }
      }
      r = -1; s = -1;
      for (int j = 0; j < m; ++j)
        if (s < 0 || ix[s] > ix[j]) {
          if (d[n + 1][j] > eps ||
              (d[n + 1][j] > -eps && d[n][j] > eps))
            s = j;
        }
      if (s < 0) break;
      for (int i = 0; i < n; ++i) if (d[i][s] < -eps) {
        if (r < 0 ||
            (dd = d[r][m] / d[r][s] - d[i][m] / d[i][s]) < -eps ||
            (dd < eps && ix[r + m] > ix[i + m]))
          r = i;
      }
      if (r < 0) return { 0 , false }; // not bounded
    }
    if (d[n + 1][m] < -eps) return { 0 , false }; // not executable
    double ans = 0;
    for(int i=0; i<m; i++) x[i] = 0;
    for (int i = m; i < n + m; ++i) { // the missing enumerated x[i] = 0
      if (ix[i] < m - 1){
        ans += d[i - m][m] * c[ix[i]];
        x[ix[i]] = d[i-m][m];
      }
    }
    return { ans , true }; 
  }
} solver;

int main(){
	scanf("%d",&n);
	rep(i,n){
		scanf("%s",&s); int n = strlen(s);
		if(s == "O") cnt[8]++;
		int x=-1,y=-1;
		if(s[0] == 'R') x=2;
		if(n >= 2 && s[0] == 'O' && s[1] == 'R') x = 1;
		
		if(s[n-1] == 'A') y=1;
		if(n >= 2 && s[n-2] == 'A' && s[n-1] == 'O') y=2;
		
		if(x==1&&y==1) cnt[0]++;
		if(x==1&&y==2) cnt[1]++;
		if(x==2&&y==1) cnt[2]++;
		if(x==2&&y==2) cnt[3]++;
		if(x==-1&&y==1) cnt[4]++;
		if(x==-1&&y==2) cnt[5]++;
		if(x==1&&y==-1) cnt[6]++;
		if(x==2&&y==-1) cnt[7]++;
		for(int j=0;j<n-2;j++){
			if(s[j]=='A' && s[j+1]=='O' && s[j+2] == 'R') res++;
		}
	}
	int var = 64;
  	int con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    }
	    int nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = 1;
				}
			}
			 solver.b[ nxt++ ] = cnt[8];
	  pair<double,bool> ans = solver.simplex(con, var);
	  if(ans.sc) add = max(add,(int)floor(ans.fi+0.5));
	  
	  
	 var = 64;
  	con = 2*(8+8+70+56+28+8+1);
	//出次数 = cnt
	//出次数 = 入次数
	//連結
	for( int j = 0 ; j < con ; j ++ )
    for( int i = 0 ; i < var ; i ++ )
      solver.a[ j ][ i ] = 0;
	  for( int i = 0 ; i < con ; i ++ )
	    solver.b[ i ] = 0;
	  for( int i = 0 ; i < var ; i ++ )
	    {
	    	int aa = i/8, ab = i%8;
	    	solver.c[ i ] = 0;
	    	if( (aa==0||aa==2||aa==4) && (ab==0||ab==1||ab==6)) solver.c[ i ] = 1;
	    	if( (aa==1||aa==3||aa==5) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 1;
	    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)) solver.c[ i ] = 0;
	    }
	nxt = 0;
	  // max{cx} subject to {Ax<=b,x>=0}
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = 1;
				   }
			}
			solver.b[ nxt++ ] = cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] = -1;
				   }
			}
			solver.b[ nxt++ ] = -cnt[i];
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] += 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for( int i = 0 ; i < 8 ; i ++ ){
		  for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(aa == i){
				    solver.a[ nxt ][ j ] -= 1;
				}
				if(ab == i){
				    solver.a[ nxt ][ j ] += 1;
				}
			}
			solver.b[ nxt++ ] = 0;
	  }
	  for(int i=1;i<(1<<8);i++){
		if(__builtin_popcount(i) > 4) continue;
		int x[10]={};
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) x[j] = 1;
		}
		int as=0,bs=0;
		for(int j=0;j<8;j++){
			if(((i>>j)&1)) as+=cnt[j]; else bs+=cnt[j];
		}
		if(as==0||bs==0) continue;
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if(x[aa] != x[ab]){
		    		solver.a[ nxt ][ j ] -= 1;
		    	}
		   }
		 solver.b[ nxt++ ] = -1;
		}
		for( int j = 0 ; j < var ; j ++ )
		  {
		    	int aa = j/8, ab = j%8;
		    	if( (aa==0||aa==2||aa==4) && (ab==2||ab==3||ab==7)){
				    solver.a[ nxt ][ j ] = -1;
				}
			}
			 solver.b[ nxt++ ] = -cnt[8];
	  ans = solver.simplex(con, var);
	  if(ans.sc)add = max(add,(int)floor(ans.fi+0.5)+cnt[8]);
	  
	  int S = 0;
	  rep(i,8) S += cnt[i];
	  if(S == add){
	  	add--;
	  }
	  add = max(add,0);
	  cout << res+add << endl;
	 }
accuracy: 0.01834862385321101
time_ms: 20
memory_kb: 74272
final_rank: 12

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n;
    cin>>n;
    vector<int> v(10);
    int ans = 0;
    rep(_,n){
    string s;
	cin>>s;
	for(int j=2;j<s.size();j++){
		if(s[j-2]=='A'&&s[j-1]=='O'&&s[j]=='R')ans++;
	}
	if(s.size()==1&&s[0]=='O'){
		v[9]++;
	}
	else if(s[0]=='R'){
		if(s.back()=='A'){
			v[1]++;
		}
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[2]++;
		else v[3]++;
	}
	else if(s.size()>=2&&s[0]=='O'&&s[1]=='R'){
		if(s.back()=='A')v[4]++;
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[5]++;
		else v[6]++;
	}
	else {
		if(s.back()=='A')v[7]++;
		else if(s.size()>=2&&s[s.size()-2]=='A'&&s.back()=='O')v[8]++;
	}
    }
    while(v[2]>=1&&v[1]+v[2]+v[3]+v[5]+v[8]>=2){
        ans++;
        v[2]--;
    }
    while(v[4]>=1&&v[4]+v[5]+v[6]+v[1]+v[7]>=2){
        ans++;
        v[4]--;
    }
    while(v[1]&&v[5]){
        --v[1];
        --v[5];
        if(v[5]+v[6]+v[1]+v[7]>=1)++ans;
        else if(v[1]+v[3]+v[5]+v[8]>=1)++ans;
        else ++v[2];
        ++ans;
    }
    while(v[1]){
        if(v[1]&&v[6]){
            ++ans;
            --v[1];
            --v[6];
            ++v[3];
        }
        else if(v[1]&&v[8]){
            ++ans;
            --v[1];
            --v[8];
            ++v[7];
        }
        else break;
    }
    while(v[5]){
        if(v[5]&&v[3]){
            ++ans;
            --v[5];
            --v[3];
            ++v[6];
        }
        else if(v[5]&&v[7]){
            ++ans;
            --v[5];
            --v[7];
            ++v[8];
        }
        else break;
    }
    while(v[2]){
        if(v[2]&&v[3]){
            ++ans;
            --v[2];
        }
        else if(v[2]&&v[8]){
            ++ans;
            --v[2];
        }
        else break;
    }
    while(v[4]){
        if(v[4]&&v[6]){
            ++ans;
            --v[4];
        }
        else if(v[4]&&v[7]){
            ++ans;
            --v[4];
        }
        else break;
    }
    while(v[3]&&v[8]){
        --v[3];
        --v[8];
        ++ans;
    }
    while(v[6]&&v[7]){
        --v[6];
        --v[7];
        ++ans;
    }
    while(v[9]){
        if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[4])--v[2],--v[4],--v[9],++ans;
        else if(v[2]&&v[7])--v[2],--v[7],--v[9],++ans;
        else if(v[3]&&v[4])--v[3],--v[4],--v[9],++ans;
        else if(v[3]&&v[7])--v[3],--v[7],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[7])--v[1],--v[7],--v[9],++ans;
        else if(v[1]&&v[4])--v[1],--v[4],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]&&v[2])--v[1],--v[2],--v[9],++ans;
        else if(v[1]&&v[3])--v[1],--v[3],--v[9],++ans;
        else break;
    }
    while(v[9]){
        if(v[1]>=2)v[1]-=2,--v[9],++ans;
        else break;
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3212
final_rank: 3