//#define _GLIBCXX_DEBUG 1
//#pragma GCC optimize ("03, unroll-seen")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll inf = 1e18, mod = 998244353;


signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    //ifstream cin ("input.txt");
    //ofstream cout ("output.txt");
    int n;
    cin >> n;
    vector<pair<ll, ll> > a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i].first >> a[i].second;
    int x;
    cin >> x;
    if (x == 2) {
        cout << -1;
        return 0;
    }
    vector<vector<ll> > dp(n, vector<ll>(n, LLONG_MIN));
    dp[0][0] = a[0].first;
    ll ans = dp[0][0];
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j <= i; j++) {
            for (int k = j; k <= i+1; k++)
                dp[i+1][k] = max(dp[i+1][k], dp[i][j]+a[i+1].first+a[i+1].second*k);
        }
    }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
}
