#include using namespace std; #include using namespace atcoder; using ll = long long; using P = pair; #define fix(x) fixed << setprecision(x) #define asc(x) x, vector, greater #define rep(i, n) for(ll i = 0; i < n; ++i) #define all(x) (x).begin(),(x).end() templatebool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;} templatebool chmax(T&a, const T&b){if(a> n >> x; vector v(n), w(n); ll tot = 0; rep(i,n){ cin >> v[i] >> w[i]; if(w[i]<0){ w[i] *= -1; v[i] *= -1; x += w[i]; tot += v[i]; } } vector dp(x+1,-INFLL); vector cnt(x+1,0); dp[0] = 0; cnt[0] = 1; rep(i,n){ auto upd = dp; auto ucnt = cnt; rep(j,x+1-w[i]){ if(dp[j]==-INFLL) continue; if(chmax(upd[j+w[i]], dp[j]+v[i])) ucnt[j+w[i]] = 0; if(upd[j+w[i]]==dp[j]+v[i]) ucnt[j+w[i]] += cnt[j]; } dp = upd, cnt = ucnt; } ll p = 0; rep(i,x+1) chmax(p, dp[i]); mint ans = 0; rep(i,x+1) if(p==dp[i]) ans += cnt[i]; cout << p-tot << " " << ans.val() << '\n'; return 0; }