<file_sep>#include <bits/stdc++.h>
#define int long long
using namespace std;
map<pair<int, int>, string> mp;
int answer[50][40];

int decodeExpr(int x, int y, string expr) {
    if(answer[x][y] != 0 )
        return answer[x][y];
    if (expr.empty()) {
        answer[x][y] = 0;
        return 0;
    }
    if (expr[0] != '=') {
        int aux = 0;
        for (char i : expr) {
            aux = aux * 10 + i - '0';
        }
        answer[x][y] = aux;
        return aux;
    }
    vector<int> numExpr;
    numExpr.push_back(-5);
    int curr = 0;
    for (int i = 1; i < expr.size(); ++i) {
        if (isdigit(expr[i])) {
            curr = curr* 10 + expr[i]-'0';
        } else {
            numExpr.push_back(curr);
            curr=0;
            if(expr[i] == ':')
                numExpr.push_back(-1);
            else
                numExpr.push_back(-2);
        }
    }
    if(curr)
        numExpr.push_back(curr);
//    for(auto it: numExpr) {
//        cerr << it << " ";
//    }
//    cerr <<"\n";
    map<int, bool> positions;
    vector<int> terms;
    for (int i = 1; i < numExpr.size(); ++i) {
        if (numExpr[i] == -1) {
            positions[i - 1] = positions[i] = positions[i + 1] = true;
            terms.push_back(decodeExpr(numExpr[i - 1] , numExpr[i + 1],
                                       mp[{numExpr[i - 1] , numExpr[i + 1] }]));
        }
    }
    for (int i = 1; i < numExpr.size(); ++i) {
        if (!positions[i] && numExpr[i]>0)
            terms.push_back(numExpr[i] );
    }
    int aux = 0;
    for (auto it: terms) {
        aux += it;
    }
    answer[x][y] = aux;
    return aux;
}

int32_t main() {
    freopen("mxl.in", "r", stdin);
    freopen("mxl.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int lin, col;
        string kekxpression;
        cin >> lin >> col >> kekxpression;
        mp[make_pair(lin, col)] = kekxpression;
        //cerr << lin<< " " <<col <<" " <<  kekxpression << "\n";
    }
    //cerr<<mp[{1,6}].size();
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (answer[i][j] == 0) {
                decodeExpr(i, j, mp[{i, j}]);
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            cout << answer[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}<file_sep>#include <bits/stdc++.h>

using namespace std;

string expression;
unordered_map<char, int> fact;
unordered_map<long long, int> firstHalf;
int main() {
    freopen("eq4.in", "r", stdin);
    freopen("eq4.out", "w", stdout);
    int cer;
    int a, b;
    long long res;
    cin >> cer;
    cin >> expression;
    cin >> a >> b >> res;
    int num = 0, sign = 1;
    bool hasNumber = false;
    fact['@'] = 0;
    fact['x'] = 0;
    fact['y'] = 0;
    fact['z'] = 0;
    fact['t'] = 0;
    for (int i = 0; i < expression.size(); ++i) {
        if (expression[i] == '+' || expression[i] == '-') {
            num = 0;
            hasNumber = false;
            if (expression[i] == '+') {
                sign = 1;
            } else
                sign = -1;
            continue;
        }
        if (isdigit(expression[i])) {
            num = num * 10 + sign * (expression[i] - '0');
            hasNumber = true;
        }
        if (isalpha(expression[i])) {
            if (!hasNumber)
                num = sign;
            fact[expression[i]] += num;
            continue;
        }
        if (expression[i + 1] == '+' || expression[i + 1] == '-' || i == expression.size() - 1) {
            if (!hasNumber)
                num = sign;
            fact['@'] += num;
        }
    }
    if (cer == 1) {
        long long sum = 0;
        for (auto it: fact) {
            sum += it.second;
        }
        cout << sum;
        return 0;
    }
    int cnt = 0;
    for (int valx = a; valx <= b; ++valx) {
        for (int valy = a; valy <= b; ++valy) {
            long long sum = 0;
            sum += fact['@'];
            sum += fact['x'] * valx;
            sum += fact['y'] * valy;
            firstHalf[sum]++;
        }
    }
    for (int valz = a; valz <= b; ++valz) {
        for (int valt = a; valt <= b; ++valt) {
            long long sum = 0;
            sum += fact['z'] * valz;
            sum += fact['t'] * valt;
            if(firstHalf.find(res-sum)!= firstHalf.end())
                cnt+=firstHalf[res-sum];
        }
    }
    cout << cnt;
    return 0;
}<file_sep>#include <fstream>
#include <algorithm>

using namespace std;

char st[100005];
int top = 0;
ifstream cin("charlie.in");
ofstream cout("charlie.out");

int main() {
    int reqFlag;
    cin >> reqFlag;
    string s;
    cin >> s;
    if (reqFlag == 1) {
        int cnt = 1;
        int ans = 0;
        for (int i = 1; i < s.size() - 1; ++i) {
            if (s[i] < s[i - 1] && s[i] < s[i + 1]) {
                cnt += 2;
                i++;
                ans = max(ans, cnt);
                continue;
            }
            cnt = 1;
            ans = max(ans, cnt);
        }
        ans = max(ans, cnt);
        cout << ans << "\n";
        return 0;
    }
    int cost = 0;
    for (char & i : s) {
        while (top > 1 && st[top - 1] > st[top] && st[top] < i) {
            cost += max(st[top - 1], i) - 'a' + 1;
            top--;
        }
        st[++top] = i;
    }
    string printBuff;
    while (top > 0) {
        printBuff += st[top];
        top--;
    }
    for (int i = printBuff.size() - 1; i >= 0; --i) {

        cout << printBuff[i];
    }
    cout << "\n" << cost;
}<file_sep>#include <bits/stdc++.h>
using namespace std;
int mat[1005][1005];
int ans[1005][1005];
int n,m;
bool inBounds (int x, int y) {
    return (x>0 && x<=n) && (y>0 && y<=m);
}
ifstream in ("acces.in");
ofstream out ("acces.out");
int main() {
    freopen("acces.in","r", stdin);
    freopen("acces.out","w", stdout);
    int k;
    in >> n >> m;
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=m;++j) {
            in >> mat[i][j];
        }
    }
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=m;++j) {
            if(mat[i][j] == 1) {
                ans[i][j] = min(min(ans[i][j-1], ans[i-1][j]), ans[i-1][j-1]);
                continue;
            }
            ans[i][j] = 1;
            if(mat[i-1][j]==0)
                ans[i][j] += ans[i-1][j];
            if(mat[i][j-1]==0)
                ans[i][j] += ans[i][j-1];
            if(mat[i-1][j]==0 && mat[i][j-1]==0)
                ans[i][j] -= ans[i-1][j-1];
        }
    }
    in >> k;
    for(int i=1;i<=k;++i) {
        int x,y;
        in >> x >> y;
        out << ans[x][y] << "\n";
    }
    return 0;
}
<file_sep>#include <bits/stdc++.h>
using namespace std;
ifstream fin ("rover.in");
ofstream fout ("rover.out");

