I'm trying to solve the following programming problem:
Once Petya and Vasya invented a new game and called it "Smart Boy". They located a certain set of words — the dictionary — for the game. It is admissible for the dictionary to contain similar words.

The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word from the dictionary and writes it down on a piece of paper. The second player adds some other letter to this one's initial or final position, thus making a word as long as 2, then it's the first player's turn again, he adds a letter in the beginning or in the end thus making a word as long as 3 and so on. But the player mustn't break one condition: the newly created word must be a substring of a word from a dictionary. The player who can't add a letter to the current word without breaking the condition loses.
Also if by the end of a turn a certain string s is written on paper, then the player, whose turn it just has been, gets a number of points according to the formula: score(s) = (sum from i=1 to i=|s| of value(s_i))*(max from i=1 to i=|s| of value(s_i))+num(s)
where:
value(c) is a sequence number of symbol c in Latin alphabet, numbered starting from 1. For example, value(a)=1 , and value(z) = 26.
num(s) is the number of words from the dictionary where s occurs as a substring at least once.
Your task is to learn who will win the game and what the final score will be. Every player plays optimally and most of all tries to win, then — to maximize the number of his points, then — to minimize the number of the points of the opponent.

Input
The first input line contains an integer n which is the number of words in the located dictionary (1 ≤ n ≤ 30). The n lines contain the words from the dictionary — one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can be in the list of words.

Output
On the first output line print a line "First" or "Second" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space.

Examples
Input
2
aba
abac
Output
Second
29 35
Input
3
artem
nik
max
Output
First
2403 1882


I created the following code, it works on most test cases but it's not perfect:

#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define filein freopen("input.in", "r", stdin);
#define fileout freopen("output.txt", "w", stdout);
#define ld long double
#define ll long long
#define ull unsigned long long
using namespace std;

struct trie
{
    trie* nx[26] = {};
    ll val = 0;
    vector<ll> ans;
    bool done = false;
};

trie *lr = new trie, *rl = new trie;

vector<ll> solve(const string &x)
{
    trie *p1 = lr, *p2 = rl;
    for (auto u : x) p1 = p1->nx[u-'a'];
    for (ll i = x.size()-1; i >= 0; i--) p2 = p2->nx[x[i]-'a'];
    if (p1->done) return p1->ans;
    p1->done = true;
    vector<ll> ans = {LLONG_MIN, LLONG_MAX, 0};
    for (ll i = 0; i < 26; i++)
    {
        if (p1->nx[i])
        {
            vector<ll> temp = solve(x+char(i+'a'));
            temp[1] += p1->nx[i]->val;
            if (!temp[2] || !ans[2])
            {
                ans[2] = !temp[2];
                if (temp[1] > ans[0] || (temp[1] == ans[0] && temp[0] < ans[1]))
                {
                    ans[0] = temp[1];
                    ans[1] = temp[0];
                }
            }
        }
        if (p2->nx[i])
        {
            vector<ll> temp = solve(char(i+'a')+x);
            temp[1] += p2->nx[i]->val;
            if (!temp[2] || !ans[2])
            {
                ans[2] = !temp[2];
                if (temp[1] > ans[0] || (temp[1] == ans[0] && temp[0] < ans[1]))
                {
                    ans[0] = temp[1];
                    ans[1] = temp[0];
                }
            }
        }
    }
    if (ans[0] == LLONG_MIN) ans = {0, 0, 0};
    return p1->ans = ans;
}

int main()
{fastio
    ll n;
    set<string> words;
    cin >> n;
    string arr[n];
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        for (ll l = 0; l < arr[i].size(); l++)
        {
            string w;
            for (ll r = l; r < arr[i].size(); r++)
            {
                w += arr[i][r];
                words.insert(w);
            }
        }
    }
    for (auto u : words)
    {
        ll num = 0, mx = 0, sum = 0;
        for (auto i : u) mx = max(mx, (ll)i-'a'+1), sum += i-'a'+1;
        for (ll i = 0; i < n; i++) num += arr[i].find(u) != string::npos;
        trie *cur = lr;
        for (auto x : u)
        {
            x -= 'a';
            if (!cur->nx[x]) cur->nx[x] = new trie;
            cur = cur->nx[x];
        }
        cur->val = num+mx*sum;
        cur = rl;
        reverse(u.begin(), u.end());
        for (auto x : u)
        {
            x -= 'a';
            if (!cur->nx[x]) cur->nx[x] = new trie;
            cur = cur->nx[x];
        }
        cur->val = num+mx*sum;
    }
    vector<ll> ans = solve("");
    cout << (ans[2] ? "First" : "Second") << '\n' << ans[0] << ' ' << ans[1];
}

here is the test case that makes it fail:
Input
25
cwvkubhsyfcc
aozqfobjrknitn
drgymqrbecum
smjnogaewohzquk
nr
t
adnuduxdbgivwwlac
cgypvxjlqztw
zuukgmzszyxkolf
yebejdewaqgx
kxrhkkuuyzlxfojvjftj
ectwyvfwxctfohsuloyebz
wcjowbrckawmn
ohhpbbfqqnhqmgjxa
oabbtzpmdcpxqo
mqwh
njlonviiqklchdxivfryjyafi
iobcqnbhhqcqigoy
mtqhfgkhbckhspoc
wrcloqcxqwxatt
fnko
iddycfjurpbtohwn
gagxcaemumdzcezomoyfybiel
bp
csctsfhaeuiyppncjdaswal

my output: 
Second
59340 55032

the correct output:
Second
51645 55834

can you help me troubleshoot?