int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
int mat[505][505];
int isStable[505][505];
int score[505][505];
deque<pair<int, int> > bfsQueue;//more like bfsdeque haha I wanna kill myself
int n;
inline void makeBfsMat(int g) {
    while(!bfsQueue.empty())
        bfsQueue.pop_back();
    bfsQueue.push_back(make_pair(1, 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            score[i][j]=0;
            if (mat[i][j] < g) {
                isStable[i][j] = 0;
            } else
                isStable[i][j] = 1;
        }
    }
    score[1][1]=1;
}
bool inBounds (pair<int, int> a) {
    return a.first > 0 && a.first <=n && a.second > 0 && a.second <= n;
}
int checkBfs() {
    while(!bfsQueue.empty()) {
        auto top = bfsQueue.front();
        bfsQueue.pop_front();
        for(int i=0;i<4;++i) {
            auto it = make_pair(top.first+dx[i], top.second+dy[i]);
            if(score[it.first][it.second] || !inBounds(it))
                continue;
            score[it.first][it.second] = score[top.first][top.second] + !isStable[it.first][it.second];
            if(isStable[it.first][it.second] != 1)
                bfsQueue.push_back(it);
            else
                bfsQueue.push_front(it);
        }
    }
    return score[n][n]-1;
}

int main() {
    int cer, g;
    fin >> cer >> n;
    if (cer == 1) {
        fin >> g;
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            fin >> mat[i][j];
        }
    }
    if (cer == 1) {
        makeBfsMat(g);
        fout << checkBfs() << "\n";
        return 0;
    }
    int st = 1, dr = 10005;
    int ans=10000;
    while (st <= dr) {
        int mid = (st + dr) / 2;
        makeBfsMat(mid);
        if(checkBfs() > 0) {
            dr=mid-1;
        }else {
            ans=mid;
            st=mid+1;
        }
    }
    fout << ans << "\n";
    return 0;
}