problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03147
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main() {
int n, l;
int i = 0;
cin >> n;
vector<int> table(n + 1);
for (int i = 0; i < n; i++) {
cin >> table[i];
}
table[n + 1] = 0;
int max = 0, count = 0;
for (i = 0; i < n; ++i) {
if (max < table[i])
max = table[i];
}
while (max != 0) {
for (i = 0; i < n; ++i) {
if (max == table[i]) {
if (table[i] == table[i + 1])
table[i]--;
else {
table[i]--;
count++;
}
}
}
max--;
}
cout << count << endl;
return 0;
}
|
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main() {
int n, l;
int i = 0;
cin >> n;
int table[n + 1];
for (int i = 0; i < n; i++) {
cin >> table[i];
}
table[n + 1] = 0;
int max = 0, count = 0;
for (i = 0; i < n; ++i) {
if (max < table[i])
max = table[i];
}
while (max != 0) {
for (i = 0; i < n; ++i) {
if (max == table[i]) {
if (table[i] == table[i + 1])
table[i]--;
else {
table[i]--;
count++;
}
}
}
max--;
}
cout << count << endl;
return 0;
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define ALL(x) (x).begin(), (x).end()
#define Sort(a) sort(ALL(a))
#define Rev(a) reverse(ALL(a))
using namespace std;
// using ll=long long;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
const long double PI = 3.14159265358979323846;
template <class T> bool contain(const std::string &s, const T &v) {
return s.find(v) != std::string::npos;
}
int main() {
ll n;
cin >> n;
vector<ll> h(n + 1);
rep(i, n) { cin >> h[i]; }
h[n] = 0;
ll ans = 10000000000;
ll i = 0;
ll j = 0;
while (h[j] == 0)
j++;
i = j;
while (j < n) {
// cout<<j<<h[i]<<endl;
if (i > n - 1 || h[i] == 0) {
// cout<<"#"<<ans<<" "<<i<<" "<<j<<endl;
while (h[j] == 0)
j++;
i = j;
ans++;
}
if (h[i] > 0) {
h[i]--;
// cout<<i<<endl;
i++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define ALL(x) (x).begin(), (x).end()
#define Sort(a) sort(ALL(a))
#define Rev(a) reverse(ALL(a))
using namespace std;
// using ll=long long;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
const long double PI = 3.14159265358979323846;
template <class T> bool contain(const std::string &s, const T &v) {
return s.find(v) != std::string::npos;
}
int main() {
ll n;
cin >> n;
vector<ll> h(n + 1);
rep(i, n) { cin >> h[i]; }
h[n] = 100000000000;
ll ans = 0;
ll i = 0;
ll j = 0;
while (h[j] == 0)
j++;
i = j;
while (j < n) {
// cout<<j<<h[i]<<endl;
if (i > n - 1 || h[i] == 0) {
// cout<<"#"<<ans<<" "<<i<<" "<<j<<endl;
while (h[j] == 0)
j++;
i = j;
ans++;
}
if (h[i] > 0) {
h[i]--;
// cout<<i<<endl;
i++;
}
}
cout << ans << endl;
}
|
replace
| 39 | 41 | 39 | 41 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define R(i, c, n) for (int i = c; i < n; i++)
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DUMP(a) \
REP(d, a.size()) { \
cout << a[d]; \
if (d != a.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) \
sort(ALL(v)); \
v.erase(unique(ALL(v)), v.end());
#define pb push_back
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> flowers(n, 0);
r(i, n) cin >> flowers[i];
n++;
int begin = 0;
int count = 0;
bool flag = false;
while (true) {
flag = false;
for (int i = begin; i < n; i++) {
if (flowers[i] > 0) {
flowers[i]--;
flag = true;
} else {
if (flowers[begin] == 0)
begin++;
break;
}
}
if (flag)
count++;
if (begin >= n - 1)
break;
}
cout << count << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define R(i, c, n) for (int i = c; i < n; i++)
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DUMP(a) \
REP(d, a.size()) { \
cout << a[d]; \
if (d != a.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) \
sort(ALL(v)); \
v.erase(unique(ALL(v)), v.end());
#define pb push_back
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> flowers(n + 1, 0);
r(i, n) cin >> flowers[i];
n++;
int begin = 0;
int count = 0;
bool flag = false;
while (true) {
flag = false;
for (int i = begin; i < n; i++) {
if (flowers[i] > 0) {
flowers[i]--;
flag = true;
} else {
if (flowers[begin] == 0)
begin++;
break;
}
}
if (flag)
count++;
if (begin >= n - 1)
break;
}
cout << count << endl;
return 0;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p03147
|
C++
|
Runtime Error
|
// g++でビルド
#include "bits/stdc++.h"
using namespace std;
vector<int> watering(vector<int> vec, int s) {
while (vec[s] != 0) {
vec[s]--;
s++;
}
return vec;
}
int main() {
int N;
cin >> N;
vector<int> flowers;
int result = 0;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
flowers.push_back(a);
}
for (int i = 0; i < N; i++) {
while (flowers[i] != 0) {
flowers = watering(flowers, i);
result++;
}
}
cout << result << endl;
return 0;
}
|
// g++でビルド
#include "bits/stdc++.h"
using namespace std;
vector<int> watering(vector<int> vec, int s) {
while (vec[s] != 0) {
vec[s]--;
s++;
if (vec.size() == s) {
break;
}
}
return vec;
}
int main() {
int N;
cin >> N;
vector<int> flowers;
int result = 0;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
flowers.push_back(a);
}
for (int i = 0; i < N; i++) {
while (flowers[i] != 0) {
flowers = watering(flowers, i);
result++;
}
}
cout << result << endl;
return 0;
}
|
insert
| 10 | 10 | 10 | 13 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n, j) for (int i = n; j <= i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;
typedef pair<ll, ll> P;
int main() {
int N;
cin >> N;
vector<int> H(N);
REP(i, 0, N) { cin >> H[i]; }
int give_water = 0, cnt, idx;
while (true) {
cnt = idx = 0;
while (idx < N) {
cnt += H[idx];
if (H[idx]) {
give_water++;
while (H[idx]) {
H[idx]--;
idx++;
}
}
idx++;
}
if (cnt == 0) {
break;
}
}
print(give_water)
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n, j) for (int i = n; j <= i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;
typedef pair<ll, ll> P;
int main() {
int N;
cin >> N;
vector<int> H(N);
REP(i, 0, N) { cin >> H[i]; }
int give_water = 0, cnt, idx;
while (true) {
cnt = idx = 0;
while (idx < N) {
cnt += H[idx];
if (H[idx]) {
give_water++;
while (idx < N && H[idx]) {
H[idx]--;
idx++;
}
}
idx++;
}
if (cnt == 0) {
break;
}
}
print(give_water)
}
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p03147
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif
const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> h(N);
for (int i = 0; i < N; i++)
cin >> h[i];
int ans = 0, st = 0, minh = 0;
while (minh != INF) {
st = 0;
minh = h[0] > 0 ? h[0] : INF;
if (N == 0) {
ans = h[0];
break;
}
for (int i = 1; i < N; i++) {
minh = (h[i] > 0) && h[i] < minh ? h[i] : minh;
if (h[i - 1] > 0 && h[i] <= 0 || (i == N - 1) && h[N - 1] > 0) {
for (int j = st; j <= i; j++)
h[j] -= minh;
ans += minh;
st = i;
minh = INF - 1;
}
}
}
cout << ans << "\n";
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif
const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> h(N);
for (int i = 0; i < N; i++)
cin >> h[i];
int ans = 0, st = 0, minh = 0;
while (minh != INF) {
st = 0;
minh = h[0] > 0 ? h[0] : INF;
if (N == 1) {
ans = h[0];
break;
}
for (int i = 1; i < N; i++) {
minh = (h[i] > 0) && h[i] < minh ? h[i] : minh;
if (h[i - 1] > 0 && h[i] <= 0 || (i == N - 1) && h[N - 1] > 0) {
for (int j = st; j <= i; j++)
h[j] -= minh;
ans += minh;
st = i;
minh = INF - 1;
}
}
}
cout << ans << "\n";
return 0;
}
|
replace
| 38 | 39 | 38 | 39 |
TLE
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
auto h = vector<int>(n, 0);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
long long ans = 0;
for (int l = 0; l < n; l++) {
while (h[l] > 0) {
int r = l;
while (h[r] > 0) {
r++;
}
r--;
auto _min = *min_element(h.begin() + l, h.begin() + r);
for (int i = l; i <= r; i++) {
h[i] -= _min;
}
ans += _min;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
auto h = vector<int>(n + 1, 0);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
long long ans = 0;
for (int l = 0; l < n; l++) {
while (h[l] > 0) {
int r = l;
while (h[r] > 0) {
r++;
}
r--;
auto _min = *min_element(h.begin() + l, h.begin() + r);
for (int i = l; i <= r; i++) {
h[i] -= _min;
}
ans += _min;
}
}
cout << ans;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
ll n, cur = -1;
cin >> n;
vi h(n);
rep(i, n) {
cin >> h[i];
if (cur == -1 && h[i] > 0)
cur = i;
}
ll ans = 0;
while (cur < n) {
if (h[cur] == 0) {
cur++;
continue;
}
ans++;
for (ll i = cur; i < n; i++) {
if (h[i] > 0)
h[i]--;
else
break;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
ll n, cur = -1;
cin >> n;
vi h(n);
rep(i, n) {
cin >> h[i];
if (cur == -1 && h[i] > 0)
cur = i;
}
ll ans = 0;
if (cur == -1) {
cout << 0 << endl;
return 0;
}
while (cur < n) {
if (h[cur] == 0) {
cur++;
continue;
}
ans++;
for (ll i = cur; i < n; i++) {
if (h[i] > 0)
h[i]--;
else
break;
}
}
cout << ans << endl;
}
|
insert
| 46 | 46 | 46 | 50 |
0
| |
p03147
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 10000000000000009
// #define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl
int h[114];
int Ans = 0;
int kukan(int l, int r) {
if (l == r) {
Ans += h[l];
h[l] = 0;
return 0;
}
if (l > r) {
return 0;
}
int e = INF;
for (int i = l; i <= r; i++) {
e = min(e, h[i]);
}
Ans += e;
for (int i = l; i <= r; i++) {
h[i] -= e;
}
for (int i = l; i <= r; i++) {
if (h[i] == 0) {
kukan(l, i - 1);
kukan(i + 1, r);
}
}
return 0;
}
signed main() {
int N;
cin >> N;
REP(i, N) { cin >> h[i]; }
kukan(0, N - 1);
cout << Ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 10000000000000009
// #define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl
int h[114];
int Ans = 0;
int kukan(int l, int r) {
if (l == r) {
Ans += h[l];
h[l] = 0;
return 0;
}
if (l > r) {
return 0;
}
int e = INF;
for (int i = l; i <= r; i++) {
e = min(e, h[i]);
}
Ans += e;
for (int i = l; i <= r; i++) {
h[i] -= e;
}
for (int i = l; i <= r; i++) {
if (h[i] == 0) {
kukan(l, i - 1);
kukan(i + 1, r);
break;
}
}
return 0;
}
signed main() {
int N;
cin >> N;
REP(i, N) { cin >> h[i]; }
kukan(0, N - 1);
cout << Ans << endl;
return 0;
}
|
insert
| 43 | 43 | 43 | 44 |
TLE
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;
int main() {
ll N;
cin >> N;
vector<int> h(N);
rep(i, N) cin >> h[i]; // input
ll ans = 0;
rep(i, N) {
while (h[i] > 0) {
int j = 0, x = 100;
while (h[i + j] > 0) {
if (i + j >= N)
break;
x = min(x, h[i + j]);
j++;
}
for (int k = i; k <= i + j; k++) {
h[k] -= x;
}
ans += x;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;
int main() {
ll N;
cin >> N;
vector<int> h(N + 2);
rep(i, N) cin >> h[i]; // input
ll ans = 0;
rep(i, N) {
while (h[i] > 0) {
int j = 0, x = 100;
while (h[i + j] > 0) {
if (i + j >= N)
break;
x = min(x, h[i + j]);
j++;
}
for (int k = i; k <= i + j; k++) {
h[k] -= x;
}
ans += x;
}
}
cout << ans << endl;
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++)
cin >> h[i];
int ans = 0;
while (1) {
int mx = 0;
for (int i = 0; i < n; i++) {
if (mx < h[i])
mx = h[i];
}
if (mx == 0)
break;
int i = 0;
while (i < n) {
if (h[i] == 0)
i++;
else {
ans++;
while (h[i] != 0) {
h[i]--;
i++;
}
}
}
}
cout << ans << endl;
}
|
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++)
cin >> h[i];
int ans = 0;
while (1) {
int mx = 0;
for (int i = 0; i < n; i++) {
if (mx < h[i])
mx = h[i];
}
if (mx == 0)
break;
int i = 0;
while (i < n) {
if (h[i] == 0)
i++;
else {
ans++;
while (h[i] != 0 && i < n) {
h[i]--;
i++;
}
}
}
}
cout << ans << endl;
}
|
replace
| 37 | 38 | 37 | 38 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
int h[200] = {0}, l = 1, cnt = 0;
void mizuyari(int l, int r) {
if (l > r)
return;
for (int i = l; i <= r; i++) {
h[i]--;
}
cnt++;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> h[i];
while (true) {
for (int i = 0; h[i] == 0; i++)
l = i + 1;
for (int i = 1; i <= n + 1; i++) {
if (h[i] == 0) {
mizuyari(l, i - 1);
for (int j = i; h[j] == 0; j++) {
l = j + 1;
}
}
}
for (int i = 1; i <= n; i++) {
if (h[i] != 0)
break;
if (i == n && h[i] == 0) {
cout << cnt << endl;
return 0;
}
}
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
int l = 1, cnt = 0;
vector<int> h(300);
void mizuyari(int l, int r) {
if (l > r)
return;
for (int i = l; i <= r; i++) {
h[i]--;
}
cnt++;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> h[i];
while (true) {
for (int i = 0; h[i] == 0; i++)
l = i + 1;
for (int i = 1; i <= n + 1; i++) {
if (h[i] == 0) {
mizuyari(l, i - 1);
for (int j = i; h[j] == 0; j++) {
l = j + 1;
}
}
}
for (int i = 1; i <= n; i++) {
if (h[i] != 0)
break;
if (i == n && h[i] == 0) {
cout << cnt << endl;
return 0;
}
}
}
return 0;
}
|
replace
| 3 | 4 | 3 | 5 |
0
| |
p03147
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> line(N);
for (int i = 0; i < N; i++)
cin >> line[i];
int i = 0;
int ans = 0;
while (1) {
if (i == N - 1 && line[i] == 0)
break;
if (line[i] == 0) {
i++;
continue;
}
ans++;
int j = 0;
while (1) {
if (line[i + j] == 0)
break;
line[i + j]--;
j++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> line(N);
for (int i = 0; i < N; i++)
cin >> line[i];
int i = 0;
int ans = 0;
while (1) {
if (i == N - 1 && line[i] == 0)
break;
if (line[i] == 0) {
i++;
continue;
}
ans++;
int j = 0;
while (1) {
if (line[i + j] == 0)
break;
line[i + j]--;
j++;
if (j == N)
break;
}
}
cout << ans << endl;
}
|
insert
| 26 | 26 | 26 | 28 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define INF 1000000000
// #define DEBUG
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int N = 1e5 + 5;
priority_queue<pair<int, int>> all;
priority_queue<pair<int, int>> rep;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
sol;
int vis[N];
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k, uniq = 0;
cin >> n >> k;
lp(i, n) {
pair<int, int> in;
cin >> in.second >> in.first;
all.push(in);
}
lp(i, n) {
pair<int, int> in = all.top();
all.pop();
if (vis[in.second] > 0 || uniq == k)
rep.push(in);
else {
sol.push(in);
++vis[in.second];
++uniq;
}
}
lpi(i, uniq, k) {
sol.push(rep.top());
++vis[rep.top().second];
rep.pop();
}
while (true) {
ll op1 = 0ll + (uniq - 1) * (uniq - 1) + rep.top().first;
ll op2 = 0ll + (uniq) * (uniq) + sol.top().first;
if (op1 > op2) {
sol.pop();
sol.push(rep.top());
rep.pop();
--uniq;
} else
break;
}
ll ans = 0;
ans = 1ll * uniq * uniq;
while (!sol.empty()) {
ans += 0ll + sol.top().first;
sol.pop();
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define INF 1000000000
// #define DEBUG
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int N = 1e5 + 5;
priority_queue<pair<int, int>> all;
priority_queue<pair<int, int>> rep;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
sol;
int vis[N];
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k, uniq = 0;
cin >> n >> k;
lp(i, n) {
pair<int, int> in;
cin >> in.second >> in.first;
all.push(in);
}
lp(i, n) {
pair<int, int> in = all.top();
all.pop();
if (vis[in.second] > 0 || uniq == k)
rep.push(in);
else {
sol.push(in);
++vis[in.second];
++uniq;
}
}
lpi(i, uniq, k) {
sol.push(rep.top());
++vis[rep.top().second];
rep.pop();
}
while (!rep.empty()) {
ll op1 = 0ll + (uniq - 1) * (uniq - 1) + rep.top().first;
ll op2 = 0ll + (uniq) * (uniq) + sol.top().first;
if (op1 > op2) {
sol.pop();
sol.push(rep.top());
rep.pop();
--uniq;
} else
break;
}
ll ans = 0;
ans = 1ll * uniq * uniq;
while (!sol.empty()) {
ans += 0ll + sol.top().first;
sol.pop();
}
cout << ans;
return 0;
}
|
replace
| 54 | 55 | 54 | 55 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N, K;
cin >> N >> K;
vector<pair<ll, ll>> vec;
for (int i = 1; i <= N; i++) {
int a, b;
cin >> a >> b;
vec.push_back(make_pair(b, a));
}
sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());
priority_queue<ll> pq;
set<ll> s;
ll sum = 0;
for (int i = 0; i < K; i++) {
sum += vec[i].first;
if (s.count(vec[i].second)) {
pq.push(-vec[i].first);
} else {
s.insert(vec[i].second);
}
}
ll ans = sum + s.size() * s.size();
for (int i = K; i < N; i++) {
if (s.count(vec[i].second)) {
continue;
}
sum += pq.top();
pq.pop();
sum += vec[i].first;
s.insert(vec[i].second);
ll tmp = sum + s.size() * s.size();
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N, K;
cin >> N >> K;
vector<pair<ll, ll>> vec;
for (int i = 1; i <= N; i++) {
int a, b;
cin >> a >> b;
vec.push_back(make_pair(b, a));
}
sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());
priority_queue<ll> pq;
set<ll> s;
ll sum = 0;
for (int i = 0; i < K; i++) {
sum += vec[i].first;
if (s.count(vec[i].second)) {
pq.push(-vec[i].first);
} else {
s.insert(vec[i].second);
}
}
ll ans = sum + s.size() * s.size();
for (int i = K; i < N && !pq.empty(); i++) {
if (s.count(vec[i].second)) {
continue;
}
sum += pq.top();
pq.pop();
sum += vec[i].first;
s.insert(vec[i].second);
ll tmp = sum + s.size() * s.size();
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
|
replace
| 28 | 29 | 28 | 29 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
const long long int mod = 1000000007, INF = 50000000000000;
typedef pair<long long int, long long int> P;
map<long long int, long long int> mp;
priority_queue<P, vector<P>, greater<P>> mazuisushi;
priority_queue<P> umaisushi;
cout << fixed << setprecision(10);
string str;
long long int n, k, t, d, a, b, now = -1, tmp = 0, typ = 0;
long long int res = 0, ng[200005] = {};
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> t >> d;
umaisushi.push({d, t});
}
for (int i = 0; i < k; i++) {
if (ng[umaisushi.top().second] == 0) {
ng[umaisushi.top().second] = 1;
typ++;
tmp += umaisushi.top().first;
} else {
mazuisushi.push(umaisushi.top());
tmp += umaisushi.top().first;
}
umaisushi.pop();
}
res = tmp + typ * typ;
while (!umaisushi.empty()) {
if (ng[umaisushi.top().second] == 0) {
ng[umaisushi.top().second] = 1;
typ++;
tmp += umaisushi.top().first;
tmp -= mazuisushi.top().first;
mazuisushi.pop();
res = max(res, tmp + typ * typ);
}
umaisushi.pop();
}
cout << res;
}
|
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
const long long int mod = 1000000007, INF = 50000000000000;
typedef pair<long long int, long long int> P;
map<long long int, long long int> mp;
priority_queue<P, vector<P>, greater<P>> mazuisushi;
priority_queue<P> umaisushi;
cout << fixed << setprecision(10);
string str;
long long int n, k, t, d, a, b, now = -1, tmp = 0, typ = 0;
long long int res = 0, ng[200005] = {};
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> t >> d;
umaisushi.push({d, t});
}
for (int i = 0; i < k; i++) {
if (ng[umaisushi.top().second] == 0) {
ng[umaisushi.top().second] = 1;
typ++;
tmp += umaisushi.top().first;
} else {
mazuisushi.push(umaisushi.top());
tmp += umaisushi.top().first;
}
umaisushi.pop();
}
res = tmp + typ * typ;
while (!umaisushi.empty() && !mazuisushi.empty()) {
if (ng[umaisushi.top().second] == 0) {
ng[umaisushi.top().second] = 1;
typ++;
tmp += umaisushi.top().first;
tmp -= mazuisushi.top().first;
mazuisushi.pop();
res = max(res, tmp + typ * typ);
}
umaisushi.pop();
}
cout << res;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); (i)++)
#define frep(i, m, n) for (ll(i) = (m); (i) <= (ll)(n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) > -1; (i)--)
#define frrep(i, m, n) for (ll(i) = (n); (i) > (ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()
const ll INF = 100100100100100100;
const ll MOD = 1000000007;
// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);
int main() {
ll n, k;
cin >> n >> k;
vector<ll> f(n + 1, -1 * INF);
vector<P> sushi;
rep(i, n) {
ll t, d;
cin >> t >> d;
sushi.push_back(make_pair(d, t)); // 美味しさ先にする
}
sort(ALL(sushi), greater<P>());
// 初期状態を作る
// pqmaxは絶対解に必要, pqminは2番手以降なので抜いてもOK
priority_queue<ll, vector<ll>, greater<ll>> pqmax, pqmin;
ll sum = 0;
set<ll> var; // どの寿司が含まれてるか
rep(i, k) {
sum += sushi[i].first;
// 始めて来た寿司
if (!var.count(sushi[i].second)) {
var.insert(sushi[i].second);
pqmax.push(sushi[i].first);
}
// 2番手以降の寿司
else {
pqmin.push(sushi[i].first);
}
}
f[var.size()] = sum;
frep(i, k, n - 1) {
// 始めて来た寿司に対してのみ操作をする
if (!var.count(sushi[i].second)) {
// 最小値取り出し
ll tp = pqmin.top();
pqmin.pop();
sum -= tp;
// 新種を挿入
sum += sushi[i].first;
pqmax.push(sushi[i].first);
var.insert(sushi[i].second);
f[var.size()] = sum;
}
}
ll ans = -1 * INF;
frep(i, 1, n) {
if (f[i] + i * i > ans)
ans = f[i] + i * i;
}
cout << ans << endl;
return 0;
}
ll my_abs(ll a) {
if (a >= 0)
return a;
else
return -1 * a;
}
ll a_n(ll a, ll n) {
if (n == 0)
return 1;
ll ret = a, count = 1;
while (count * 2 < n) {
ret *= ret;
count *= 2;
}
if (count == n)
return ret;
else
return (ret * a_n(a, n - count));
}
ll my_gcd(ll a, ll b) {
if (b == 0)
return a;
return my_gcd(b, a % b);
}
ll inv(ll a) { return a_n(a, MOD - 2); }
ll madd(ll a, ll b, ll c) {
ll ret = (a + b) % MOD;
return (ret + c) % MOD;
}
ll msub(ll a, ll b) {
if (a < b)
return (a - b + MOD) % MOD;
else
return (a - b) % MOD;
}
ll mtime(ll a, ll b, ll c) {
ll ret = (a * b) % MOD;
return (ret * c) % MOD;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); (i)++)
#define frep(i, m, n) for (ll(i) = (m); (i) <= (ll)(n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) > -1; (i)--)
#define frrep(i, m, n) for (ll(i) = (n); (i) > (ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()
const ll INF = 100100100100100100;
const ll MOD = 1000000007;
// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);
int main() {
ll n, k;
cin >> n >> k;
vector<ll> f(n + 1, -1 * INF);
vector<P> sushi;
rep(i, n) {
ll t, d;
cin >> t >> d;
sushi.push_back(make_pair(d, t)); // 美味しさ先にする
}
sort(ALL(sushi), greater<P>());
// 初期状態を作る
// pqmaxは絶対解に必要, pqminは2番手以降なので抜いてもOK
priority_queue<ll, vector<ll>, greater<ll>> pqmax, pqmin;
ll sum = 0;
set<ll> var; // どの寿司が含まれてるか
rep(i, k) {
sum += sushi[i].first;
// 始めて来た寿司
if (!var.count(sushi[i].second)) {
var.insert(sushi[i].second);
pqmax.push(sushi[i].first);
}
// 2番手以降の寿司
else {
pqmin.push(sushi[i].first);
}
}
f[var.size()] = sum;
frep(i, k, n - 1) {
// 始めて来た寿司に対してのみ操作をする
if (!var.count(sushi[i].second) && pqmin.size() != 0) {
// 最小値取り出し
ll tp = pqmin.top();
pqmin.pop();
sum -= tp;
// 新種を挿入
sum += sushi[i].first;
pqmax.push(sushi[i].first);
var.insert(sushi[i].second);
f[var.size()] = sum;
}
}
ll ans = -1 * INF;
frep(i, 1, n) {
if (f[i] + i * i > ans)
ans = f[i] + i * i;
}
cout << ans << endl;
return 0;
}
ll my_abs(ll a) {
if (a >= 0)
return a;
else
return -1 * a;
}
ll a_n(ll a, ll n) {
if (n == 0)
return 1;
ll ret = a, count = 1;
while (count * 2 < n) {
ret *= ret;
count *= 2;
}
if (count == n)
return ret;
else
return (ret * a_n(a, n - count));
}
ll my_gcd(ll a, ll b) {
if (b == 0)
return a;
return my_gcd(b, a % b);
}
ll inv(ll a) { return a_n(a, MOD - 2); }
ll madd(ll a, ll b, ll c) {
ll ret = (a + b) % MOD;
return (ret + c) % MOD;
}
ll msub(ll a, ll b) {
if (a < b)
return (a - b + MOD) % MOD;
else
return (a - b) % MOD;
}
ll mtime(ll a, ll b, ll c) {
ll ret = (a * b) % MOD;
return (ret * c) % MOD;
}
|
replace
| 63 | 64 | 63 | 64 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
priority_queue<P> pq1;
rep(i, n) {
int t, d;
cin >> t >> d;
pq1.emplace(d, t);
}
ll now = 0;
set<int> st;
priority_queue<int, vector<int>, greater<int>> pq2;
rep(i, k) {
P p = pq1.top();
pq1.pop();
if (st.count(p.se)) {
pq2.push(p.fi);
} else {
st.insert(p.se);
}
now += p.fi;
}
ll v = st.size();
ll ans = now + v * v;
while (!pq1.empty()) {
P p = pq1.top();
pq1.pop();
if (st.count(p.se))
continue;
now -= pq2.top();
pq2.pop();
st.insert(p.se);
now += p.fi;
v++;
chmax(ans, now + v * v);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
priority_queue<P> pq1;
rep(i, n) {
int t, d;
cin >> t >> d;
pq1.emplace(d, t);
}
ll now = 0;
set<int> st;
priority_queue<int, vector<int>, greater<int>> pq2;
rep(i, k) {
P p = pq1.top();
pq1.pop();
if (st.count(p.se)) {
pq2.push(p.fi);
} else {
st.insert(p.se);
}
now += p.fi;
}
ll v = st.size();
ll ans = now + v * v;
while (!pq1.empty() && !pq2.empty()) {
P p = pq1.top();
pq1.pop();
if (st.count(p.se))
continue;
now -= pq2.top();
pq2.pop();
st.insert(p.se);
now += p.fi;
v++;
chmax(ans, now + v * v);
}
cout << ans << endl;
}
|
replace
| 66 | 67 | 66 | 67 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> Pii;
const ll mod = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<pair<int, ll>> td(n);
for (auto &x : td)
cin >> x.first >> x.second;
vector<vector<ll>> c(n);
for (int i = 0; i < n; i++) {
c[td[i].first - 1].push_back(td[i].second);
}
for (int i = 0; i < n; i++)
sort(c[i].rbegin(), c[i].rend());
sort(c.rbegin(), c.rend(), [](auto &a, auto &b) {
if (a.size() == 0)
return true;
else if (b.size() == 0)
return false;
else
return a[0] < b[0];
});
ll ans = 0;
ll varCount = 0;
priority_queue<ll, vector<ll>, greater<ll>> noPenalty, penalty;
for (int i = 0; i < n; i++) {
if (c[i].size() == 0)
continue;
varCount++;
penalty.push(c[i][0]);
for (int j = 1; j < (int)c[i].size(); j++)
noPenalty.push(c[i][j]);
for (auto &x : c[i])
ans += x;
}
ans += varCount * varCount;
for (int i = n; i > k; i--) {
ll p, np;
p = penalty.top();
if (!noPenalty.empty())
np = noPenalty.top();
else
np = 9e18;
if (p + 2 * varCount - 1 <= np) {
penalty.pop();
ans -= p + 2 * varCount - 1;
varCount--;
} else {
noPenalty.pop();
ans -= np;
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> Pii;
const ll mod = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<pair<int, ll>> td(n);
for (auto &x : td)
cin >> x.first >> x.second;
vector<vector<ll>> c(n);
for (int i = 0; i < n; i++) {
c[td[i].first - 1].push_back(td[i].second);
}
for (int i = 0; i < n; i++)
sort(c[i].rbegin(), c[i].rend());
ll ans = 0;
ll varCount = 0;
priority_queue<ll, vector<ll>, greater<ll>> noPenalty, penalty;
for (int i = 0; i < n; i++) {
if (c[i].size() == 0)
continue;
varCount++;
penalty.push(c[i][0]);
for (int j = 1; j < (int)c[i].size(); j++)
noPenalty.push(c[i][j]);
for (auto &x : c[i])
ans += x;
}
ans += varCount * varCount;
for (int i = n; i > k; i--) {
ll p, np;
p = penalty.top();
if (!noPenalty.empty())
np = noPenalty.top();
else
np = 9e18;
if (p + 2 * varCount - 1 <= np) {
penalty.pop();
ans -= p + 2 * varCount - 1;
varCount--;
} else {
noPenalty.pop();
ans -= np;
}
}
cout << ans << endl;
return 0;
}
|
delete
| 32 | 40 | 32 | 32 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
/* ---------- Namespace ---------- */
using namespace std;
/* ---------- Type Abbreviation ---------- */
using ll = long long;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using GPQ = priority_queue<T, vector<T>, greater<T>>;
#define mp make_pair
#define mt make_tuple
/* ----------- debug ---------- */
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> p);
template <class T> ostream &operator<<(ostream &os, vector<T> v) {
os << "[";
for (auto vv : v)
os << vv << ",";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> v) {
os << "[";
for (auto vv : v)
os << vv << ",";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> p) {
return os << "(" << p.first << "," << p.second << ")";
}
/* ---------- Constants ---------- */
// const ll MOD = 1000000007;
// const ll MOD = 998244353;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const double PI = acos(-1);
// const double EPS = 1e-10;
// mt19937 mert(LL(time(0)));
/* ---------- Short Functions ---------- */
template <typename T> inline T sq(T a) { return a * a; }
template <typename T> T gcd(T a, T b) {
if (a > b)
return gcd(b, a);
return a == 0 ? b : gcd(b % a, a);
}
template <typename T, typename U> T mypow(T b, U n) {
if (n == 0)
return 1;
if (n == 1)
return b /* % MOD */;
if (n % 2 == 0) {
return mypow(b * b /* % MOD */, n / 2);
} else {
return mypow(b, n - 1) * b /* % MOD */;
}
}
ll pcnt(ll b) { return __builtin_popcountll(b); }
template <typename T> T iceil(T n, T d) { return (n + d - 1) / d; }
/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int main() {
int N, K;
cin >> N >> K;
vector<ll> kind[N];
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
--t;
kind[t].push_back(d);
}
vector<ll> best, res;
for (int i = 0; i < N; ++i) {
if (!kind[i].empty()) {
sort(kind[i].begin(), kind[i].end());
best.push_back(kind[i].back());
kind[i].pop_back();
while (!kind[i].empty()) {
res.push_back(kind[i].back());
kind[i].pop_back();
}
}
}
sort(best.rbegin(), best.rend());
sort(res.rbegin(), res.rend());
vector<ll> bsum = {0};
for (int i = 0; i < best.size(); ++i) {
bsum.push_back(bsum.back() + best[i]);
}
vector<ll> rsum = {0};
for (int i = 0; i < res.size(); ++i) {
rsum.push_back(rsum.back() + res[i]);
}
ll ans = 0;
for (ll x = 1; x <= min(K, (int)best.size()); ++x) {
ans = max(ans, bsum[x] + rsum[K - x] + x * x);
}
cout << ans << endl;
return 0;
}
|
/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
/* ---------- Namespace ---------- */
using namespace std;
/* ---------- Type Abbreviation ---------- */
using ll = long long;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using GPQ = priority_queue<T, vector<T>, greater<T>>;
#define mp make_pair
#define mt make_tuple
/* ----------- debug ---------- */
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> p);
template <class T> ostream &operator<<(ostream &os, vector<T> v) {
os << "[";
for (auto vv : v)
os << vv << ",";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> v) {
os << "[";
for (auto vv : v)
os << vv << ",";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> p) {
return os << "(" << p.first << "," << p.second << ")";
}
/* ---------- Constants ---------- */
// const ll MOD = 1000000007;
// const ll MOD = 998244353;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const double PI = acos(-1);
// const double EPS = 1e-10;
// mt19937 mert(LL(time(0)));
/* ---------- Short Functions ---------- */
template <typename T> inline T sq(T a) { return a * a; }
template <typename T> T gcd(T a, T b) {
if (a > b)
return gcd(b, a);
return a == 0 ? b : gcd(b % a, a);
}
template <typename T, typename U> T mypow(T b, U n) {
if (n == 0)
return 1;
if (n == 1)
return b /* % MOD */;
if (n % 2 == 0) {
return mypow(b * b /* % MOD */, n / 2);
} else {
return mypow(b, n - 1) * b /* % MOD */;
}
}
ll pcnt(ll b) { return __builtin_popcountll(b); }
template <typename T> T iceil(T n, T d) { return (n + d - 1) / d; }
/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int main() {
int N, K;
cin >> N >> K;
vector<ll> kind[N];
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
--t;
kind[t].push_back(d);
}
vector<ll> best, res;
for (int i = 0; i < N; ++i) {
if (!kind[i].empty()) {
sort(kind[i].begin(), kind[i].end());
best.push_back(kind[i].back());
kind[i].pop_back();
while (!kind[i].empty()) {
res.push_back(kind[i].back());
kind[i].pop_back();
}
}
}
sort(best.rbegin(), best.rend());
sort(res.rbegin(), res.rend());
vector<ll> bsum = {0};
for (int i = 0; i < best.size(); ++i) {
bsum.push_back(bsum.back() + best[i]);
}
vector<ll> rsum = {0};
for (int i = 0; i < res.size(); ++i) {
rsum.push_back(rsum.back() + res[i]);
}
while (rsum.size() < K)
rsum.push_back(0);
ll ans = 0;
for (ll x = 1; x <= min(K, (int)best.size()); ++x) {
ans = max(ans, bsum[x] + rsum[K - x] + x * x);
}
cout << ans << endl;
return 0;
}
|
insert
| 139 | 139 | 139 | 141 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b) \
for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
ll ugauss(ll a, ll b) {
if (!a)
return 0;
if (a > 0 ^ b > 0)
return a / b;
else
return (a + (a > 0 ? -1 : 1)) / b + 1;
}
ll lgauss(ll a, ll b) {
if (!a)
return 0;
if (a > 0 ^ b > 0)
return (a + (a > 0 ? -1 : 1)) / b - 1;
else
return a / b;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
o << "(" << v.first << ", " << v.second << ")";
return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
using s = int[];
(void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
-> basic_ostream<Ch, Tr> & {
os << "(";
print_tuple(os, t, gen_seq<sizeof...(Args)>());
return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
rep(i, v.size()) {
rep(j, v[i].size()) o << v[i][j] << " ";
o << endl;
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const unordered_set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it;
o << "]";
return o;
}
vector<int> range(const int x, const int y) {
vector<int> v(y - x + 1);
iota(v.begin(), v.end(), x);
return v;
}
template <typename T> istream &operator>>(istream &i, vector<T> &o) {
rep(j, o.size()) i >> o[j];
return i;
}
template <typename T, typename S, typename U>
ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.front();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> unordered_map<T, ll> counter(vector<T> vec) {
unordered_map<T, ll> ret;
for (auto &&x : vec)
ret[x]++;
return ret;
};
void vizGraph(vvll &g, int mode = 0, string filename = "out.png") {
ofstream ofs("./out.dot");
ofs << "digraph graph_name {" << endl;
set<P> memo;
rep(i, g.size()) rep(j, g[i].size()) {
if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i))))
continue;
memo.insert(P(i, g[i][j]));
ofs << " " << i << " -> " << g[i][j]
<< (mode ? " [arrowhead = none]" : "") << endl;
}
ofs << "}" << endl;
ofs.close();
system(((string) "dot -T png out.dot >" + filename).c_str());
}
struct timeval start;
double sec() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec - start.tv_sec) + (tv.tv_usec - start.tv_usec) * 1e-6;
}
size_t random_seed;
struct init_ {
init_() {
ios::sync_with_stdio(false);
cin.tie(0);
gettimeofday(&start, NULL);
struct timeval myTime;
struct tm *time_st;
gettimeofday(&myTime, NULL);
time_st = localtime(&myTime.tv_sec);
srand(myTime.tv_usec);
random_seed = RAND_MAX / 2 + rand() / 2;
}
} init__;
#define ldout fixed << setprecision(40)
#define EPS (double)1e-14
#define INF (ll)1e18
#define mo (ll)(1e9 + 7)
int main(void) {
ll n, k;
cin >> n >> k;
vvll f(n);
rep(i, n) {
ll a, b;
cin >> a >> b;
f[a].pb(b);
}
rep(i, n) sort(all(f[i]), greater<ll>());
vll d, e;
rep(i, n) rep(j, f[i].size())(j ? d : e).pb(f[i][j]);
sort(all(d), greater<ll>());
sort(all(e), greater<ll>());
vll ds(d.size() + 1);
rep(i, d.size()) ds[i + 1] = ds[i] + d[i];
vll es(e.size() + 1);
rep(i, e.size()) es[i + 1] = es[i] + e[i];
ll ret = 0;
repi(i, 1, es.size()) if (k - i <= d.size()) {
chmax(ret, i * i + es[i] + ds[max(0ll, min(k - i, (ll)ds.size() - 1))]);
}
cout << ret << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b) \
for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
ll ugauss(ll a, ll b) {
if (!a)
return 0;
if (a > 0 ^ b > 0)
return a / b;
else
return (a + (a > 0 ? -1 : 1)) / b + 1;
}
ll lgauss(ll a, ll b) {
if (!a)
return 0;
if (a > 0 ^ b > 0)
return (a + (a > 0 ? -1 : 1)) / b - 1;
else
return a / b;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
o << "(" << v.first << ", " << v.second << ")";
return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
using s = int[];
(void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
-> basic_ostream<Ch, Tr> & {
os << "(";
print_tuple(os, t, gen_seq<sizeof...(Args)>());
return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
rep(i, v.size()) {
rep(j, v[i].size()) o << v[i][j] << " ";
o << endl;
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const unordered_set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it;
o << "]";
return o;
}
vector<int> range(const int x, const int y) {
vector<int> v(y - x + 1);
iota(v.begin(), v.end(), x);
return v;
}
template <typename T> istream &operator>>(istream &i, vector<T> &o) {
rep(j, o.size()) i >> o[j];
return i;
}
template <typename T, typename S, typename U>
ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.front();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> unordered_map<T, ll> counter(vector<T> vec) {
unordered_map<T, ll> ret;
for (auto &&x : vec)
ret[x]++;
return ret;
};
void vizGraph(vvll &g, int mode = 0, string filename = "out.png") {
ofstream ofs("./out.dot");
ofs << "digraph graph_name {" << endl;
set<P> memo;
rep(i, g.size()) rep(j, g[i].size()) {
if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i))))
continue;
memo.insert(P(i, g[i][j]));
ofs << " " << i << " -> " << g[i][j]
<< (mode ? " [arrowhead = none]" : "") << endl;
}
ofs << "}" << endl;
ofs.close();
system(((string) "dot -T png out.dot >" + filename).c_str());
}
struct timeval start;
double sec() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec - start.tv_sec) + (tv.tv_usec - start.tv_usec) * 1e-6;
}
size_t random_seed;
struct init_ {
init_() {
ios::sync_with_stdio(false);
cin.tie(0);
gettimeofday(&start, NULL);
struct timeval myTime;
struct tm *time_st;
gettimeofday(&myTime, NULL);
time_st = localtime(&myTime.tv_sec);
srand(myTime.tv_usec);
random_seed = RAND_MAX / 2 + rand() / 2;
}
} init__;
#define ldout fixed << setprecision(40)
#define EPS (double)1e-14
#define INF (ll)1e18
#define mo (ll)(1e9 + 7)
int main(void) {
ll n, k;
cin >> n >> k;
vvll f(n);
rep(i, n) {
ll a, b;
cin >> a >> b;
f[a - 1].pb(b);
}
rep(i, n) sort(all(f[i]), greater<ll>());
vll d, e;
rep(i, n) rep(j, f[i].size())(j ? d : e).pb(f[i][j]);
sort(all(d), greater<ll>());
sort(all(e), greater<ll>());
vll ds(d.size() + 1);
rep(i, d.size()) ds[i + 1] = ds[i] + d[i];
vll es(e.size() + 1);
rep(i, e.size()) es[i + 1] = es[i] + e[i];
ll ret = 0;
repi(i, 1, es.size()) if (k - i <= d.size()) {
chmax(ret, i * i + es[i] + ds[max(0ll, min(k - i, (ll)ds.size() - 1))]);
}
cout << ret << endl;
return 0;
}
|
replace
| 202 | 203 | 202 | 203 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
int main() {
int n, k;
cin >> n >> k;
vector<pair<int, int>> sushi(n);
vector<ll> f(n + 1);
unordered_map<int, int> um;
rep(i, n) { cin >> sushi.at(i).second >> sushi.at(i).first; }
sort(sushi.begin(), sushi.end(), greater<pair<int, int>>());
// priority_queue<pair<int, int>, vector<pair<int, int>>,
// greater<pair<int, int>>>
// pq;
ll sum = 0;
rep(i, k) {
int type = sushi.at(i).second;
if (um.count(type)) {
um.at(type) += 1;
} else {
um.emplace(type, 1);
}
sum += sushi.at(i).first;
// pq.push(sushi.at(i));
}
f.at(um.size()) = sum + um.size() * um.size();
int from = k - 1;
int to = k;
ll maxval = f.at(um.size());
while (true) {
int ftype = sushi.at(from).second;
int fpt = sushi.at(from).first;
int ttype = sushi.at(to).second;
int tpt = sushi.at(to).first;
bool fstop = true;
bool tstop = true;
if (um.at(ftype) == 1) {
from--;
fstop = false;
}
if (um.count(ttype) != 0) {
to++;
tstop = false;
}
if (fstop && tstop) {
int usize = um.size();
f.at(usize + 1) =
f.at(usize) - fpt + tpt - usize * usize + (usize + 1) * (usize + 1);
um.emplace(ttype, 1);
maxval = max(f.at(usize + 1), maxval);
um.at(ftype) -= 1;
from--;
to++;
}
if (from == -1 || to == n) {
break;
}
}
cout << maxval << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
int main() {
int n, k;
cin >> n >> k;
vector<pair<int, int>> sushi(n);
vector<ll> f(n + 1);
unordered_map<int, int> um;
rep(i, n) { cin >> sushi.at(i).second >> sushi.at(i).first; }
sort(sushi.begin(), sushi.end(), greater<pair<int, int>>());
// priority_queue<pair<int, int>, vector<pair<int, int>>,
// greater<pair<int, int>>>
// pq;
ll sum = 0;
rep(i, k) {
int type = sushi.at(i).second;
if (um.count(type)) {
um.at(type) += 1;
} else {
um.emplace(type, 1);
}
sum += sushi.at(i).first;
// pq.push(sushi.at(i));
}
f.at(um.size()) = sum + um.size() * um.size();
int from = k - 1;
int to = k;
ll maxval = f.at(um.size());
if (n == k) {
cout << maxval << endl;
return 0;
}
while (true) {
int ftype = sushi.at(from).second;
int fpt = sushi.at(from).first;
int ttype = sushi.at(to).second;
int tpt = sushi.at(to).first;
bool fstop = true;
bool tstop = true;
if (um.at(ftype) == 1) {
from--;
fstop = false;
}
if (um.count(ttype) != 0) {
to++;
tstop = false;
}
if (fstop && tstop) {
int usize = um.size();
f.at(usize + 1) =
f.at(usize) - fpt + tpt - usize * usize + (usize + 1) * (usize + 1);
um.emplace(ttype, 1);
maxval = max(f.at(usize + 1), maxval);
um.at(ftype) -= 1;
from--;
to++;
}
if (from == -1 || to == n) {
break;
}
}
cout << maxval << endl;
return 0;
}
|
insert
| 40 | 40 | 40 | 44 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
int main() {
ll n, k;
cin >> n >> k;
vector<pair<int, int>> sushi(n);
for (auto &e : sushi)
cin >> e.first >> e.second;
sort(sushi.begin(), sushi.end(), greater<>());
vector<ll> kt, other;
kt.push_back(sushi[0].second);
for (int i = 1; i < n; ++i) {
if (sushi[i].first != sushi[i - 1].first) {
kt.push_back(sushi[i].second);
} else {
other.push_back(sushi[i].second);
}
}
sort(kt.begin(), kt.end(), greater<>());
sort(other.begin(), other.end(), greater<>());
ll kcnt = min(k, (ll)kt.size());
ll p = accumulate(kt.begin(), kt.begin() + kcnt, 0LL) + kcnt * kcnt;
p += accumulate(other.begin(), other.begin() + (k - kcnt), 0LL);
ll ans = p;
--kcnt;
for (; k - kcnt <= other.size(); --kcnt) {
p -= kt[kcnt] + ((kcnt + 1) * (kcnt + 1)) - (kcnt * kcnt);
p += other[k - kcnt - 1];
ans = max(ans, p);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
int main() {
ll n, k;
cin >> n >> k;
vector<pair<int, int>> sushi(n);
for (auto &e : sushi)
cin >> e.first >> e.second;
sort(sushi.begin(), sushi.end(), greater<>());
vector<ll> kt, other;
kt.push_back(sushi[0].second);
for (int i = 1; i < n; ++i) {
if (sushi[i].first != sushi[i - 1].first) {
kt.push_back(sushi[i].second);
} else {
other.push_back(sushi[i].second);
}
}
sort(kt.begin(), kt.end(), greater<>());
sort(other.begin(), other.end(), greater<>());
ll kcnt = min(k, (ll)kt.size());
ll p = accumulate(kt.begin(), kt.begin() + kcnt, 0LL) + kcnt * kcnt;
p += accumulate(other.begin(), other.begin() + (k - kcnt), 0LL);
ll ans = p;
--kcnt;
for (; kcnt > 0 && k - kcnt <= other.size(); --kcnt) {
p -= kt[kcnt] + ((kcnt + 1) * (kcnt + 1)) - (kcnt * kcnt);
p += other[k - kcnt - 1];
ans = max(ans, p);
}
cout << ans << endl;
return 0;
}
|
replace
| 45 | 46 | 45 | 46 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
ll N, K;
cin >> N >> K;
vector<Pll> sushis(N);
for (ll i = 0; i < N; i++) {
cin >> sushis[i].second >> sushis[i].first;
}
sort(sushis.begin(), sushis.end(), greater<Pll>());
ll pdeli = 0;
stack<ll> dups;
unordered_set<ll> st;
for (ll i = 0; i < K; i++) {
ll t, d;
tie(d, t) = sushis[i];
if (st.find(t) != st.end()) {
dups.emplace(d);
}
pdeli += d;
st.emplace(t);
}
ll nk = (ll)st.size();
ll ans = pdeli + nk * nk;
for (ll i = K; i < N; i++) {
ll t, d;
tie(d, t) = sushis[i];
if (st.find(t) != st.end())
continue;
pdeli -= dups.top();
dups.pop();
pdeli += d;
st.emplace(t);
nk = (ll)st.size();
ans = max(ans, pdeli + nk * nk);
}
cout << ans << "\n";
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
ifstream ifs("./in_out/input.txt");
cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
ofstream ofs("./in_out/output.txt");
cout.rdbuf(ofs.rdbuf());
#endif
solve();
cout << flush;
return 0;
}
|
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
ll N, K;
cin >> N >> K;
vector<Pll> sushis(N);
for (ll i = 0; i < N; i++) {
cin >> sushis[i].second >> sushis[i].first;
}
sort(sushis.begin(), sushis.end(), greater<Pll>());
ll pdeli = 0;
stack<ll> dups;
unordered_set<ll> st;
for (ll i = 0; i < K; i++) {
ll t, d;
tie(d, t) = sushis[i];
if (st.find(t) != st.end()) {
dups.emplace(d);
}
pdeli += d;
st.emplace(t);
}
ll nk = (ll)st.size();
ll ans = pdeli + nk * nk;
for (ll i = K; i < N; i++) {
ll t, d;
tie(d, t) = sushis[i];
if (st.find(t) != st.end())
continue;
if (dups.empty())
break;
pdeli -= dups.top();
dups.pop();
pdeli += d;
st.emplace(t);
nk = (ll)st.size();
ans = max(ans, pdeli + nk * nk);
}
cout << ans << "\n";
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
ifstream ifs("./in_out/input.txt");
cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
ofstream ofs("./in_out/output.txt");
cout.rdbuf(ofs.rdbuf());
#endif
solve();
cout << flush;
return 0;
}
|
insert
| 44 | 44 | 44 | 46 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
typedef long long ll;
#define PI 3.14159265359
using namespace std;
bool newcoun[100001];
bool counter[100001];
bool firused[100001];
bool secused[100001];
int councoun[100001];
ll llmax(ll x, ll y) {
if (x > y)
return x;
return y;
}
int main() {
ll n, k, maximum = 0;
cin >> n >> k;
vector<pair<ll, ll>> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i].second >> vec[i].first;
newcoun[vec[i].second] = true;
}
for (int i = 0; i <= n; i++) {
if (newcoun[i]) {
maximum++;
}
}
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
ll sumoisi = 0;
for (int i = 0; i < k; i++) {
sumoisi += vec[i].first;
counter[vec[i].second] = true;
councoun[vec[i].second]++;
if (firused[vec[i].second]) {
secused[vec[i].second] = true;
} else {
firused[vec[i].second] = true;
}
}
ll coun = 0;
for (int i = 1; i <= n; i++) {
if (counter[i]) {
coun++;
}
}
sumoisi += coun * coun;
ll ans = sumoisi;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
priority_queue<pair<ll, ll>> pq2;
for (int i = k - 1; i >= 0; i--) {
if (secused[vec[i].second] && councoun[vec[i].second] > 1) {
pq.push(make_pair(vec[i].first, vec[i].second));
councoun[vec[i].second]--;
}
}
for (int i = k; i < n; i++) {
if (!firused[vec[i].second]) {
pq2.push(make_pair(vec[i].first, vec[i].second));
}
}
for (int i = coun; i < maximum; i++) {
pair<ll, ll> p = pq.top();
pq.pop();
sumoisi -= p.first;
if (pq2.empty()) {
cout << ans << endl;
return 0;
}
pair<ll, ll> p2 = pq2.top();
while (firused[p2.second]) {
if (pq2.empty()) {
cout << ans << endl;
return 0;
}
pq2.pop();
p2 = pq2.top();
}
pq2.pop();
firused[p2.second] = true;
sumoisi += p2.first;
sumoisi += (i + 1) * (i + 1);
sumoisi -= i * i;
ans = max(ans, sumoisi);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
typedef long long ll;
#define PI 3.14159265359
using namespace std;
bool newcoun[100001];
bool counter[100001];
bool firused[100001];
bool secused[100001];
int councoun[100001];
ll llmax(ll x, ll y) {
if (x > y)
return x;
return y;
}
int main() {
ll n, k, maximum = 0;
cin >> n >> k;
vector<pair<ll, ll>> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i].second >> vec[i].first;
newcoun[vec[i].second] = true;
}
for (int i = 0; i <= n; i++) {
if (newcoun[i]) {
maximum++;
}
}
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
ll sumoisi = 0;
for (int i = 0; i < k; i++) {
sumoisi += vec[i].first;
counter[vec[i].second] = true;
councoun[vec[i].second]++;
if (firused[vec[i].second]) {
secused[vec[i].second] = true;
} else {
firused[vec[i].second] = true;
}
}
ll coun = 0;
for (int i = 1; i <= n; i++) {
if (counter[i]) {
coun++;
}
}
sumoisi += coun * coun;
ll ans = sumoisi;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
priority_queue<pair<ll, ll>> pq2;
for (int i = k - 1; i >= 0; i--) {
if (secused[vec[i].second] && councoun[vec[i].second] > 1) {
pq.push(make_pair(vec[i].first, vec[i].second));
councoun[vec[i].second]--;
}
}
for (int i = k; i < n; i++) {
if (!firused[vec[i].second]) {
pq2.push(make_pair(vec[i].first, vec[i].second));
}
}
for (int i = coun; i < maximum; i++) {
if (pq.empty()) {
cout << ans << endl;
return 0;
}
pair<ll, ll> p = pq.top();
pq.pop();
sumoisi -= p.first;
if (pq2.empty()) {
cout << ans << endl;
return 0;
}
pair<ll, ll> p2 = pq2.top();
while (firused[p2.second]) {
if (pq2.empty()) {
cout << ans << endl;
return 0;
}
pq2.pop();
p2 = pq2.top();
}
pq2.pop();
firused[p2.second] = true;
sumoisi += p2.first;
sumoisi += (i + 1) * (i + 1);
sumoisi -= i * i;
ans = max(ans, sumoisi);
}
cout << ans << endl;
return 0;
}
|
insert
| 82 | 82 | 82 | 86 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using i64 = int64_t;
using i32 = int32_t;
int main() {
int n, k;
cin >> n >> k;
using P = pair<i64, i64>;
vector<P> td(n);
for (int i = 0; i < n; ++i) {
i64 t, d;
cin >> t >> d;
td[i] = {t, d};
}
sort(begin(td), end(td), [](P p, P q) { return p.second < q.second; });
vector<bool> ct(n + 1);
i64 del = 0;
i64 kin = 0;
priority_queue<i64, vector<i64>, greater<i64>> que;
for (int i = 0; i < k; ++i) {
i64 t, d;
tie(t, d) = td.back();
td.pop_back();
del += d;
if (!ct[t]) {
ct[t] = true;
kin++;
} else {
que.push(d);
}
}
i64 ans = del + kin * kin;
while (!td.empty()) {
i64 t, d;
tie(t, d) = td.back();
td.pop_back();
if (!ct[t]) {
ct[t] = true;
kin++;
del -= que.top();
que.pop();
del += d;
ans = max(ans, del + kin * kin);
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using i64 = int64_t;
using i32 = int32_t;
int main() {
int n, k;
cin >> n >> k;
using P = pair<i64, i64>;
vector<P> td(n);
for (int i = 0; i < n; ++i) {
i64 t, d;
cin >> t >> d;
td[i] = {t, d};
}
sort(begin(td), end(td), [](P p, P q) { return p.second < q.second; });
vector<bool> ct(n + 1);
i64 del = 0;
i64 kin = 0;
priority_queue<i64, vector<i64>, greater<i64>> que;
for (int i = 0; i < k; ++i) {
i64 t, d;
tie(t, d) = td.back();
td.pop_back();
del += d;
if (!ct[t]) {
ct[t] = true;
kin++;
} else {
que.push(d);
}
}
i64 ans = del + kin * kin;
while (!td.empty() && !que.empty()) {
i64 t, d;
tie(t, d) = td.back();
td.pop_back();
if (!ct[t]) {
ct[t] = true;
kin++;
del -= que.top();
que.pop();
del += d;
ans = max(ans, del + kin * kin);
}
}
cout << ans << endl;
return 0;
}
|
replace
| 53 | 54 | 53 | 54 |
0
| |
p03148
|
C++
|
Runtime Error
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
ll n, k, ans = 0;
cin >> n >> k;
ll cnt = k;
set<ll> c;
vector<pair<ll, ll>> v(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
v[i] = mk(d, t);
}
sort(al(v));
reverse(al(v));
vector<vector<ll>> sushi(n + 1);
rep(i, k) {
ans += v[i].first;
c.insert(v[i].second);
sushi[v[i].second].push_back(v[i].first);
}
vector<ll> l;
rep(i, n + 1) {
if ((ll)sushi[i].size() <= 1)
continue;
sort(al(sushi[i]));
rep(j, (ll)sushi[i].size() - 1) { l.push_back(sushi[i][j]); }
}
if ((ll)l.size() > 0)
sort(al(l));
ans += c.size() * c.size();
ll cnt2 = 0, cur = ans;
while (cnt < n) {
if (c.count(v[cnt].second)) {
cnt++;
continue;
} else {
c.insert(v[cnt].second);
cur -= l[cnt2];
cnt2++;
cur += v[cnt].first;
cur += c.size() * 2 - 1;
cnt++;
ans = max(ans, cur);
}
}
cout << ans << endl;
}
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
ll n, k, ans = 0;
cin >> n >> k;
ll cnt = k;
set<ll> c;
vector<pair<ll, ll>> v(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
v[i] = mk(d, t);
}
sort(al(v));
reverse(al(v));
vector<vector<ll>> sushi(n + 1);
rep(i, k) {
ans += v[i].first;
c.insert(v[i].second);
sushi[v[i].second].push_back(v[i].first);
}
vector<ll> l;
rep(i, n + 1) {
if ((ll)sushi[i].size() <= 1)
continue;
sort(al(sushi[i]));
rep(j, (ll)sushi[i].size() - 1) { l.push_back(sushi[i][j]); }
}
if ((ll)l.size() > 0)
sort(al(l));
ans += c.size() * c.size();
ll cnt2 = 0, cur = ans;
while (cnt < n) {
if (c.count(v[cnt].second)) {
cnt++;
continue;
} else {
if (cnt2 == (ll)l.size())
break;
c.insert(v[cnt].second);
cur -= l[cnt2];
cnt2++;
cur += v[cnt].first;
cur += c.size() * 2 - 1;
cnt++;
ans = max(ans, cur);
}
}
cout << ans << endl;
}
|
insert
| 51 | 51 | 51 | 53 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mop 1000000007
using namespace std;
struct items {
ll t, v;
};
static const ll MAX = 100001;
bool asc(const items &left, const items &right) {
return left.v == right.v ? left.t < right.t : left.v < right.v;
}
int main() {
ll n, k;
cin >> n >> k;
vector<items> lis, rem;
for (ll i = 0; i < n; i++) {
items p;
cin >> p.t >> p.v;
lis.push_back(p);
}
sort(lis.begin(), lis.end(), asc);
reverse(lis.begin(), lis.end());
ll eachmax[MAX] = {};
ll per = 0;
for (ll i = 0; i < n; i++) {
if (eachmax[lis[i].t] < lis[i].v) {
eachmax[lis[i].t] = lis[i].v;
}
}
ll numt = 0;
ll exist[MAX] = {};
for (ll i = 0; i < k; i++) {
per += lis[i].v;
if (exist[lis[i].t] == 0) {
numt++;
} else {
rem.push_back(lis[i]);
}
exist[lis[i].t] = 1;
// cout << lis[i].t << endl;
}
reverse(rem.begin(), rem.end());
ll ans = 0;
if (ans < per + numt * numt) {
ans = per + numt * numt;
}
// cout << ans << endl;
vector<ll> memo;
for (ll i = 0; i < n; i++) {
if (exist[i] == 0 and eachmax[i] > 0) {
memo.push_back(eachmax[i]);
}
}
sort(memo.begin(), memo.end());
reverse(memo.begin(), memo.end());
// cout << rem.size() << endl;
// cout << memo.size() << endl;
for (ll i = 0; i < memo.size(); i++) {
per -= rem[i].v;
per += memo[i];
numt++;
if (ans < per + numt * numt) {
ans = per + numt * numt;
}
// cout << ans << endl;
}
// cout << "mofu" << endl;
cout << ans << endl;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mop 1000000007
using namespace std;
struct items {
ll t, v;
};
static const ll MAX = 100001;
bool asc(const items &left, const items &right) {
return left.v == right.v ? left.t < right.t : left.v < right.v;
}
int main() {
ll n, k;
cin >> n >> k;
vector<items> lis, rem;
for (ll i = 0; i < n; i++) {
items p;
cin >> p.t >> p.v;
lis.push_back(p);
}
sort(lis.begin(), lis.end(), asc);
reverse(lis.begin(), lis.end());
ll eachmax[MAX] = {};
ll per = 0;
for (ll i = 0; i < n; i++) {
if (eachmax[lis[i].t] < lis[i].v) {
eachmax[lis[i].t] = lis[i].v;
}
}
ll numt = 0;
ll exist[MAX] = {};
for (ll i = 0; i < k; i++) {
per += lis[i].v;
if (exist[lis[i].t] == 0) {
numt++;
} else {
rem.push_back(lis[i]);
}
exist[lis[i].t] = 1;
// cout << lis[i].t << endl;
}
reverse(rem.begin(), rem.end());
ll ans = 0;
if (ans < per + numt * numt) {
ans = per + numt * numt;
}
// cout << ans << endl;
vector<ll> memo;
for (ll i = 0; i < n; i++) {
if (exist[i] == 0 and eachmax[i] > 0) {
memo.push_back(eachmax[i]);
}
}
sort(memo.begin(), memo.end());
reverse(memo.begin(), memo.end());
// cout << rem.size() << endl;
// cout << memo.size() << endl;
for (ll i = 0; i < min(memo.size(), rem.size()); i++) {
per -= rem[i].v;
per += memo[i];
numt++;
if (ans < per + numt * numt) {
ans = per + numt * numt;
}
// cout << ans << endl;
}
// cout << "mofu" << endl;
cout << ans << endl;
}
|
replace
| 79 | 80 | 79 | 80 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l) \
sort(ALL(l)); \
l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void _OUT(){};
template <class H, class... T> void _OUT(H &h, T &...t) {
cout << ' ' << h;
_OUT(t...);
};
template <class H, class... T> void _P(H &h, T &...t) {
cout << h;
_OUT(t...);
};
#define _PN(...) \
_P(__VA_ARGS__); \
cout << endl;
void solve();
signed main() {
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif
int N, K;
vector<int> C(SIZE), Q;
void solve() {
cin >> N >> K;
int t, d;
priority_queue<pair<int, int>> que;
REP(i, N) {
cin >> t >> d;
que.push({d, t});
}
int ans = 0, cur = 0, s = 0;
while (K--) {
auto [d, t] = que.top();
que.pop();
cur += d;
if (!C[t])
s++;
else
Q.push_back(d);
C[t] = 1;
}
chmax(ans, cur + s * s);
while (que.size()) {
auto [d, t] = que.top();
que.pop();
if (!C[t]) {
s++;
cur -= Q.back();
cur += d;
Q.pop_back();
C[t] = 1;
chmax(ans, cur + s * s);
}
}
_PN(ans);
}
|
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l) \
sort(ALL(l)); \
l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void _OUT(){};
template <class H, class... T> void _OUT(H &h, T &...t) {
cout << ' ' << h;
_OUT(t...);
};
template <class H, class... T> void _P(H &h, T &...t) {
cout << h;
_OUT(t...);
};
#define _PN(...) \
_P(__VA_ARGS__); \
cout << endl;
void solve();
signed main() {
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif
int N, K;
vector<int> C(SIZE), Q;
void solve() {
cin >> N >> K;
int t, d;
priority_queue<pair<int, int>> que;
REP(i, N) {
cin >> t >> d;
que.push({d, t});
}
int ans = 0, cur = 0, s = 0;
while (K--) {
auto [d, t] = que.top();
que.pop();
cur += d;
if (!C[t])
s++;
else
Q.push_back(d);
C[t] = 1;
}
chmax(ans, cur + s * s);
while (que.size() && Q.size()) {
auto [d, t] = que.top();
que.pop();
if (!C[t]) {
s++;
cur -= Q.back();
cur += d;
Q.pop_back();
C[t] = 1;
chmax(ans, cur + s * s);
}
}
_PN(ans);
}
|
replace
| 89 | 90 | 89 | 90 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<pair<int, int>> d_t;
for (int i = 0; i < N; i++) {
int t, d;
cin >> t >> d;
d_t.push_back(make_pair(d, t));
}
sort(d_t.begin(), d_t.end(), greater<pair<int, int>>());
vector<long long> ans(1, 0);
map<int, bool> mp;
vector<pair<int, int>> NG;
int sz = 0;
for (int i = 0; i < K; i++) {
ans[0] += d_t[i].first;
if (mp[d_t[i].second]) {
NG.push_back(d_t[i]);
} else {
mp[d_t[i].second] = true;
sz++;
}
}
ans[0] += (long long)sz * sz;
reverse(NG.begin(), NG.end());
int x = 0;
for (int i = K; i < N; i++) {
if (mp[d_t[i].second] == false) {
long long a = ans[ans.size() - 1];
a += (long long)2 * sz + 1;
a -= NG[x].first;
a += d_t[i].first;
ans.push_back(a);
mp[d_t[i].second] = true;
sz++;
x++;
}
if (x == NG.size())
break;
}
cout << *max_element(ans.begin(), ans.end()) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<pair<int, int>> d_t;
for (int i = 0; i < N; i++) {
int t, d;
cin >> t >> d;
d_t.push_back(make_pair(d, t));
}
sort(d_t.begin(), d_t.end(), greater<pair<int, int>>());
vector<long long> ans(1, 0);
map<int, bool> mp;
vector<pair<int, int>> NG;
int sz = 0;
for (int i = 0; i < K; i++) {
ans[0] += d_t[i].first;
if (mp[d_t[i].second]) {
NG.push_back(d_t[i]);
} else {
mp[d_t[i].second] = true;
sz++;
}
}
ans[0] += (long long)sz * sz;
if (NG.size() == 0) {
cout << ans[0] << endl;
return 0;
}
reverse(NG.begin(), NG.end());
int x = 0;
for (int i = K; i < N; i++) {
if (mp[d_t[i].second] == false) {
long long a = ans[ans.size() - 1];
a += (long long)2 * sz + 1;
a -= NG[x].first;
a += d_t[i].first;
ans.push_back(a);
mp[d_t[i].second] = true;
sz++;
x++;
}
if (x == NG.size())
break;
}
cout << *max_element(ans.begin(), ans.end()) << endl;
return 0;
}
|
replace
| 29 | 30 | 29 | 33 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm> // minmax, sort, swap
#include <cmath>
#include <cstdio> // printf, scanf
#include <deque> // deque
#include <functional> // function<void(int)>
#include <iomanip> // cout<<setprecision(n)
#include <iostream> // cin, cout, cerr
#include <map> // key-value pairs sorted by keys
#include <numeric> // iota
#include <queue> // queue, priority_queue
#include <set> // set
#include <string> // string, stoi, to_string
#include <vector> // vector
#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define int long long // at least int64 > 9*10^18
#define ENDL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */
signed main() {
int n, k;
std::cin >> n >> k;
std::map<int, std::vector<int>> map;
rep(i, n) {
int t, d;
std::cin >> t >> d;
map[t].emplace_back(d);
}
for (auto &&x : map)
std::sort(all(x.second), std::greater<int>());
// get the best of all varieties
std::vector<int> best;
for (auto x : map)
best.emplace_back(x.second[0]);
std::vector<int> rest;
for (auto x : map)
for (int i = 1; i < x.second.size(); i++)
rest.emplace_back(x.second[i]);
std::sort(all(best), std::greater<int>());
std::sort(all(rest), std::greater<int>());
std::vector<int> bac(best.size() + 1, 0);
std::vector<int> rac(rest.size() + 1, 0);
rep(i, best.size()) bac[i + 1] = bac[i] + best[i];
rep(i, rest.size()) rac[i + 1] = rac[i] + rest[i];
int max = 0;
for (int t = k - rest.size() + 1; t <= std::min(k, (int)best.size()); t++) {
int sum = t * t;
sum += bac[t];
sum += rac[k - t];
max = std::max(max, sum);
}
print(max);
return 0;
}
|
#include <algorithm> // minmax, sort, swap
#include <cmath>
#include <cstdio> // printf, scanf
#include <deque> // deque
#include <functional> // function<void(int)>
#include <iomanip> // cout<<setprecision(n)
#include <iostream> // cin, cout, cerr
#include <map> // key-value pairs sorted by keys
#include <numeric> // iota
#include <queue> // queue, priority_queue
#include <set> // set
#include <string> // string, stoi, to_string
#include <vector> // vector
#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define int long long // at least int64 > 9*10^18
#define ENDL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */
signed main() {
int n, k;
std::cin >> n >> k;
std::map<int, std::vector<int>> map;
rep(i, n) {
int t, d;
std::cin >> t >> d;
map[t].emplace_back(d);
}
for (auto &&x : map)
std::sort(all(x.second), std::greater<int>());
// get the best of all varieties
std::vector<int> best;
for (auto x : map)
best.emplace_back(x.second[0]);
std::vector<int> rest;
for (auto x : map)
for (int i = 1; i < x.second.size(); i++)
rest.emplace_back(x.second[i]);
std::sort(all(best), std::greater<int>());
std::sort(all(rest), std::greater<int>());
std::vector<int> bac(best.size() + 1, 0);
std::vector<int> rac(rest.size() + 1, 0);
rep(i, best.size()) bac[i + 1] = bac[i] + best[i];
rep(i, rest.size()) rac[i + 1] = rac[i] + rest[i];
int max = 0;
for (int t = 1; t <= std::min(k, (int)best.size()); t++) {
if (t > best.size() || k - t > rest.size())
continue;
int sum = t * t;
sum += bac[t];
sum += rac[k - t];
max = std::max(max, sum);
}
print(max);
return 0;
}
|
replace
| 53 | 54 | 53 | 56 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
#define FOR(i, N) for (i = 0; i < N; i++)
int N, K;
vector<P> t;
bool pairCompareFirst(const P &firstElof, const P &secondElof) {
return firstElof.first > secondElof.first;
}
bool pairCompareSecond(const P &firstElof, const P &secondElof) {
return firstElof.second > secondElof.second;
}
int main(void) {
cin >> N >> K;
t.resize(N);
// k:食べる種類,k_max:食べる種類の最大値
int i, k = 0, k_max = 0;
int num[N + 1];
// 種類iの最大か否か
bool kind_max[N];
vector<ll> base(N, 0);
ll sum = 0;
ll max = 0;
FOR(i, N + 1) num[i] = 0;
FOR(i, N) { cin >> t[i].first >> t[i].second; }
sort(t.begin(), t.end(), pairCompareSecond);
FOR(i, N) {
k_max += num[t[i].first] == 0;
kind_max[i] = (num[t[i].first] == 0);
if (i < K) {
sum += t[i].second;
k += num[t[i].first] == 0;
}
num[t[i].first]++;
}
base[k - 1] = sum;
max = sum + (ll)k * k;
int EM_index = K - 1, NM_index = K;
for (i = k; i <= k_max && i <= K; i++) {
// 交換可能な場所まで移動
while (kind_max[EM_index]) {
EM_index--;
if (EM_index < 0)
break;
}
while (!kind_max[NM_index]) {
NM_index++;
if (NM_index >= N)
break;
}
// cout << EM_index << " " << NM_index << endl;
base[i] = base[i - 1] - t[EM_index].second + t[NM_index].second;
if (base[i] + (ll)(i + 1) * (i + 1) > max)
max = base[i] + (ll)(i + 1) * (i + 1);
swap(t[EM_index], t[NM_index]);
swap(kind_max[EM_index], kind_max[NM_index]);
if (EM_index != 0)
EM_index--;
else
break;
if (NM_index != N - 1)
NM_index++;
else
break;
}
// cout << endl;
// FOR(i,N) cout << t[i].first << " " << t[i].second << " " << kind_max[i] <<
// endl;
cout << max << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
#define FOR(i, N) for (i = 0; i < N; i++)
int N, K;
vector<P> t;
bool pairCompareFirst(const P &firstElof, const P &secondElof) {
return firstElof.first > secondElof.first;
}
bool pairCompareSecond(const P &firstElof, const P &secondElof) {
return firstElof.second > secondElof.second;
}
int main(void) {
cin >> N >> K;
t.resize(N);
// k:食べる種類,k_max:食べる種類の最大値
int i, k = 0, k_max = 0;
int num[N + 1];
// 種類iの最大か否か
bool kind_max[N];
vector<ll> base(N, 0);
ll sum = 0;
ll max = 0;
FOR(i, N + 1) num[i] = 0;
FOR(i, N) { cin >> t[i].first >> t[i].second; }
sort(t.begin(), t.end(), pairCompareSecond);
FOR(i, N) {
k_max += num[t[i].first] == 0;
kind_max[i] = (num[t[i].first] == 0);
if (i < K) {
sum += t[i].second;
k += num[t[i].first] == 0;
}
num[t[i].first]++;
}
base[k - 1] = sum;
max = sum + (ll)k * k;
int EM_index = K - 1, NM_index = K;
for (i = k; i < k_max && i < K; i++) {
// 交換可能な場所まで移動
while (kind_max[EM_index]) {
EM_index--;
if (EM_index < 0)
break;
}
while (!kind_max[NM_index]) {
NM_index++;
if (NM_index >= N)
break;
}
// cout << EM_index << " " << NM_index << endl;
base[i] = base[i - 1] - t[EM_index].second + t[NM_index].second;
if (base[i] + (ll)(i + 1) * (i + 1) > max)
max = base[i] + (ll)(i + 1) * (i + 1);
swap(t[EM_index], t[NM_index]);
swap(kind_max[EM_index], kind_max[NM_index]);
if (EM_index != 0)
EM_index--;
else
break;
if (NM_index != N - 1)
NM_index++;
else
break;
}
// cout << endl;
// FOR(i,N) cout << t[i].first << " " << t[i].second << " " << kind_max[i] <<
// endl;
cout << max << endl;
return 0;
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
int t, d;
Node() {}
Node(int _t, int _d) : t(_t), d(_d) {}
bool operator<(const Node &_r) const { return d > _r.d; }
};
void solve() {
int n, k;
cin >> n >> k;
vector<Node> a(n);
for (auto &p : a) {
cin >> p.t >> p.d;
p.t--;
}
sort(a.begin(), a.end());
vector<bool> trk(k, false);
vector<int> stk;
int type = 0;
long long sum = 0;
for (int i = 0; i < k; i++) {
int t = a[i].t, d = a[i].d;
if (!trk[t]) {
trk[t] = true;
type++;
} else {
stk.push_back(d);
}
sum += d;
}
long long res = sum + 1ll * type * type;
for (int i = k; i < n; i++) {
int t = a[i].t, d = a[i].d;
if (stk.empty())
break;
if (trk[t])
continue;
trk[t] = true;
type++;
sum -= stk.back();
stk.pop_back();
sum += d;
res = max(res, sum + 1ll * type * type);
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
int t, d;
Node() {}
Node(int _t, int _d) : t(_t), d(_d) {}
bool operator<(const Node &_r) const { return d > _r.d; }
};
void solve() {
int n, k;
cin >> n >> k;
vector<Node> a(n);
for (auto &p : a) {
cin >> p.t >> p.d;
p.t--;
}
sort(a.begin(), a.end());
vector<bool> trk(n, false);
vector<int> stk;
int type = 0;
long long sum = 0;
for (int i = 0; i < k; i++) {
int t = a[i].t, d = a[i].d;
if (!trk[t]) {
trk[t] = true;
type++;
} else {
stk.push_back(d);
}
sum += d;
}
long long res = sum + 1ll * type * type;
for (int i = k; i < n; i++) {
int t = a[i].t, d = a[i].d;
if (stk.empty())
break;
if (trk[t])
continue;
trk[t] = true;
type++;
sum -= stk.back();
stk.pop_back();
sum += d;
res = max(res, sum + 1ll * type * type);
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
cout << endl;
}
|
replace
| 20 | 21 | 20 | 21 |
0
| |
p03148
|
C++
|
Runtime Error
|
#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;
}
// INSERT ABOVE HERE
signed main() {
Int n, k;
cin >> n >> k;
vector<Int> t(n), d(n);
for (Int i = 0; i < n; i++)
cin >> t[i] >> d[i], t[i]--;
{
using P = pair<Int, Int>;
vector<P> vp;
for (Int i = 0; i < n; i++)
vp.emplace_back(d[i], t[i]);
sort(vp.rbegin(), vp.rend());
for (Int i = 0; i < n; i++)
tie(d[i], t[i]) = vp[i];
}
priority_queue<Int, vector<Int>, greater<Int>> pq;
Int res = 0;
vector<Int> used(n, 0);
for (Int i = 0; i < k; i++) {
if (used[t[i]])
pq.emplace(d[i]);
used[t[i]] = 1;
res += d[i];
}
Int cnt = 0;
for (Int i = 0; i < n; i++)
cnt += used[i];
Int ans = res + cnt * cnt;
for (Int i = k; i < n; i++) {
if (used[t[i]])
continue;
used[t[i]] = 1;
cnt++;
res -= pq.top();
pq.pop();
res += d[i];
chmax(ans, res + cnt * cnt);
}
cout << ans << endl;
return 0;
}
|
#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;
}
// INSERT ABOVE HERE
signed main() {
Int n, k;
cin >> n >> k;
vector<Int> t(n), d(n);
for (Int i = 0; i < n; i++)
cin >> t[i] >> d[i], t[i]--;
{
using P = pair<Int, Int>;
vector<P> vp;
for (Int i = 0; i < n; i++)
vp.emplace_back(d[i], t[i]);
sort(vp.rbegin(), vp.rend());
for (Int i = 0; i < n; i++)
tie(d[i], t[i]) = vp[i];
}
priority_queue<Int, vector<Int>, greater<Int>> pq;
Int res = 0;
vector<Int> used(n, 0);
for (Int i = 0; i < k; i++) {
if (used[t[i]])
pq.emplace(d[i]);
used[t[i]] = 1;
res += d[i];
}
Int cnt = 0;
for (Int i = 0; i < n; i++)
cnt += used[i];
Int ans = res + cnt * cnt;
for (Int i = k; i < n; i++) {
if (used[t[i]])
continue;
if (pq.empty())
break;
used[t[i]] = 1;
cnt++;
res -= pq.top();
pq.pop();
res += d[i];
chmax(ans, res + cnt * cnt);
}
cout << ans << endl;
return 0;
}
|
insert
| 47 | 47 | 47 | 49 |
0
| |
p03148
|
Python
|
Runtime Error
|
n, k = map(int, input().split())
td = sorted(
[list(map(int, input().split())) for i in range(n)],
reverse=True,
key=lambda x: x[1],
)
type = set()
L = []
Sum = 0
for x in td[:k]:
Sum += x[1]
if x[0] not in type:
type.add(x[0])
else:
L.append(x[1])
L = L[::-1]
type_cnt = len(type)
ans = Sum + type_cnt**2
for x in td[k:]:
if x[0] not in type:
type.add(x[0])
type_cnt += 1
Sum = Sum - L.pop(0) + x[1]
ans = max(ans, Sum + type_cnt**2)
print(ans)
|
n, k = map(int, input().split())
td = sorted(
[list(map(int, input().split())) for i in range(n)],
reverse=True,
key=lambda x: x[1],
)
type = set()
L = []
Sum = 0
for x in td[:k]:
Sum += x[1]
if x[0] not in type:
type.add(x[0])
else:
L.append(x[1])
L = L[::-1]
type_cnt = len(type)
ans = Sum + type_cnt**2
for x in td[k:]:
if len(L) == 0:
break
if x[0] not in type:
type.add(x[0])
type_cnt += 1
Sum = Sum - L.pop(0) + x[1]
ans = max(ans, Sum + type_cnt**2)
print(ans)
|
insert
| 21 | 21 | 21 | 23 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
void solve(long long N, long long K, std::vector<long long> t,
std::vector<long long> d) {
vector<pair<long long, long long>> vs;
for (int i = 0; i < K; i++) {
vs.push_back({d[i], t[i]});
}
sort(vs.rbegin(), vs.rend());
for (int i = 0; i < vs.size(); i++)
swap(vs[i].first, vs[i].second);
long long tot = 0;
set<long long> ts;
vector<long long> remCand;
for (int i = 0; i < K; i++) {
if (!ts.count(vs[i].first)) {
ts.insert(vs[i].first);
} else {
remCand.push_back(vs[i].second);
}
tot += vs[i].second;
}
sort(remCand.rbegin(), remCand.rend());
long long ans = tot + 1ll * ts.size() * ts.size();
for (int i = K; i < N; i++) {
if (!ts.count(vs[i].first) && remCand.size() > 0) {
tot += vs[i].second;
tot -= remCand.back();
remCand.pop_back();
ts.insert(vs[i].first);
ans = max<long long>(tot + 1ll * ts.size() * ts.size(), ans);
}
}
cout << ans << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> t(N);
std::vector<long long> d(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &t[i]);
scanf("%lld", &d[i]);
}
solve(N, K, std::move(t), std::move(d));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve(long long N, long long K, std::vector<long long> t,
std::vector<long long> d) {
vector<pair<long long, long long>> vs;
for (int i = 0; i < N; i++) {
vs.push_back({d[i], t[i]});
}
sort(vs.rbegin(), vs.rend());
for (int i = 0; i < vs.size(); i++)
swap(vs[i].first, vs[i].second);
long long tot = 0;
set<long long> ts;
vector<long long> remCand;
for (int i = 0; i < K; i++) {
if (!ts.count(vs[i].first)) {
ts.insert(vs[i].first);
} else {
remCand.push_back(vs[i].second);
}
tot += vs[i].second;
}
sort(remCand.rbegin(), remCand.rend());
long long ans = tot + 1ll * ts.size() * ts.size();
for (int i = K; i < N; i++) {
if (!ts.count(vs[i].first) && remCand.size() > 0) {
tot += vs[i].second;
tot -= remCand.back();
remCand.pop_back();
ts.insert(vs[i].first);
ans = max<long long>(tot + 1ll * ts.size() * ts.size(), ans);
}
}
cout << ans << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> t(N);
std::vector<long long> d(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &t[i]);
scanf("%lld", &d[i]);
}
solve(N, K, std::move(t), std::move(d));
return 0;
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
signed main() {
int n, k;
cin >> n >> k;
vector<pair<int, ll>> v(n);
REP(i, n) { cin >> v[i].second >> v[i].first; }
sort(ALL(v));
reverse(ALL(v));
ll now = 0;
set<int> s;
multiset<ll> used;
for (int i = 0; i < k; i++) {
now += v[i].first;
if (s.count(v[i].second))
used.insert(v[i].first);
else
s.insert(v[i].second);
}
now += s.size() * s.size();
ll ans = now;
for (int i = k; i < n; i++) {
if (!s.count(v[i].second)) {
now -= *used.begin();
used.erase(used.begin());
s.insert(v[i].second);
now += v[i].first;
now += s.size() * s.size() - (s.size() - 1) * (s.size() - 1);
ans = max(ans, now);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
signed main() {
int n, k;
cin >> n >> k;
vector<pair<int, ll>> v(n);
REP(i, n) { cin >> v[i].second >> v[i].first; }
sort(ALL(v));
reverse(ALL(v));
ll now = 0;
set<int> s;
multiset<ll> used;
for (int i = 0; i < k; i++) {
now += v[i].first;
if (s.count(v[i].second))
used.insert(v[i].first);
else
s.insert(v[i].second);
}
now += s.size() * s.size();
ll ans = now;
for (int i = k; i < n; i++) {
if (used.size() == 0)
break;
if (!s.count(v[i].second)) {
now -= *used.begin();
used.erase(used.begin());
s.insert(v[i].second);
now += v[i].first;
now += s.size() * s.size() - (s.size() - 1) * (s.size() - 1);
ans = max(ans, now);
}
}
cout << ans << endl;
return 0;
}
|
insert
| 30 | 30 | 30 | 32 |
TLE
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
bool checked[100001];
bool selected[100001];
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
long long int n, k, a, b;
long long int res = 0;
int x, y;
vector<pair<long long int, long long int>> v;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a >> b;
v.push_back(make_pair(b, a));
}
sort(v.rbegin(), v.rend());
// 1.
memset(checked, false, sizeof(checked));
long long int sum = 0;
long long int cnt = 0;
long long int count = 0;
for (int i = 0; i < n; i++) {
if (checked[v[i].second] == false) {
checked[v[i].second] = true;
cnt++;
}
sum += v[i].first;
count++;
if (count == k) {
break;
}
}
res = max(res, (cnt * cnt) + sum);
x = cnt;
// cout << cnt << '\n';
// 2.
memset(selected, false, sizeof(selected));
memset(checked, false, sizeof(checked));
sum = 0;
cnt = 0;
count = 0;
for (int i = 0; i < n; i++) {
if (checked[v[i].second] == false) {
checked[v[i].second] = true;
selected[i] = true;
sum += v[i].first;
cnt++;
count++;
}
if (count == k) {
break;
}
}
for (int i = 0; i < n; i++) {
if (count == k) {
break;
}
if (selected[i] == false) {
sum += v[i].first;
count++;
}
}
res = max(res, sum + (cnt * cnt));
y = cnt;
// cout << cnt << '\n';
for (int i = x + 1; i < y; i++) {
memset(selected, false, sizeof(selected));
memset(checked, false, sizeof(checked));
sum = 0;
cnt = 0;
count = 0;
for (int j = 0; j < n; j++) {
if (checked[v[j].second] == false) {
checked[v[j].second] = true;
selected[j] = true;
sum += v[j].first;
cnt++;
count++;
}
if (cnt == i || count == k) {
break;
}
}
for (int j = 0; j < n; j++) {
if (count == k) {
break;
}
if (checked[v[j].second] == true && selected[j] == false) {
sum += v[j].first;
count++;
}
}
res = max(res, cnt * cnt + sum);
}
cout << res << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool checked[100001];
bool selected[100001];
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
long long int n, k, a, b;
long long int res = 0;
int x, y;
vector<pair<long long int, long long int>> v;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a >> b;
v.push_back(make_pair(b, a));
}
sort(v.rbegin(), v.rend());
// 1.
memset(checked, false, sizeof(checked));
long long int sum = 0;
long long int cnt = 0;
long long int count = 0;
for (int i = 0; i < n; i++) {
if (checked[v[i].second] == false) {
checked[v[i].second] = true;
cnt++;
}
sum += v[i].first;
count++;
if (count == k) {
break;
}
}
res = max(res, (cnt * cnt) + sum);
x = cnt;
// cout << cnt << '\n';
// 2.
memset(selected, false, sizeof(selected));
memset(checked, false, sizeof(checked));
sum = 0;
cnt = 0;
count = 0;
for (int i = 0; i < n; i++) {
if (checked[v[i].second] == false) {
checked[v[i].second] = true;
selected[i] = true;
sum += v[i].first;
cnt++;
count++;
}
if (count == k) {
break;
}
}
for (int i = 0; i < n; i++) {
if (count == k) {
break;
}
if (selected[i] == false) {
sum += v[i].first;
count++;
}
}
res = max(res, sum + (cnt * cnt));
y = cnt;
// cout << cnt << '\n';
for (int i = x + 1; i < y; i++) {
memset(selected, false, sizeof(selected));
memset(checked, false, sizeof(checked));
sum = 0;
cnt = 0;
count = 0;
for (int j = 0; j < n; j++) {
if (checked[v[j].second] == false) {
checked[v[j].second] = true;
selected[j] = true;
sum += v[j].first;
cnt++;
count++;
}
if (cnt == i || count == k) {
break;
}
}
for (int j = 0; j < n; j++) {
if (count == k) {
break;
}
if (checked[v[j].second] == true && selected[j] == false) {
sum += v[j].first;
count++;
}
}
if (res > cnt * cnt + sum) {
break;
}
res = max(res, cnt * cnt + sum);
}
cout << res << '\n';
return 0;
}
|
insert
| 105 | 105 | 105 | 108 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, m, n) for (int i = m; i < n; i++)
ll mod = 1e9 + 7;
int main() {
int n, k;
cin >> n >> k;
vector<int> v[n];
rep(i, 0, n) {
int a, d;
cin >> a >> d;
v[a - 1].push_back(d);
}
rep(i, 0, n) { sort(v[i].begin(), v[i].end(), greater<int>()); }
priority_queue<int> t;
priority_queue<int> f;
rep(i, 0, n) {
int l = v[i].size();
rep(j, 0, l) {
if (j == 0)
t.push(v[i][j]);
else
f.push(v[i][j]);
}
}
priority_queue<int> tt;
priority_queue<int, vector<int>, greater<int>> ff;
ll sum = 0;
ll size = 0;
rep(i, 0, k) {
if (t.empty()) {
ff.push(f.top());
sum += f.top();
f.pop();
} else if (t.top() >= f.top()) {
tt.push(t.top());
sum += t.top();
size++;
t.pop();
} else {
ff.push(f.top());
sum += f.top();
f.pop();
}
}
ll ans = sum + size * size;
while (t.size() && ff.size()) {
tt.push(t.top());
sum += t.top();
size++;
t.pop();
sum -= ff.top();
ff.pop();
ans = max(ans, sum + size * size);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, m, n) for (int i = m; i < n; i++)
ll mod = 1e9 + 7;
int main() {
int n, k;
cin >> n >> k;
vector<int> v[n];
rep(i, 0, n) {
int a, d;
cin >> a >> d;
v[a - 1].push_back(d);
}
rep(i, 0, n) { sort(v[i].begin(), v[i].end(), greater<int>()); }
priority_queue<int> t;
priority_queue<int> f;
rep(i, 0, n) {
int l = v[i].size();
rep(j, 0, l) {
if (j == 0)
t.push(v[i][j]);
else
f.push(v[i][j]);
}
}
priority_queue<int> tt;
priority_queue<int, vector<int>, greater<int>> ff;
ll sum = 0;
ll size = 0;
rep(i, 0, k) {
if (t.empty()) {
ff.push(f.top());
sum += f.top();
f.pop();
} else if (f.empty()) {
tt.push(t.top());
sum += t.top();
size++;
t.pop();
} else if (t.top() >= f.top()) {
tt.push(t.top());
sum += t.top();
size++;
t.pop();
} else {
ff.push(f.top());
sum += f.top();
f.pop();
}
}
ll ans = sum + size * size;
while (t.size() && ff.size()) {
tt.push(t.top());
sum += t.top();
size++;
t.pop();
sum -= ff.top();
ff.pop();
ans = max(ans, sum + size * size);
}
cout << ans << endl;
}
|
insert
| 37 | 37 | 37 | 42 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector
int main() {
ll n, k;
cin >> n >> k;
ve<P> dt(n);
ve<ll> fl(n, 0);
rep(i, n) {
cin >> dt[i].second >> dt[i].first;
dt[i].second--;
}
sort(dt.rbegin(), dt.rend());
ll ans = 0;
ll cnt = 0;
ve<P> kouho;
rep(i, k) {
ans += dt[i].first;
if (fl[dt[i].second] == 0)
cnt++;
else
kouho.push_back(dt[i]);
fl[dt[i].second] += 1;
}
ans += cnt * cnt;
ve<ll> x;
x.push_back(ans);
ll i = k;
while (i < n) {
if (fl[dt[i].second] != 0) {
i++;
continue;
}
sort(kouho.begin(), kouho.end());
ans -= cnt * cnt;
ans -= kouho[0].first;
kouho.erase(kouho.begin());
cnt++;
ans += cnt * cnt;
ans += dt[i].first;
fl[dt[i].second] = 1;
x.push_back(ans);
i++;
}
sort(x.rbegin(), x.rend());
cout << x[0] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector
int main() {
ll n, k;
cin >> n >> k;
ve<P> dt(n);
ve<ll> fl(n, 0);
rep(i, n) {
cin >> dt[i].second >> dt[i].first;
dt[i].second--;
}
sort(dt.rbegin(), dt.rend());
ll ans = 0;
ll cnt = 0;
ve<P> kouho;
rep(i, k) {
ans += dt[i].first;
if (fl[dt[i].second] == 0)
cnt++;
else
kouho.push_back(dt[i]);
fl[dt[i].second] += 1;
}
ans += cnt * cnt;
ve<ll> x;
x.push_back(ans);
ll i = k;
while (i < n && !kouho.empty()) {
if (fl[dt[i].second] != 0) {
i++;
continue;
}
sort(kouho.begin(), kouho.end());
ans -= cnt * cnt;
ans -= kouho[0].first;
kouho.erase(kouho.begin());
cnt++;
ans += cnt * cnt;
ans += dt[i].first;
fl[dt[i].second] = 1;
x.push_back(ans);
i++;
}
sort(x.rbegin(), x.rend());
cout << x[0] << endl;
return 0;
}
|
replace
| 32 | 33 | 32 | 33 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<pair<int, int>> sushi(N);
for (int i = 0; i < N; ++i) {
int t, d;
cin >> t >> d;
--t;
sushi[i].first = d;
sushi[i].second = t;
}
sort(sushi.begin(), sushi.end(), greater<pair<int, int>>());
long long deliciousness = 0, variety = 0;
vector<bool> is_used(N, false);
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < K; ++i) {
deliciousness += sushi[i].first;
if (!is_used[sushi[i].second]) {
is_used[sushi[i].second] = true;
++variety;
} else
pq.push(sushi[i].first);
}
long long ans = deliciousness + variety * variety;
for (int i = K; i < N; ++i) {
if (!is_used[sushi[i].second]) {
int tmp = pq.top();
pq.pop();
is_used[sushi[i].second] = true;
deliciousness += sushi[i].first - tmp;
++variety;
}
ans = max(ans, deliciousness + variety * variety);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<pair<int, int>> sushi(N);
for (int i = 0; i < N; ++i) {
int t, d;
cin >> t >> d;
--t;
sushi[i].first = d;
sushi[i].second = t;
}
sort(sushi.begin(), sushi.end(), greater<pair<int, int>>());
long long deliciousness = 0, variety = 0;
vector<bool> is_used(N, false);
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < K; ++i) {
deliciousness += sushi[i].first;
if (!is_used[sushi[i].second]) {
is_used[sushi[i].second] = true;
++variety;
} else
pq.push(sushi[i].first);
}
long long ans = deliciousness + variety * variety;
for (int i = K; i < N; ++i) {
if (!is_used[sushi[i].second] and !pq.empty()) {
int tmp = pq.top();
pq.pop();
is_used[sushi[i].second] = true;
deliciousness += sushi[i].first - tmp;
++variety;
}
ans = max(ans, deliciousness + variety * variety);
}
cout << ans << endl;
return 0;
}
|
replace
| 34 | 35 | 34 | 35 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
int used[114514];
signed main() {
int n, k;
cin >> n >> k;
vector<pair<int, int>> sus;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
sus.pb(mp(y, x));
}
sort(sus.begin(), sus.end());
reverse(sus.begin(), sus.end());
vector<ll> fir;
priority_queue<ll> dup;
ll sum = 0;
for (int i = 0; i < k; i++) {
if (used[sus[i].second] == 0) {
used[sus[i].second] = 1;
fir.push_back(sus[i].first);
sum += sus[i].first;
} else {
dup.push(-sus[i].first);
sum += sus[i].first;
}
}
vector<ll> anss;
anss.pb(sum + fir.size() * fir.size());
for (int i = k; i < sus.size(); i++) {
if (used[sus[i].second] == 0) {
used[sus[i].second] = 1;
sum -= -dup.top();
dup.pop();
fir.push_back(-sus[i].first);
sum += sus[i].first;
anss.pb(sum + fir.size() * fir.size());
}
}
sort(anss.begin(), anss.end());
cout << anss[anss.size() - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
int used[114514];
signed main() {
int n, k;
cin >> n >> k;
vector<pair<int, int>> sus;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
sus.pb(mp(y, x));
}
sort(sus.begin(), sus.end());
reverse(sus.begin(), sus.end());
vector<ll> fir;
priority_queue<ll> dup;
ll sum = 0;
for (int i = 0; i < k; i++) {
if (used[sus[i].second] == 0) {
used[sus[i].second] = 1;
fir.push_back(sus[i].first);
sum += sus[i].first;
} else {
dup.push(-sus[i].first);
sum += sus[i].first;
}
}
vector<ll> anss;
anss.pb(sum + fir.size() * fir.size());
for (int i = k; i < sus.size(); i++) {
if (used[sus[i].second] == 0 && !dup.empty()) {
used[sus[i].second] = 1;
sum -= -dup.top();
dup.pop();
fir.push_back(-sus[i].first);
sum += sus[i].first;
anss.pb(sum + fir.size() * fir.size());
}
}
sort(anss.begin(), anss.end());
cout << anss[anss.size() - 1] << endl;
return 0;
}
|
replace
| 34 | 35 | 34 | 35 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define rFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define rrep(i, a) rFOR(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
ll to, cost;
};
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vP p(N);
rep(i, N) { cin >> p[i].second >> p[i].first; }
sort(p.begin(), p.end(), greater<P>());
set<int> type;
multiset<int> kaburi;
unsigned long long res = 0, ans = 0;
rep(i, N) {
if (i < K) {
if (type.count(p[i].second)) {
kaburi.insert(p[i].first);
res += p[i].first;
} else {
type.insert(p[i].second);
res += p[i].first;
}
} else {
if (type.count(p[i].second)) {
continue;
} else {
int x = *kaburi.begin();
res -= x;
kaburi.erase(kaburi.find(x));
res += p[i].first;
type.insert(p[i].second);
}
}
chmax(ans, res + type.size() * type.size());
}
cout << ans << '\n';
} /*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define rFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define rrep(i, a) rFOR(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
ll to, cost;
};
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vP p(N);
rep(i, N) { cin >> p[i].second >> p[i].first; }
sort(p.begin(), p.end(), greater<P>());
set<int> type;
multiset<int> kaburi;
unsigned long long res = 0, ans = 0;
rep(i, N) {
if (i < K) {
if (type.count(p[i].second)) {
kaburi.insert(p[i].first);
res += p[i].first;
} else {
type.insert(p[i].second);
res += p[i].first;
}
} else {
if (type.count(p[i].second)) {
continue;
} else {
int x = *kaburi.begin();
res -= x;
if (kaburi.size() == 0)
continue;
kaburi.erase(kaburi.find(x));
res += p[i].first;
type.insert(p[i].second);
}
}
chmax(ans, res + type.size() * type.size());
}
cout << ans << '\n';
} /*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/
|
insert
| 66 | 66 | 66 | 68 |
TLE
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
int u, p, d, n, tc, q, k, cnt;
char s[N];
ll ans;
vector<int> topping[N];
pair<int, int> a[N];
multiset<int> m, kind;
vector<pair<int, int>> v;
int main() {
// freopen("test.txt","r",stdin);
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d%d", &a[i].first, &a[i].second),
topping[--a[i].first].push_back(a[i].second);
ll del = 0;
for (int i = 0; i < n; i++) {
if (topping[i].size()) {
sort(topping[i].begin(), topping[i].end(), greater<int>());
v.push_back({topping[i][0], i});
}
}
sort(v.begin(), v.end(), greater<pair<int, int>>());
for (int ii = 0; ii < v.size() && ii < k; ii++) {
int i = v[ii].second;
del += topping[i][0];
if (m.size() + ii + 1 > k) {
del -= *m.begin();
m.erase(m.begin());
}
for (int j = 1; j < topping[i].size(); j++) {
if ((ii + 1 + (int)m.size()) == k) {
if (topping[i][j] > *m.begin()) {
del -= *m.begin();
m.erase(m.begin());
} else
break;
}
del += topping[i][j];
m.insert(topping[i][j]);
}
ll sz = ii + 1;
ans = max(ans, sz * sz + del);
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
int u, p, d, n, tc, q, k, cnt;
char s[N];
ll ans;
vector<int> topping[N];
pair<int, int> a[N];
multiset<int> m, kind;
vector<pair<int, int>> v;
int main() {
// freopen("test.txt","r",stdin);
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d%d", &a[i].first, &a[i].second),
topping[--a[i].first].push_back(a[i].second);
ll del = 0;
for (int i = 0; i < n; i++) {
if (topping[i].size()) {
sort(topping[i].begin(), topping[i].end(), greater<int>());
v.push_back({topping[i][0], i});
}
}
sort(v.begin(), v.end(), greater<pair<int, int>>());
for (int ii = 0; ii < v.size() && ii < k; ii++) {
int i = v[ii].second;
del += topping[i][0];
if (m.size() + ii + 1 > k) {
del -= *m.begin();
m.erase(m.begin());
}
for (int j = 1; j < topping[i].size(); j++) {
if ((ii + 1 + (int)m.size()) == k) {
if (!m.size())
break;
if (topping[i][j] > *m.begin()) {
del -= *m.begin();
m.erase(m.begin());
} else
break;
}
del += topping[i][j];
m.insert(topping[i][j]);
}
ll sz = ii + 1;
ans = max(ans, sz * sz + del);
}
printf("%lld\n", ans);
}
|
insert
| 45 | 45 | 45 | 47 |
TLE
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int t[100005];
ll d[100005];
vector<pair<ll, int>> vec;
int mp[100005];
struct sushi {
int ti;
ll di;
bool operator>(const sushi &n) const { return di > n.di; }
} eat;
int main() {
priority_queue<sushi, vector<sushi>, greater<sushi>> pque;
int N, K;
scanf("%d %d", &N, &K);
int i;
for (i = 0; i < N; i++) {
scanf("%d %lld", &t[i], &d[i]);
vec.push_back(make_pair(d[i], t[i]));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
ll sum = 0ll;
ll x = 0ll;
int pos = K;
for (i = 0; i < K; i++) {
sum += vec[i].first;
mp[vec[i].second]++;
eat.di = vec[i].first;
eat.ti = vec[i].second;
pque.push(eat);
}
for (i = 1; i <= N; i++) {
if (mp[i] > 0) {
x += 1ll;
}
}
ll ans = sum + (x * x);
sushi tmp, tmp2;
while (1) {
if (pos >= N) {
break;
}
tmp = pque.top();
pque.pop();
if (mp[tmp.ti] == 1) {
continue;
}
for (i = pos; i < N; i++) {
if (mp[vec[i].second] == 0) {
break;
}
}
if (i < N) {
ll score = (sum - tmp.di) + (vec[i].first) + ((x + 1ll) * (x + 1ll));
tmp2.di = vec[i].first;
tmp2.ti = vec[i].second;
pque.push(tmp2);
x += 1ll;
sum = sum - tmp.di + vec[i].first;
mp[tmp.ti]--;
mp[vec[i].second]++;
ans = max(ans, score);
pos = i;
} else {
break;
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int t[100005];
ll d[100005];
vector<pair<ll, int>> vec;
int mp[100005];
struct sushi {
int ti;
ll di;
bool operator>(const sushi &n) const { return di > n.di; }
} eat;
int main() {
priority_queue<sushi, vector<sushi>, greater<sushi>> pque;
int N, K;
scanf("%d %d", &N, &K);
int i;
for (i = 0; i < N; i++) {
scanf("%d %lld", &t[i], &d[i]);
vec.push_back(make_pair(d[i], t[i]));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
ll sum = 0ll;
ll x = 0ll;
int pos = K;
for (i = 0; i < K; i++) {
sum += vec[i].first;
mp[vec[i].second]++;
eat.di = vec[i].first;
eat.ti = vec[i].second;
pque.push(eat);
}
for (i = 1; i <= N; i++) {
if (mp[i] > 0) {
x += 1ll;
}
}
ll ans = sum + (x * x);
sushi tmp, tmp2;
while (1) {
if (pos >= N || pque.empty()) {
break;
}
tmp = pque.top();
pque.pop();
if (mp[tmp.ti] == 1) {
continue;
}
for (i = pos; i < N; i++) {
if (mp[vec[i].second] == 0) {
break;
}
}
if (i < N) {
ll score = (sum - tmp.di) + (vec[i].first) + ((x + 1ll) * (x + 1ll));
tmp2.di = vec[i].first;
tmp2.ti = vec[i].second;
pque.push(tmp2);
x += 1ll;
sum = sum - tmp.di + vec[i].first;
mp[tmp.ti]--;
mp[vec[i].second]++;
ans = max(ans, score);
pos = i;
} else {
break;
}
}
printf("%lld\n", ans);
return 0;
}
|
replace
| 65 | 66 | 65 | 66 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define ll long long
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<vector<ll>> v(N + 1);
ll sum = 0;
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
v[t].push_back(d);
sum += d;
}
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
pq_g;
priority_queue<ll, vector<ll>, greater<ll>> pq_f;
vector<int> cnt(N + 1, 0);
ll num = 0;
for (int i = 1; i <= N; ++i) {
if (v[i].size() > 0) {
++num;
sort(v[i].begin(), v[i].end(), greater<ll>());
if (v[i].size() == 1)
pq_f.push(v[i][0]);
else {
for (int j = 1; j < v[i].size(); ++j)
pq_g.push(make_pair(v[i][j], i));
}
}
}
for (int i = N; i > K; --i) {
if (pq_f.size() == 0) {
sum -= pq_g.top().first;
++cnt[pq_g.top().second];
if (cnt[pq_g.top().second] + 1 == v[pq_g.top().second].size()) {
pq_f.push(v[pq_g.top().second][0]);
}
pq_g.pop();
} else if (pq_g.top().first < 2 * num - 1 + pq_f.top()) {
sum -= pq_g.top().first;
++cnt[pq_g.top().second];
if (cnt[pq_g.top().second] + 1 == v[pq_g.top().second].size()) {
pq_f.push(v[pq_g.top().second][0]);
}
pq_g.pop();
} else {
sum -= pq_f.top();
--num;
pq_f.pop();
}
}
cout << sum + num * num << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define ll long long
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<vector<ll>> v(N + 1);
ll sum = 0;
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
v[t].push_back(d);
sum += d;
}
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
pq_g;
priority_queue<ll, vector<ll>, greater<ll>> pq_f;
vector<int> cnt(N + 1, 0);
ll num = 0;
for (int i = 1; i <= N; ++i) {
if (v[i].size() > 0) {
++num;
sort(v[i].begin(), v[i].end(), greater<ll>());
if (v[i].size() == 1)
pq_f.push(v[i][0]);
else {
for (int j = 1; j < v[i].size(); ++j)
pq_g.push(make_pair(v[i][j], i));
}
}
}
for (int i = N; i > K; --i) {
if (pq_f.size() == 0) {
sum -= pq_g.top().first;
++cnt[pq_g.top().second];
if (cnt[pq_g.top().second] + 1 == v[pq_g.top().second].size()) {
pq_f.push(v[pq_g.top().second][0]);
}
pq_g.pop();
} else if (pq_g.size() == 0) {
sum -= pq_f.top();
--num;
pq_f.pop();
} else if (pq_g.top().first < 2 * num - 1 + pq_f.top()) {
sum -= pq_g.top().first;
++cnt[pq_g.top().second];
if (cnt[pq_g.top().second] + 1 == v[pq_g.top().second].size()) {
pq_f.push(v[pq_g.top().second][0]);
}
pq_g.pop();
} else {
sum -= pq_f.top();
--num;
pq_f.pop();
}
}
cout << sum + num * num << endl;
return 0;
}
|
insert
| 45 | 45 | 45 | 49 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF bit(60)
#define pb push_back
#define mod 1000000007
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
int qp(int a, ll b) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
} while (b >>= 1);
return ans;
}
int qp(int a, ll b, int mo) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mo;
a = 1ll * a * a % mo;
} while (b >>= 1);
return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define FACSIZE 2002
ll invfac[FACSIZE];
ll fac[FACSIZE];
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
int main(void) {
ll ans = 0;
using pa = pair<ll, ll>;
ll n, k;
cin >> n >> k;
vector<ll> t(n);
vector<ll> d(n);
priority_queue<pa> pq;
priority_queue<pa, vector<pa>, greater<pa>> pq_remove_cand;
rep(i, n) {
cin >> t[i] >> d[i];
pq.push(make_pair(d[i], t[i]));
}
unordered_map<ll, ll> um;
unordered_map<ll, ll> um_kind;
ll score = 0;
ll kind = 0;
ll score_base = 0;
rep(i, k) {
auto q = pq.top();
// cout << q.first << "," << q.second << endl;
pq.pop();
pq_remove_cand.push(q);
score_base += q.first;
um[q.second]++;
}
kind = um.size();
// cout << "kind:" << kind << endl;
score = score_base + kind * kind;
ans = max(ans, score);
// cout << "ans:" << ans << endl;
while (!pq.empty()) {
auto q = pq.top();
pq.pop();
if (um.find(q.second) != end(um))
continue;
else {
// cout << "In:" << q.first << "," << q.second << endl;
pq_remove_cand.push(q);
um[q.second]++;
while (1) {
auto cq = pq_remove_cand.top();
pq_remove_cand.pop();
if (um[cq.second] > 1) {
// cout << "Out:" << cq.first << "," << cq.second << endl;
um[cq.second]--;
score = score + (kind + 1) * (kind + 1) - kind * kind + q.first -
cq.first;
ans = max(ans, score);
// cout << "ans:" << ans << endl;
kind++;
// cout << "kind:" << kind << endl;
break;
}
}
}
}
cout << ans << endl;
return 0;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % mod;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % mod;
return t * t % mod;
} else {
ll t = mod_pow(x, r / 2) % mod;
ll k = t * t % mod;
return k * x % mod;
}
}
ll mod_add(ll a, ll b) { return (a + b) % mod; }
ll mod_mul(ll a, ll b) { return (a * b) % mod; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }
void make_fact(ll n) {
fac[0] = 1;
rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], mod - 2);
for (int i = n - 1; i >= 0; i--) {
invfac[i] = mod_mul(invfac[i + 1], i + 1);
}
}
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF bit(60)
#define pb push_back
#define mod 1000000007
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
int qp(int a, ll b) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
} while (b >>= 1);
return ans;
}
int qp(int a, ll b, int mo) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mo;
a = 1ll * a * a % mo;
} while (b >>= 1);
return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define FACSIZE 2002
ll invfac[FACSIZE];
ll fac[FACSIZE];
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
int main(void) {
ll ans = 0;
using pa = pair<ll, ll>;
ll n, k;
cin >> n >> k;
vector<ll> t(n);
vector<ll> d(n);
priority_queue<pa> pq;
priority_queue<pa, vector<pa>, greater<pa>> pq_remove_cand;
rep(i, n) {
cin >> t[i] >> d[i];
pq.push(make_pair(d[i], t[i]));
}
unordered_map<ll, ll> um;
unordered_map<ll, ll> um_kind;
ll score = 0;
ll kind = 0;
ll score_base = 0;
rep(i, k) {
auto q = pq.top();
// cout << q.first << "," << q.second << endl;
pq.pop();
pq_remove_cand.push(q);
score_base += q.first;
um[q.second]++;
}
kind = um.size();
// cout << "kind:" << kind << endl;
score = score_base + kind * kind;
ans = max(ans, score);
// cout << "ans:" << ans << endl;
while (!pq.empty()) {
auto q = pq.top();
pq.pop();
if (um.find(q.second) != end(um))
continue;
else {
// cout << "In:" << q.first << "," << q.second << endl;
pq_remove_cand.push(q);
um[q.second]++;
while (!pq_remove_cand.empty()) {
auto cq = pq_remove_cand.top();
pq_remove_cand.pop();
if (um[cq.second] > 1) {
// cout << "Out:" << cq.first << "," << cq.second << endl;
um[cq.second]--;
score = score + (kind + 1) * (kind + 1) - kind * kind + q.first -
cq.first;
ans = max(ans, score);
// cout << "ans:" << ans << endl;
kind++;
// cout << "kind:" << kind << endl;
break;
}
}
}
}
cout << ans << endl;
return 0;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % mod;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % mod;
return t * t % mod;
} else {
ll t = mod_pow(x, r / 2) % mod;
ll k = t * t % mod;
return k * x % mod;
}
}
ll mod_add(ll a, ll b) { return (a + b) % mod; }
ll mod_mul(ll a, ll b) { return (a * b) % mod; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }
void make_fact(ll n) {
fac[0] = 1;
rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], mod - 2);
for (int i = n - 1; i >= 0; i--) {
invfac[i] = mod_mul(invfac[i + 1], i + 1);
}
}
|
replace
| 125 | 126 | 125 | 126 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T) \
for_each(begin(A), end(A), [&](T i) { cout << i << " "; }); \
cout << "\n"
#define MOD 1000000007
typedef long lint;
typedef pair<lint, lint> P;
bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool morePair(const P &l, const P &r) { return l.second > r.second; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
lint N;
cin >> N;
lint K;
cin >> K;
vector<P> sushi(N);
REP(i, N) {
lint t;
cin >> t;
lint d;
cin >> d;
sushi[i] = P(t, d);
}
sort(sushi.begin(), sushi.end(), morePair);
vector<lint> res(N + 1, 0);
vector<lint> best_kind(N + 1, 0);
vector<lint> rest;
lint min_kind = 0;
lint res_val = 0;
REP(i, K) {
lint kind = sushi[i].first;
lint val = sushi[i].second;
if (best_kind[kind] == 0) {
best_kind[kind] = val;
res[kind] = val;
min_kind++;
} else {
rest.push_back(val);
}
res_val += val;
}
res[min_kind] = res_val + min_kind * min_kind;
lint max_kind = min_kind;
FOR(i, K, N) {
lint kind = sushi[i].first;
lint val = sushi[i].second;
// PRINT(best_kind[kind]);
if (best_kind[kind] == 0) {
best_kind[kind] = val;
res_val += val;
res_val -= rest[rest.size() - 1];
rest.pop_back();
max_kind++;
res[max_kind] = res_val + max_kind * max_kind;
if (rest.size() == 0)
break;
} else {
}
}
// EACHPRINT(res, lint);
lint m = *max_element(res.begin() + min_kind, res.begin() + max_kind + 1);
PRINT(m);
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T) \
for_each(begin(A), end(A), [&](T i) { cout << i << " "; }); \
cout << "\n"
#define MOD 1000000007
typedef long lint;
typedef pair<lint, lint> P;
bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool morePair(const P &l, const P &r) { return l.second > r.second; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
lint N;
cin >> N;
lint K;
cin >> K;
vector<P> sushi(N);
REP(i, N) {
lint t;
cin >> t;
lint d;
cin >> d;
sushi[i] = P(t, d);
}
sort(sushi.begin(), sushi.end(), morePair);
vector<lint> res(N + 1, 0);
vector<lint> best_kind(N + 1, 0);
vector<lint> rest;
lint min_kind = 0;
lint res_val = 0;
REP(i, K) {
lint kind = sushi[i].first;
lint val = sushi[i].second;
if (best_kind[kind] == 0) {
best_kind[kind] = val;
res[kind] = val;
min_kind++;
} else {
rest.push_back(val);
}
res_val += val;
}
res[min_kind] = res_val + min_kind * min_kind;
lint max_kind = min_kind;
FOR(i, K, N) {
lint kind = sushi[i].first;
lint val = sushi[i].second;
if (rest.size() == 0)
break;
// PRINT(best_kind[kind]);
if (best_kind[kind] == 0) {
best_kind[kind] = val;
res_val += val;
res_val -= rest[rest.size() - 1];
rest.pop_back();
max_kind++;
res[max_kind] = res_val + max_kind * max_kind;
if (rest.size() == 0)
break;
} else {
}
}
// EACHPRINT(res, lint);
lint m = *max_element(res.begin() + min_kind, res.begin() + max_kind + 1);
PRINT(m);
return 0;
}
|
insert
| 72 | 72 | 72 | 75 |
0
| |
p03148
|
Python
|
Runtime Error
|
n, k = map(int, input().split())
td = list(
sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda td: -td[1])
)
td2 = []
eaten = set()
dup = 0
score = k * k
i = 0
j = 0
while len(eaten) + dup < k:
if td[i][0] in eaten:
td2.append(td[i])
i += 1
else:
diff = (k - dup) * (k - dup) - (k - dup - 1) * (k - dup - 1)
if i < n and (j >= len(td2) or td[i][1] >= td2[j][1] - diff):
t, d = td[i]
score += d
eaten.add(t)
i += 1
else:
t, d = td2[j]
score += d - diff
dup += 1
j += 1
print(score)
|
n, k = map(int, input().split())
td = list(
sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda td: -td[1])
)
td2 = []
eaten = set()
dup = 0
score = k * k
i = 0
j = 0
while len(eaten) + dup < k:
if i < n and td[i][0] in eaten:
td2.append(td[i])
i += 1
else:
diff = (k - dup) * (k - dup) - (k - dup - 1) * (k - dup - 1)
if i < n and (j >= len(td2) or td[i][1] >= td2[j][1] - diff):
t, d = td[i]
score += d
eaten.add(t)
i += 1
else:
t, d = td2[j]
score += d - diff
dup += 1
j += 1
print(score)
|
replace
| 11 | 12 | 11 | 12 |
0
| |
p03148
|
C++
|
Runtime Error
|
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
constexpr long long INF = 1LL << 60;
constexpr long long MOD = 1000000007;
double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
int ct = 0;
for (auto &s : st)
cout << s << (++ct != st.size() ? " " : "");
return os;
}
template <typename T>
constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
st1.insert(s);
return st1;
}
template <typename T>
constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
if (st1.count(s))
st1.erase(s);
return st1;
}
template <typename T>
constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) {
auto itr = st1.begin();
while (itr != st1.end()) {
if (!st2.count(*itr))
itr = st1.erase(itr);
else
++itr;
}
return st1;
}
template <typename T>
constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res |= st2;
return res;
}
template <typename T>
constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res -= st2;
return res;
}
template <typename T>
constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res &= st2;
return res;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N, K;
cin >> N >> K;
vector<P> sushi(N);
rep(i, N) {
ll t, d;
cin >> t >> d;
sushi[i] = {d, t};
}
sort(RALL(sushi));
ll sum = 0;
set<ll> used;
priority_queue<ll, vec, greater<>> pq;
rep(i, K) {
sum += sushi[i].first;
if (used.count(sushi[i].second)) {
pq.push(sushi[i].first);
} else
used.insert(sushi[i].second);
}
ll ans = sum + len(used) * len(used);
for (ll i = K; i < N; ++i) {
if (used.count(sushi[i].second))
continue;
sum -= pq.top();
sum += sushi[i].first;
pq.pop();
used.insert(sushi[i].second);
chmax(ans, sum + len(used) * len(used));
}
print(ans);
return 0;
}
|
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
constexpr long long INF = 1LL << 60;
constexpr long long MOD = 1000000007;
double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
int ct = 0;
for (auto &s : st)
cout << s << (++ct != st.size() ? " " : "");
return os;
}
template <typename T>
constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
st1.insert(s);
return st1;
}
template <typename T>
constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
if (st1.count(s))
st1.erase(s);
return st1;
}
template <typename T>
constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) {
auto itr = st1.begin();
while (itr != st1.end()) {
if (!st2.count(*itr))
itr = st1.erase(itr);
else
++itr;
}
return st1;
}
template <typename T>
constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res |= st2;
return res;
}
template <typename T>
constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res -= st2;
return res;
}
template <typename T>
constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res &= st2;
return res;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N, K;
cin >> N >> K;
vector<P> sushi(N);
rep(i, N) {
ll t, d;
cin >> t >> d;
sushi[i] = {d, t};
}
sort(RALL(sushi));
ll sum = 0;
set<ll> used;
priority_queue<ll, vec, greater<>> pq;
rep(i, K) {
sum += sushi[i].first;
if (used.count(sushi[i].second)) {
pq.push(sushi[i].first);
} else
used.insert(sushi[i].second);
}
ll ans = sum + len(used) * len(used);
for (ll i = K; i < N; ++i) {
if (used.count(sushi[i].second))
continue;
if (pq.empty())
continue;
sum -= pq.top();
sum += sushi[i].first;
pq.pop();
used.insert(sushi[i].second);
chmax(ans, sum + len(used) * len(used));
}
print(ans);
return 0;
}
|
insert
| 228 | 228 | 228 | 230 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 10010;
struct node {
ll v, x;
} a[M];
ll vis[M];
bool cmp(node x, node y) { return x.v > y.v; }
int n, k;
ll cnt, tp, res, ans;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int a1, b;
cin >> a1 >> b;
a[i].x = a1;
a[i].v = b;
}
sort(a + 1, a + n + 1, cmp);
stack<ll> s;
for (int i = 1; i <= n; i++) {
if (cnt < k) {
if (vis[a[i].x] == 0) {
vis[a[i].x] = 1;
tp++;
} else {
s.push(a[i].v);
}
res += a[i].v;
cnt++;
ans = max(ans, res + 1ll * tp * tp);
} else {
if (s.empty())
break;
if (vis[a[i].x])
continue;
vis[a[i].x] = 1;
tp++;
res -= s.top();
res += a[i].v;
s.pop();
ans = max(ans, res + tp * tp);
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 100010;
struct node {
ll v, x;
} a[M];
ll vis[M];
bool cmp(node x, node y) { return x.v > y.v; }
int n, k;
ll cnt, tp, res, ans;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int a1, b;
cin >> a1 >> b;
a[i].x = a1;
a[i].v = b;
}
sort(a + 1, a + n + 1, cmp);
stack<ll> s;
for (int i = 1; i <= n; i++) {
if (cnt < k) {
if (vis[a[i].x] == 0) {
vis[a[i].x] = 1;
tp++;
} else {
s.push(a[i].v);
}
res += a[i].v;
cnt++;
ans = max(ans, res + 1ll * tp * tp);
} else {
if (s.empty())
break;
if (vis[a[i].x])
continue;
vis[a[i].x] = 1;
tp++;
res -= s.top();
res += a[i].v;
s.pop();
ans = max(ans, res + tp * tp);
}
}
printf("%lld\n", ans);
return 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#include <cinttypes>
using namespace std;
using ll = long long;
const int NMAX = 100010;
int used[NMAX];
pair<int, int> dt[NMAX];
ll f[NMAX];
int main() {
ios::sync_with_stdio(false);
int N, K;
scanf("%d %d", &N, &K);
// vector<pair<int, int>> dt(N);
// vector<int> used(N+1, 0);
fill(used, used + N + 1, 0);
// vector<ll> f(N+1, -1LL);
fill(f, f + N + 1, -1);
for (int i = 0; i < N; i++)
scanf("%d %d", &(dt[i].second), &(dt[i].first)); // first = d, second = t;
sort(dt, dt + N, greater<pair<int, int>>());
ll goodness = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
int used_cnt = 0;
for (int i = 0; i < K; i++) {
que.push(dt[i]);
goodness += dt[i].first;
if (used[dt[i].second] == false) {
used_cnt++;
}
used[dt[i].second]++;
}
f[used_cnt] = goodness;
for (int i = K; i < N; i++) {
if (used[dt[i].second] > 0)
continue;
while (used[que.top().second] == 1) {
que.pop();
}
if (que.empty()) {
break;
}
pair<int, int> q = que.top();
que.pop();
goodness -= q.first;
goodness += dt[i].first;
used[dt[i].second]++;
used_cnt++;
f[used_cnt] = goodness;
}
ll ans = 0;
for (int i = 1; i <= N; i++) {
if (f[i] == -1)
continue;
ans = max(ans, f[i] + (ll)i * (ll)i);
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#include <cinttypes>
using namespace std;
using ll = long long;
const int NMAX = 100010;
int used[NMAX];
pair<int, int> dt[NMAX];
ll f[NMAX];
int main() {
ios::sync_with_stdio(false);
int N, K;
scanf("%d %d", &N, &K);
// vector<pair<int, int>> dt(N);
// vector<int> used(N+1, 0);
fill(used, used + N + 1, 0);
// vector<ll> f(N+1, -1LL);
fill(f, f + N + 1, -1);
for (int i = 0; i < N; i++)
scanf("%d %d", &(dt[i].second), &(dt[i].first)); // first = d, second = t;
sort(dt, dt + N, greater<pair<int, int>>());
ll goodness = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
int used_cnt = 0;
for (int i = 0; i < K; i++) {
que.push(dt[i]);
goodness += dt[i].first;
if (used[dt[i].second] == false) {
used_cnt++;
}
used[dt[i].second]++;
}
f[used_cnt] = goodness;
for (int i = K; i < N; i++) {
if (used[dt[i].second] > 0)
continue;
while (!que.empty() && used[que.top().second] == 1) {
que.pop();
}
if (que.empty()) {
break;
}
pair<int, int> q = que.top();
que.pop();
goodness -= q.first;
goodness += dt[i].first;
used[dt[i].second]++;
used_cnt++;
f[used_cnt] = goodness;
}
ll ans = 0;
for (int i = 1; i <= N; i++) {
if (f[i] == -1)
continue;
ans = max(ans, f[i] + (ll)i * (ll)i);
}
cout << ans << '\n';
return 0;
}
|
replace
| 44 | 45 | 44 | 45 |
TLE
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
int main() {
ll n, k;
cin >> n >> k;
priority_queue<P> pq;
rep(i, n) {
ll t, d;
cin >> t >> d;
t--;
pq.emplace(P(d, t));
}
vl sushi(n, 0);
ll sum = 0;
ll x = 0;
multiset<ll> mse;
rep(i, k) {
auto p = pq.top();
pq.pop();
sum += p.first;
ll u = p.second;
if (sushi[u] == 0) {
sushi[u]++;
x++;
} else
mse.insert(p.first);
}
ll ans = sum + x * x;
while (x < k && !mse.empty()) {
auto p = pq.top();
pq.pop();
ll u = p.second;
if (sushi[u] > 0)
continue;
sushi[u]++;
sum -= *mse.begin();
mse.erase(mse.begin());
sum += p.first;
x++;
ans = max(ans, sum + x * x);
}
cout << ans << endl;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
int main() {
ll n, k;
cin >> n >> k;
priority_queue<P> pq;
rep(i, n) {
ll t, d;
cin >> t >> d;
t--;
pq.emplace(P(d, t));
}
vl sushi(n, 0);
ll sum = 0;
ll x = 0;
multiset<ll> mse;
rep(i, k) {
auto p = pq.top();
pq.pop();
sum += p.first;
ll u = p.second;
if (sushi[u] == 0) {
sushi[u]++;
x++;
} else
mse.insert(p.first);
}
ll ans = sum + x * x;
while (x < k && !mse.empty() && !pq.empty()) {
auto p = pq.top();
pq.pop();
ll u = p.second;
if (sushi[u] > 0)
continue;
sushi[u]++;
sum -= *mse.begin();
mse.erase(mse.begin());
sum += p.first;
x++;
ans = max(ans, sum + x * x);
}
cout << ans << endl;
}
|
replace
| 50 | 51 | 50 | 51 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
using P = pair<ll, ll>;
template <typename T> vector<T> cumulative_sum(vector<T> &v) {
vector<T> sum(v.size() + 1);
rep(i, v.size()) { sum[i + 1] = sum[i] + v[i]; }
return sum;
}
int main(void) {
int n;
ll K;
cin >> n >> K;
vector<P> vp(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
vp[i] = {d, t};
}
sort(all(vp), greater<P>());
map<ll, bool> used;
vector<ll> top, rem;
rep(i, n) {
ll t = vp[i].second;
ll d = vp[i].first;
if (used[t]) {
rem.push_back(d);
} else {
used[t] = true;
top.push_back(d);
}
}
auto top_sum = cumulative_sum(top);
auto rem_sum = cumulative_sum(rem);
ll res = 0;
for (ll x = 1; x <= K; x++) {
if (K < x)
continue;
if (K - x >= (ll)rem_sum.size())
continue;
res = max(res, top_sum[x] + rem_sum[K - x] + x * x);
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
using P = pair<ll, ll>;
template <typename T> vector<T> cumulative_sum(vector<T> &v) {
vector<T> sum(v.size() + 1);
rep(i, v.size()) { sum[i + 1] = sum[i] + v[i]; }
return sum;
}
int main(void) {
int n;
ll K;
cin >> n >> K;
vector<P> vp(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
vp[i] = {d, t};
}
sort(all(vp), greater<P>());
map<ll, bool> used;
vector<ll> top, rem;
rep(i, n) {
ll t = vp[i].second;
ll d = vp[i].first;
if (used[t]) {
rem.push_back(d);
} else {
used[t] = true;
top.push_back(d);
}
}
auto top_sum = cumulative_sum(top);
auto rem_sum = cumulative_sum(rem);
ll res = 0;
for (ll x = 1; x <= K; x++) {
if (x >= (ll)top_sum.size())
continue;
if (K < x)
continue;
if (K - x >= (ll)rem_sum.size())
continue;
res = max(res, top_sum[x] + rem_sum[K - x] + x * x);
}
cout << res << endl;
return 0;
}
|
insert
| 40 | 40 | 40 | 42 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
ll N, K;
cin >> N >> K;
vector<array<ll, 2>> td(N);
for (int i = 0; i < N; i++) {
cin >> td[i][1] >> td[i][0];
}
sort(td.begin(), td.end(), greater<array<ll, 2>>());
ll res = 0;
ll vsum = 0;
set<ll> kind;
stack<ll> unused;
for (int i = 0; i < K; i++) {
auto t = td[i][1];
auto d = td[i][0];
vsum += d;
;
if (kind.count(t) > 0)
unused.push(d);
else
kind.insert(t);
}
res = vsum + kind.size() * kind.size();
for (int i = K; i < N; i++) {
auto t = td[i][1];
auto d = td[i][0];
if (kind.count(t) > 0)
continue;
kind.insert(t);
vsum += d;
vsum -= unused.top();
unused.pop();
res = max(res, (ll)(vsum + kind.size() * kind.size()));
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
ll N, K;
cin >> N >> K;
vector<array<ll, 2>> td(N);
for (int i = 0; i < N; i++) {
cin >> td[i][1] >> td[i][0];
}
sort(td.begin(), td.end(), greater<array<ll, 2>>());
ll res = 0;
ll vsum = 0;
set<ll> kind;
stack<ll> unused;
for (int i = 0; i < K; i++) {
auto t = td[i][1];
auto d = td[i][0];
vsum += d;
;
if (kind.count(t) > 0)
unused.push(d);
else
kind.insert(t);
}
res = vsum + kind.size() * kind.size();
for (int i = K; i < N; i++) {
auto t = td[i][1];
auto d = td[i][0];
if (kind.count(t) > 0)
continue;
kind.insert(t);
vsum += d;
if (unused.size() == 0)
break;
vsum -= unused.top();
unused.pop();
res = max(res, (ll)(vsum + kind.size() * kind.size()));
}
cout << res << endl;
return 0;
}
|
insert
| 37 | 37 | 37 | 39 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define EPS (1e-20)
#define MOD (int)(1e9 + 7)
#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX 210000
int N, K;
int used[MAX];
int temp[MAX], temp2[MAX], temp3[MAX];
priority_queue<int> td[MAX];
priority_queue<int> fuga;
vector<pair<int, int>> hoge;
vector<int> type;
priority_queue<int> hogehoge;
signed main() {
cout << fixed << setprecision(7);
set<int> tt;
set<int> ttt;
int count = 0;
int t, d;
int ans = 0;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> t >> d;
hoge.push_back(make_pair(d, t));
tt.insert(t);
}
sort(hoge.begin(), hoge.end());
for (int i = 0; i < K; i++) {
temp[i] += hoge[N - i - 1].first;
if (used[hoge[N - i - 1].second] == 0) {
temp2[i]++;
used[hoge[N - i - 1].second] = i + 1;
temp3[i] = hoge[N - i - 1].second;
count++;
}
temp[i + 1] += temp[i];
temp2[i + 1] += temp2[i];
// cout<<i<<" temp = "<<temp[i]<<" "<<hoge[N-i-1].first<<"
// "<<hoge[N-i-1].second<<endl;
}
for (int i = N - K - 1; i >= 0; i--) {
td[hoge[i].second].push(hoge[i].first);
// cout<<i<<" td "<<hoge[i].second<<" "<<hoge[i].first<<endl;
}
// cout<<"XXX "<<endl;
for (int y : tt) { // cout<<y<<" "<<" "<<tt.size()<<" "<<used[y]<<endl;
if (used[y])
continue;
if (!td[y].empty())
hogehoge.push(td[y].top());
}
// cout<<"YYY "<<endl;
ans = temp[K - 1] + temp2[K - 1] * temp2[K - 1];
// for(int i = 0; i < K; i++){
// cout<<i<<" temp "<<temp[i]<<endl;
// }
for (int i = K - 1, xx = 0, tete = tt.size(); i >= 1;
i--) { // cout<<i<<" "<<temp[i]<<" "<<xx<<endl;
// cout<<temp2[i-1]<<" "<<temp2[i]+K-i<<" tete "<<tete<<endl;
if (temp3[i]) {
xx += temp[i] - temp[i - 1];
} else {
xx += hogehoge.top();
hogehoge.pop();
}
ans =
max(temp[i - 1] + xx + (temp2[i - 1] + K - i) * (temp2[i - 1] + K - i),
ans);
// cout<<" end "<<xx<<" temp[i]-> "<<temp[i-1]<<" ans = "<<ans<<endl;
if (temp2[i - 1] + K - i == tete) {
break;
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define EPS (1e-20)
#define MOD (int)(1e9 + 7)
#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX 210000
int N, K;
int used[MAX];
int temp[MAX], temp2[MAX], temp3[MAX];
priority_queue<int> td[MAX];
priority_queue<int> fuga;
vector<pair<int, int>> hoge;
vector<int> type;
priority_queue<int> hogehoge;
signed main() {
cout << fixed << setprecision(7);
set<int> tt;
set<int> ttt;
int count = 0;
int t, d;
int ans = 0;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> t >> d;
hoge.push_back(make_pair(d, t));
tt.insert(t);
}
sort(hoge.begin(), hoge.end());
for (int i = 0; i < K; i++) {
temp[i] += hoge[N - i - 1].first;
if (used[hoge[N - i - 1].second] == 0) {
temp2[i]++;
used[hoge[N - i - 1].second] = i + 1;
temp3[i] = hoge[N - i - 1].second;
count++;
}
temp[i + 1] += temp[i];
temp2[i + 1] += temp2[i];
// cout<<i<<" temp = "<<temp[i]<<" "<<hoge[N-i-1].first<<"
// "<<hoge[N-i-1].second<<endl;
}
for (int i = N - K - 1; i >= 0; i--) {
td[hoge[i].second].push(hoge[i].first);
// cout<<i<<" td "<<hoge[i].second<<" "<<hoge[i].first<<endl;
}
// cout<<"XXX "<<endl;
for (int y : tt) { // cout<<y<<" "<<" "<<tt.size()<<" "<<used[y]<<endl;
if (used[y])
continue;
if (!td[y].empty())
hogehoge.push(td[y].top());
}
// cout<<"YYY "<<endl;
ans = temp[K - 1] + temp2[K - 1] * temp2[K - 1];
// for(int i = 0; i < K; i++){
// cout<<i<<" temp "<<temp[i]<<endl;
// }
for (int i = K - 1, xx = 0, tete = tt.size(); i >= 1;
i--) { // cout<<i<<" "<<temp[i]<<" "<<xx<<endl;
// cout<<temp2[i-1]<<" "<<temp2[i]+K-i<<" tete "<<tete<<endl;
if (temp3[i]) {
xx += temp[i] - temp[i - 1];
} else {
if (hogehoge.size() == 0)
break;
xx += hogehoge.top();
hogehoge.pop();
}
ans =
max(temp[i - 1] + xx + (temp2[i - 1] + K - i) * (temp2[i - 1] + K - i),
ans);
// cout<<" end "<<xx<<" temp[i]-> "<<temp[i-1]<<" ans = "<<ans<<endl;
if (temp2[i - 1] + K - i == tete) {
break;
}
}
cout << ans << endl;
return 0;
}
|
insert
| 90 | 90 | 90 | 92 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int INF = 1e9 + 7;
int main() {
int N, K;
cin >> N >> K;
priority_queue<ll> kind[N];
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
kind[t - 1].push(d);
}
priority_queue<ll> best, remain;
// best:各種類で一番おいしいやつを集めたもの
// remain:bestの余り
for (int i = 0; i < N; ++i) {
if (!kind[i].empty()) {
best.push(kind[i].top());
kind[i].pop();
while (!kind[i].empty())
;
{
remain.push(kind[i].top());
kind[i].pop();
}
}
}
vector<ll> bsum = {0};
// bestの累積和(bsum[i]=bestの先頭からi個の総和)
while (!best.empty()) {
bsum.push_back(bsum.back() + best.top());
best.pop();
}
vector<ll> rsum = {0};
// remainの累積和
while (!remain.empty()) {
rsum.push_back(rsum.back() + remain.top());
remain.pop();
}
ll ans = 0;
for (ll x = 1; x <= K; ++x) {
// 種類が多すぎるケース
if (x >= bsum.size())
break;
// 種類が少なすぎて余り物が足りないケース
if (K - x >= rsum.size())
continue;
// bestからx個、remainからK-x個貪欲に選ぶ
ans = max(ans, bsum[x] + rsum[K - x] + x * x);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int INF = 1e9 + 7;
int main() {
int N, K;
cin >> N >> K;
priority_queue<ll> kind[N];
for (int i = 0; i < N; ++i) {
int t;
ll d;
cin >> t >> d;
kind[t - 1].push(d);
}
priority_queue<ll> best, remain;
// best:各種類で一番おいしいやつを集めたもの
// remain:bestの余り
for (int i = 0; i < N; ++i) {
if (!kind[i].empty()) {
best.push(kind[i].top());
kind[i].pop();
while (!kind[i].empty()) {
remain.push(kind[i].top());
kind[i].pop();
}
}
}
vector<ll> bsum = {0};
// bestの累積和(bsum[i]=bestの先頭からi個の総和)
while (!best.empty()) {
bsum.push_back(bsum.back() + best.top());
best.pop();
}
vector<ll> rsum = {0};
// remainの累積和
while (!remain.empty()) {
rsum.push_back(rsum.back() + remain.top());
remain.pop();
}
ll ans = 0;
for (ll x = 1; x <= K; ++x) {
// 種類が多すぎるケース
if (x >= bsum.size())
break;
// 種類が少なすぎて余り物が足りないケース
if (K - x >= rsum.size())
continue;
// bestからx個、remainからK-x個貪欲に選ぶ
ans = max(ans, bsum[x] + rsum[K - x] + x * x);
}
cout << ans << endl;
return 0;
}
|
replace
| 39 | 42 | 39 | 40 |
TLE
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<P> v;
rep(i, n) {
int t;
ll d;
cin >> t >> d;
v.emplace_back(d, t - 1);
}
sort(v.begin(), v.end(), greater<>());
ll sum = 0, type = 0;
vector<int> cnt(n, 0);
rep(i, k) {
sum += v[i].first;
if (cnt[v[i].second]++ == 0)
++type;
}
ll ans = sum + type * type;
for (int i = k, j = k - 1; i < n; ++i) {
if (cnt[v[i].second] > 0)
continue;
++cnt[v[i].second];
sum += v[i].first;
++type;
while (j >= 0) {
if (cnt[v[j].second] > 1) {
--cnt[v[j].second];
sum -= v[j].first;
chmax(ans, sum + type * type);
--j;
break;
}
}
if (j < 0)
break;
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<P> v;
rep(i, n) {
int t;
ll d;
cin >> t >> d;
v.emplace_back(d, t - 1);
}
sort(v.begin(), v.end(), greater<>());
ll sum = 0, type = 0;
vector<int> cnt(n, 0);
rep(i, k) {
sum += v[i].first;
if (cnt[v[i].second]++ == 0)
++type;
}
ll ans = sum + type * type;
for (int i = k, j = k - 1; i < n; ++i) {
if (cnt[v[i].second] > 0)
continue;
++cnt[v[i].second];
sum += v[i].first;
++type;
while (j >= 0) {
if (cnt[v[j].second] > 1) {
--cnt[v[j].second];
sum -= v[j].first;
chmax(ans, sum + type * type);
--j;
break;
}
--j;
}
if (j < 0)
break;
}
cout << ans << endl;
return 0;
}
|
insert
| 55 | 55 | 55 | 56 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<P> sushi;
rep(i, n) {
int t;
ll d;
cin >> t >> d;
sushi.emplace_back(d, t);
}
sort(sushi.rbegin(), sushi.rend());
ll sum = 0, ans = 0, kind = 0;
priority_queue<P> pq;
vector<int> cnt(n + 1, 0);
rep(i, k) {
++kind;
if (cnt[sushi[i].second]++ > 0) {
pq.push(P(-sushi[i].first, sushi[i].second));
--kind;
}
sum += sushi[i].first;
}
ans = sum + kind * kind;
for (int i = k; i < n; ++i) {
if (cnt[sushi[i].second] > 0)
continue;
ll d = -pq.top().first;
int t = pq.top().second;
pq.pop();
sum = sum - d + sushi[i].first;
++cnt[sushi[i].second];
--cnt[t];
++kind;
chmax(ans, sum + kind * kind);
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<P> sushi;
rep(i, n) {
int t;
ll d;
cin >> t >> d;
sushi.emplace_back(d, t);
}
sort(sushi.rbegin(), sushi.rend());
ll sum = 0, ans = 0, kind = 0;
priority_queue<P> pq;
vector<int> cnt(n + 1, 0);
rep(i, k) {
++kind;
if (cnt[sushi[i].second]++ > 0) {
pq.push(P(-sushi[i].first, sushi[i].second));
--kind;
}
sum += sushi[i].first;
}
ans = sum + kind * kind;
for (int i = k; i < n; ++i) {
if (cnt[sushi[i].second] > 0)
continue;
if (pq.empty())
break;
ll d = -pq.top().first;
int t = pq.top().second;
pq.pop();
sum = sum - d + sushi[i].first;
++cnt[sushi[i].second];
--cnt[t];
++kind;
chmax(ans, sum + kind * kind);
}
cout << ans << endl;
return 0;
}
|
insert
| 48 | 48 | 48 | 50 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <functional>
#include <stack>
#include <stdio.h>
#include <utility>
typedef long long ll;
using namespace std;
int main(void) {
ll i, j, k, n, ans, now = 0, cnt = 0;
scanf("%lld%lld", &n, &k);
pair<ll, ll> s[n];
bool f[n];
for (i = 0; i < n; ++i)
scanf("%lld%lld", &s[i].second, &s[i].first), s[i].second--, f[i] = true;
sort(s, s + n, greater<pair<ll, ll>>());
stack<ll> st;
for (i = 0; i < k; ++i) {
if (f[s[i].second]) {
f[s[i].second] = false;
cnt++;
} else
st.push(s[i].first);
now += s[i].first;
}
ans = now + cnt * cnt;
for (; i < n; ++i)
if (f[s[i].second]) {
now -= st.top();
st.pop();
now += s[i].first;
cnt++;
f[s[i].second] = false;
if (now + cnt * cnt > ans)
ans = now + cnt * cnt;
}
printf("%lld", ans);
return 0;
}
|
#include <algorithm>
#include <functional>
#include <stack>
#include <stdio.h>
#include <utility>
typedef long long ll;
using namespace std;
int main(void) {
ll i, j, k, n, ans, now = 0, cnt = 0;
scanf("%lld%lld", &n, &k);
pair<ll, ll> s[n];
bool f[n];
for (i = 0; i < n; ++i)
scanf("%lld%lld", &s[i].second, &s[i].first), s[i].second--, f[i] = true;
sort(s, s + n, greater<pair<ll, ll>>());
stack<ll> st;
for (i = 0; i < k; ++i) {
if (f[s[i].second]) {
f[s[i].second] = false;
cnt++;
} else
st.push(s[i].first);
now += s[i].first;
}
ans = now + cnt * cnt;
for (; i < n && st.size(); ++i)
if (f[s[i].second]) {
now -= st.top();
st.pop();
now += s[i].first;
cnt++;
f[s[i].second] = false;
if (now + cnt * cnt > ans)
ans = now + cnt * cnt;
}
printf("%lld", ans);
return 0;
}
|
replace
| 26 | 27 | 26 | 27 |
0
| |
p03148
|
C++
|
Runtime Error
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
template <class T, class U> constexpr bool chmin(T &a, const U b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, class U> constexpr bool chmax(T &a, const U b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T, class U> constexpr bool bit(const T n, const U k) {
return (n >> k) & 1;
}
// デバッグ
template <class T> void Vout(vector<T> &V) {
cout << "start" << endl;
const int sz = V.size();
for (int i = 0; i < sz; i++) {
cout << i << " " << V[i] << '\n';
}
cout << "end" << endl;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
constexpr int MAX = 1 << 30;
constexpr ll INF = 1LL << 62;
constexpr int MOD = 1e9 + 7;
//__builtin_popcount(S);
// #define int ll
// vector<vector<int>> data(3, vector<int>(4));
// vector.resize(a,vector<int>(b,-1));
// vector<vector<vector<要素の型>>> 変数名(要素数1,
// vector<vector<要素の型>>(要素数2, vector<要素の型>(要素数3, 初期値)));
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<vector<ll>> V(N + 1, vector<ll>(0));
for (int i = 0; i < N; i++) {
int t;
ll d;
cin >> t >> d;
V[t].push_back(d);
}
for (int i = 0; i <= N; i++) {
sort(V[i].rbegin(), V[i].rend());
}
vector<ll> ne, ol;
for (int i = 0; i <= N; i++) {
for (int j = 0; j < (int)V[i].size(); j++) {
if (j == 0)
ne.push_back(V[i][j]);
else
ol.push_back(V[i][j]);
}
}
sort(ne.rbegin(), ne.rend());
sort(ol.rbegin(), ol.rend());
for (int i = 1; i < (int)ne.size(); i++)
ne[i] += ne[i - 1];
for (int i = 1; i < (int)ol.size(); i++)
ol[i] += ol[i - 1];
ll ans = 0;
for (int i = 1; i <= K; i++) {
if (ne.size() < i)
break;
ll x = ne[i - 1];
if (i != K)
x += ol[min(K - i - 1, (int)ol.size() - 1)];
chmax(ans, x + (ll)i * i);
}
cout << ans << endl;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
template <class T, class U> constexpr bool chmin(T &a, const U b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, class U> constexpr bool chmax(T &a, const U b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T, class U> constexpr bool bit(const T n, const U k) {
return (n >> k) & 1;
}
// デバッグ
template <class T> void Vout(vector<T> &V) {
cout << "start" << endl;
const int sz = V.size();
for (int i = 0; i < sz; i++) {
cout << i << " " << V[i] << '\n';
}
cout << "end" << endl;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
constexpr int MAX = 1 << 30;
constexpr ll INF = 1LL << 62;
constexpr int MOD = 1e9 + 7;
//__builtin_popcount(S);
// #define int ll
// vector<vector<int>> data(3, vector<int>(4));
// vector.resize(a,vector<int>(b,-1));
// vector<vector<vector<要素の型>>> 変数名(要素数1,
// vector<vector<要素の型>>(要素数2, vector<要素の型>(要素数3, 初期値)));
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<vector<ll>> V(N + 1, vector<ll>(0));
for (int i = 0; i < N; i++) {
int t;
ll d;
cin >> t >> d;
V[t].push_back(d);
}
for (int i = 0; i <= N; i++) {
sort(V[i].rbegin(), V[i].rend());
}
vector<ll> ne, ol;
for (int i = 0; i <= N; i++) {
for (int j = 0; j < (int)V[i].size(); j++) {
if (j == 0)
ne.push_back(V[i][j]);
else
ol.push_back(V[i][j]);
}
}
sort(ne.rbegin(), ne.rend());
sort(ol.rbegin(), ol.rend());
for (int i = 1; i < (int)ne.size(); i++)
ne[i] += ne[i - 1];
for (int i = 1; i < (int)ol.size(); i++)
ol[i] += ol[i - 1];
ll ans = 0;
for (int i = 1; i <= K; i++) {
if (ne.size() < i)
break;
ll x = ne[i - 1];
if (i != K && (int)ol.size() > 0)
x += ol[min(K - i - 1, (int)ol.size() - 1)];
chmax(ans, x + (ll)i * i);
}
cout << ans << endl;
}
|
replace
| 90 | 91 | 90 | 91 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define FOR(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define RFOR(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)
template <typename T> bool setmin(T &a, T b) {
if (b < a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmax(T &a, T b) {
if (b > a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmineq(T &a, T b) {
if (b <= a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmaxeq(T &a, T b) {
if (b >= a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ---- ----
int main() {
LL n, k;
cin >> n >> k;
vector<pair<LL, LL>> v(n);
inc(i, n) { cin >> v[i].FI >> v[i].SE; }
sort(RALL(v));
vector<LL> x, y;
inc(i, v.size()) { (i == 0 || v[i].FI != v[i - 1].FI ? x : y).PB(v[i].SE); }
sort(RALL(x));
sort(RALL(y));
vector<LL> sx(k + 1), sy(k + 1);
inc(i, x.size()) { sx[i + 1] = sx[i] + x[i]; }
inc(i, y.size()) { sy[i + 1] = sy[i] + y[i]; }
LL ans = 0;
incII(i, 0, k) { setmax(ans, sx[i] + sy[k - i] + 1LL * i * i); }
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define FOR(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define RFOR(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)
template <typename T> bool setmin(T &a, T b) {
if (b < a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmax(T &a, T b) {
if (b > a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmineq(T &a, T b) {
if (b <= a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> bool setmaxeq(T &a, T b) {
if (b >= a) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ---- ----
int main() {
LL n, k;
cin >> n >> k;
vector<pair<LL, LL>> v(n);
inc(i, n) { cin >> v[i].FI >> v[i].SE; }
sort(RALL(v));
vector<LL> x, y;
inc(i, v.size()) { (i == 0 || v[i].FI != v[i - 1].FI ? x : y).PB(v[i].SE); }
sort(RALL(x));
sort(RALL(y));
vector<LL> sx(n + 1, -1e18), sy(n + 1);
sx[0] = 0;
inc(i, x.size()) { sx[i + 1] = sx[i] + x[i]; }
inc(i, y.size()) { sy[i + 1] = sy[i] + y[i]; }
LL ans = 0;
incII(i, 0, k) { setmax(ans, sx[i] + sy[k - i] + 1LL * i * i); }
cout << ans << endl;
return 0;
}
|
replace
| 80 | 81 | 80 | 82 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;
vector<ll> a[100001];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
int t, d;
cin >> t >> d;
a[t].push_back(d);
}
vector<ll> bst, ot;
for (int i = 1; i <= n; ++i) {
if (a[i].empty())
continue;
sort(all(a[i]));
reverse(all(a[i]));
bst.push_back(a[i][0]);
for (int j = 1; j < a[i].size(); ++j)
ot.push_back(a[i][j]);
}
sort(all(bst));
reverse(all(bst));
sort(all(ot));
reverse(all(ot));
for (int i = 1; i < bst.size(); ++i)
bst[i] += bst[i - 1];
for (int i = 1; i < ot.size(); ++i)
ot[i] += ot[i - 1];
ll sol = 0;
for (int i = 0; i < min((int)bst.size(), k); ++i) {
ll att = bst[i] + 1ll * (i + 1) * (i + 1);
if (k - i - 2 >= 0)
att += ot[max(0, min((int)ot.size() - 1, k - i - 2))];
sol = max(sol, att);
}
cout << sol;
}
|
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;
vector<ll> a[100001];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
int t, d;
cin >> t >> d;
a[t].push_back(d);
}
vector<ll> bst, ot;
for (int i = 1; i <= n; ++i) {
if (a[i].empty())
continue;
sort(all(a[i]));
reverse(all(a[i]));
bst.push_back(a[i][0]);
for (int j = 1; j < a[i].size(); ++j)
ot.push_back(a[i][j]);
}
sort(all(bst));
reverse(all(bst));
sort(all(ot));
reverse(all(ot));
for (int i = 1; i < bst.size(); ++i)
bst[i] += bst[i - 1];
for (int i = 1; i < ot.size(); ++i)
ot[i] += ot[i - 1];
ll sol = 0;
for (int i = 0; i < min((int)bst.size(), k); ++i) {
ll att = bst[i] + 1ll * (i + 1) * (i + 1);
if (k - i - 2 >= 0 && !ot.empty())
att += ot[max(0, min((int)ot.size() - 1, k - i - 2))];
sol = max(sol, att);
}
cout << sol;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int MAXN = 1e5 + 10;
int k, n;
P sushi[MAXN];
vector<int> removable;
vector<ll> v[MAXN];
vector<ll> sumV;
void solve() {
sort(sushi, sushi + n, greater<P>());
ll sum = 0;
ll typeNum = 0;
for (int i = 0; i < k; i++) {
sum += sushi[i].first;
if (v[sushi[i].second].size() > 0) {
removable.push_back(sushi[i].second);
} else {
typeNum++;
}
v[sushi[i].second].push_back(sushi[i].first);
}
sumV.push_back(sum + typeNum * typeNum);
for (int i = k; i < n; i++) {
if (v[sushi[i].second].size() == 0) {
int removeType = removable.back();
sum -= v[removeType].back();
v[removeType].pop_back();
removable.pop_back();
typeNum++;
sum += sushi[i].first;
v[sushi[i].second].push_back(sushi[i].first);
sumV.push_back(sum + typeNum * typeNum);
}
}
sort(sumV.begin(), sumV.end(), greater<ll>());
cout << sumV.front() << endl;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> sushi[i].second >> sushi[i].first;
}
solve();
}
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int MAXN = 1e5 + 10;
int k, n;
P sushi[MAXN];
vector<int> removable;
vector<ll> v[MAXN];
vector<ll> sumV;
void solve() {
sort(sushi, sushi + n, greater<P>());
ll sum = 0;
ll typeNum = 0;
for (int i = 0; i < k; i++) {
sum += sushi[i].first;
if (v[sushi[i].second].size() > 0) {
removable.push_back(sushi[i].second);
} else {
typeNum++;
}
v[sushi[i].second].push_back(sushi[i].first);
}
sumV.push_back(sum + typeNum * typeNum);
for (int i = k; i < n && !removable.empty(); i++) {
if (v[sushi[i].second].size() == 0) {
int removeType = removable.back();
sum -= v[removeType].back();
v[removeType].pop_back();
removable.pop_back();
typeNum++;
sum += sushi[i].first;
v[sushi[i].second].push_back(sushi[i].first);
sumV.push_back(sum + typeNum * typeNum);
}
}
sort(sumV.begin(), sumV.end(), greater<ll>());
cout << sumV.front() << endl;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> sushi[i].second >> sushi[i].first;
}
solve();
}
|
replace
| 30 | 31 | 30 | 31 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int main() {
int64_t N, K, t, d;
cin >> N >> K;
vector<priority_queue<int64_t>> vpq(MAXN);
for (int i = 0; i < N; i++) {
cin >> t >> d;
vpq[t].push(d);
}
priority_queue<pair<int64_t, int64_t>> pq;
for (int i = 0; i < MAXN; i++) {
if (vpq[i].empty())
continue;
pq.emplace(vpq[i].top(), 1);
vpq[i].pop();
while (vpq[i].size()) {
pq.emplace(vpq[i].top(), 0);
vpq[i].pop();
}
}
int64_t tmp = 0, ans = 0;
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> pq0, pq1;
for (int i = 0; i < K; i++) {
tmp += pq.top().first;
if (pq.top().second)
pq1.push(pq.top().first);
else
pq0.push(pq.top().first);
pq.pop();
}
tmp += pq1.size() * pq1.size();
ans = max(ans, tmp);
while (pq.size()) {
if (pq.top().second) {
tmp -= pq1.size() * pq1.size();
tmp -= pq0.top();
pq0.pop();
tmp += pq.top().first;
pq1.push(pq.top().first);
tmp += pq1.size() * pq1.size();
}
ans = max(ans, tmp);
pq.pop();
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int main() {
int64_t N, K, t, d;
cin >> N >> K;
vector<priority_queue<int64_t>> vpq(MAXN);
for (int i = 0; i < N; i++) {
cin >> t >> d;
vpq[t].push(d);
}
priority_queue<pair<int64_t, int64_t>> pq;
for (int i = 0; i < MAXN; i++) {
if (vpq[i].empty())
continue;
pq.emplace(vpq[i].top(), 1);
vpq[i].pop();
while (vpq[i].size()) {
pq.emplace(vpq[i].top(), 0);
vpq[i].pop();
}
}
int64_t tmp = 0, ans = 0;
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> pq0, pq1;
for (int i = 0; i < K; i++) {
tmp += pq.top().first;
if (pq.top().second)
pq1.push(pq.top().first);
else
pq0.push(pq.top().first);
pq.pop();
}
tmp += pq1.size() * pq1.size();
ans = max(ans, tmp);
while (pq.size()) {
if (pq.top().second) {
tmp -= pq1.size() * pq1.size();
if (pq0.empty()) {
cout << ans << endl;
return 0;
}
tmp -= pq0.top();
pq0.pop();
tmp += pq.top().first;
pq1.push(pq.top().first);
tmp += pq1.size() * pq1.size();
}
ans = max(ans, tmp);
pq.pop();
}
cout << ans << endl;
}
|
insert
| 38 | 38 | 38 | 42 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define ALL(c) (c).begin(), (c).end()
#define sz(v) (int)v.size()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
const char EOL = '\n';
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
void solve() {
int n, k;
cin >> n >> k;
vector<pair<ll, int>> s(n);
int t;
ll d;
REP(i, n) {
cin >> t >> d;
s[i] = make_pair(d, t);
}
sort(ALL(s));
reverse(ALL(s));
map<int, ll> v;
deque<ll> q;
ll tmp = 0;
REP(i, k) {
if (v.count(s[i].second) == 0) {
v[s[i].second] = s[i].first;
} else {
q.push_front(s[i].first);
}
tmp += s[i].first;
}
ll x = sz(v);
tmp += x * x;
ll ans = tmp;
FOR(i, k, n) {
if (v.count(s[i].second) != 0)
continue;
ll r = q.front();
tmp = tmp - r - (x * x) + s[i].first + ((x + 1) * (x + 1));
q.pop_front();
v[s[i].second] = s[i].first;
x++;
chmax(ans, tmp);
}
cout << ans << EOL;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define ALL(c) (c).begin(), (c).end()
#define sz(v) (int)v.size()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
const char EOL = '\n';
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
void solve() {
int n, k;
cin >> n >> k;
vector<pair<ll, int>> s(n);
int t;
ll d;
REP(i, n) {
cin >> t >> d;
s[i] = make_pair(d, t);
}
sort(ALL(s));
reverse(ALL(s));
map<int, ll> v;
deque<ll> q;
ll tmp = 0;
REP(i, k) {
if (v.count(s[i].second) == 0) {
v[s[i].second] = s[i].first;
} else {
q.push_front(s[i].first);
}
tmp += s[i].first;
}
ll x = sz(v);
tmp += x * x;
ll ans = tmp;
FOR(i, k, n) {
if (q.empty())
break;
if (v.count(s[i].second) != 0)
continue;
ll r = q.front();
tmp = tmp - r - (x * x) + s[i].first + ((x + 1) * (x + 1));
q.pop_front();
v[s[i].second] = s[i].first;
x++;
chmax(ans, tmp);
}
cout << ans << EOL;
}
|
insert
| 67 | 67 | 67 | 69 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#ifdef DEBUG
#define PRINT(x) \
cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = " \
<< (x) << endl;
#define PRINTA(a, first, last) \
cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \
<< (first) << ", " << (last) << ")" << endl; \
for (int i = (first); i < (last); ++i) { \
cout << #a << "[" << i << "] = " << (a)[i] << endl; \
}
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
template <class T, class U> void amax(T &x, U y) {
if (x < y)
x = y;
}
template <class T, class U> void amin(T &x, U y) {
if (x > y)
x = y;
}
int main(void) {
int N, K;
cin >> N >> K;
vector<pii> v(N);
REP(i, N) {
int t, d;
cin >> t >> d;
t--;
v[i] = pii(d, t);
}
sort(all(v), greater<pii>());
vector<int> variety(N + 1);
vector<int> w;
variety[0] = 0;
vector<bool> used(N, false);
REP(i, N) {
if (!used[v[i].second]) {
used[v[i].second] = true;
variety[i + 1] = variety[i] + 1;
w.pb(v[i].first);
} else {
variety[i + 1] = variety[i];
}
}
vector<ll> acc_v_first(N + 1);
acc_v_first[0] = 0;
REP(i, N) { acc_v_first[i + 1] = acc_v_first[i] + v[i].first; }
vector<ll> acc_w(w.size() + 1);
acc_w[0] = 0;
REP(i, w.size()) { acc_w[i + 1] = acc_w[i] + w[i]; }
ll result = 0;
REP(l, K + 1) {
PRINT(l);
int x0 = variety[l];
int x1 = K - l;
PRINT(x0);
PRINT(x1);
ll x = x0 + x1;
ll variety_bonus = x * x;
ll base_total_deliciousness = acc_v_first[l] + (acc_w[x] - acc_w[x0]);
PRINT(variety_bonus);
PRINT(base_total_deliciousness);
ll satisfaction = base_total_deliciousness + variety_bonus;
PRINT(satisfaction);
amax(result, satisfaction);
}
cout << result << endl;
return 0;
}
|
#include <bits/stdc++.h>
#ifdef DEBUG
#define PRINT(x) \
cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = " \
<< (x) << endl;
#define PRINTA(a, first, last) \
cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \
<< (first) << ", " << (last) << ")" << endl; \
for (int i = (first); i < (last); ++i) { \
cout << #a << "[" << i << "] = " << (a)[i] << endl; \
}
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
template <class T, class U> void amax(T &x, U y) {
if (x < y)
x = y;
}
template <class T, class U> void amin(T &x, U y) {
if (x > y)
x = y;
}
int main(void) {
int N, K;
cin >> N >> K;
vector<pii> v(N);
REP(i, N) {
int t, d;
cin >> t >> d;
t--;
v[i] = pii(d, t);
}
sort(all(v), greater<pii>());
vector<int> variety(N + 1);
vector<int> w;
variety[0] = 0;
vector<bool> used(N, false);
REP(i, N) {
if (!used[v[i].second]) {
used[v[i].second] = true;
variety[i + 1] = variety[i] + 1;
w.pb(v[i].first);
} else {
variety[i + 1] = variety[i];
}
}
vector<ll> acc_v_first(N + 1);
acc_v_first[0] = 0;
REP(i, N) { acc_v_first[i + 1] = acc_v_first[i] + v[i].first; }
vector<ll> acc_w(w.size() + 1);
acc_w[0] = 0;
REP(i, w.size()) { acc_w[i + 1] = acc_w[i] + w[i]; }
ll result = 0;
REP(l, K + 1) {
PRINT(l);
int x0 = variety[l];
int x1 = K - l;
PRINT(x0);
PRINT(x1);
ll x = x0 + x1;
if (x > w.size()) {
continue;
}
ll variety_bonus = x * x;
ll base_total_deliciousness = acc_v_first[l] + (acc_w[x] - acc_w[x0]);
PRINT(variety_bonus);
PRINT(base_total_deliciousness);
ll satisfaction = base_total_deliciousness + variety_bonus;
PRINT(satisfaction);
amax(result, satisfaction);
}
cout << result << endl;
return 0;
}
|
insert
| 84 | 84 | 84 | 87 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T, class U> void assign(V<T> &v, int n, const U &a) {
v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
v.resize(n);
for (auto &&e : v)
assign(e, args...);
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
V<> t(n), d(n);
for (int i = 0; i < n; ++i)
cin >> t[i] >> d[i], --t[i];
V<> mx(n);
for (int i = 0; i < n; ++i) {
mx[t[i]] = max(mx[t[i]], d[i]);
}
auto _mx = mx;
sort(rbegin(_mx), rend(_mx));
V<lint> cmx(n + 1);
for (int i = 0; i < n; ++i) {
cmx[i + 1] = cmx[i] + _mx[i];
}
for (int i = 0; i < n; ++i) {
if (mx[t[i]] == d[i]) {
mx[t[i]] = d[i] = 0;
}
}
sort(rbegin(d), rend(d));
V<lint> cd(n + 1);
for (int i = 0; i < n; ++i) {
cd[i + 1] = cd[i] + d[i];
}
lint res = 0;
int m = set<int>(begin(t), end(t)).size();
for (lint x = 1; x <= m; ++x) {
res = max(res, cmx[x] + cd[k - x] + x * x);
}
cout << res << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T, class U> void assign(V<T> &v, int n, const U &a) {
v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
v.resize(n);
for (auto &&e : v)
assign(e, args...);
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
V<> t(n), d(n);
for (int i = 0; i < n; ++i)
cin >> t[i] >> d[i], --t[i];
V<> mx(n);
for (int i = 0; i < n; ++i) {
mx[t[i]] = max(mx[t[i]], d[i]);
}
auto _mx = mx;
sort(rbegin(_mx), rend(_mx));
V<lint> cmx(n + 1);
for (int i = 0; i < n; ++i) {
cmx[i + 1] = cmx[i] + _mx[i];
}
for (int i = 0; i < n; ++i) {
if (mx[t[i]] == d[i]) {
mx[t[i]] = d[i] = 0;
}
}
sort(rbegin(d), rend(d));
V<lint> cd(n + 1);
for (int i = 0; i < n; ++i) {
cd[i + 1] = cd[i] + d[i];
}
lint res = 0;
int m = set<int>(begin(t), end(t)).size();
for (lint x = 1; x <= min(m, k); ++x) {
res = max(res, cmx[x] + cd[k - x] + x * x);
}
cout << res << '\n';
}
|
replace
| 47 | 48 | 47 | 48 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define itrdebug(x) \
cerr << #x << " "; \
for (auto &el : (x)) { \
cerr << (el) << " "; \
} \
cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;
void solve(long long N, long long K, std::vector<long long> t,
std::vector<long long> d) {
multiset<pair<int, int>> all, notIncluded, included;
unordered_map<int, int> cnt;
rep(i, 0, N) all.insert({-d[i], t[i]});
notIncluded = all;
ll answer = 0;
for (const auto &p : all) {
if (included.size() == K)
break;
if (cnt.find(p.second) == cnt.end()) {
cnt[p.second]++;
answer += abs(p.first);
notIncluded.erase(notIncluded.find(p));
included.insert({abs(p.first), p.second});
}
}
vector<pair<int, int>> toBeRemoved;
for (const auto &p : notIncluded) {
if (included.size() == K)
break;
cnt[p.second]++;
answer += abs(p.first);
toBeRemoved.push_back(p);
notIncluded.erase(notIncluded.find(p));
included.insert({abs(p.first), p.second});
}
for (const auto &p : toBeRemoved)
notIncluded.erase(notIncluded.find(p));
for (const auto &p : included) {
auto add = notIncluded.begin();
auto remove = included.find(p);
if (add == notIncluded.end() || remove == included.end() ||
abs(add->first) <= abs(remove->first))
break;
if (add->second == remove->second ||
pow(cnt.size() - 1, 2) + abs(add->first) >
pow(cnt.size(), 2) + abs(remove->first)) {
notIncluded.erase(add);
answer += abs(add->first) - abs(remove->first);
cnt[remove->second]--;
cnt[add->second]++;
if (cnt[remove->second] == 0)
cnt.erase(remove->second);
}
}
answer += pow(cnt.size(), 2);
cout << answer << endl;
}
signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> t(N);
std::vector<long long> d(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &t[i]);
scanf("%lld", &d[i]);
}
solve(N, K, std::move(t), std::move(d));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define itrdebug(x) \
cerr << #x << " "; \
for (auto &el : (x)) { \
cerr << (el) << " "; \
} \
cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;
void solve(long long N, long long K, std::vector<long long> t,
std::vector<long long> d) {
multiset<pair<int, int>> all, notIncluded, included;
unordered_map<int, int> cnt;
rep(i, 0, N) all.insert({-d[i], t[i]});
notIncluded = all;
ll answer = 0;
for (const auto &p : all) {
if (included.size() == K)
break;
if (cnt.find(p.second) == cnt.end()) {
cnt[p.second]++;
answer += abs(p.first);
notIncluded.erase(notIncluded.find(p));
included.insert({abs(p.first), p.second});
}
}
vector<pair<int, int>> toBeRemoved;
for (const auto &p : notIncluded) {
if (included.size() == K)
break;
cnt[p.second]++;
answer += abs(p.first);
toBeRemoved.push_back(p);
included.insert({abs(p.first), p.second});
}
for (const auto &p : toBeRemoved)
notIncluded.erase(notIncluded.find(p));
for (const auto &p : included) {
auto add = notIncluded.begin();
auto remove = included.find(p);
if (add == notIncluded.end() || remove == included.end() ||
abs(add->first) <= abs(remove->first))
break;
if (add->second == remove->second ||
pow(cnt.size() - 1, 2) + abs(add->first) >
pow(cnt.size(), 2) + abs(remove->first)) {
notIncluded.erase(add);
answer += abs(add->first) - abs(remove->first);
cnt[remove->second]--;
cnt[add->second]++;
if (cnt[remove->second] == 0)
cnt.erase(remove->second);
}
}
answer += pow(cnt.size(), 2);
cout << answer << endl;
}
signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> t(N);
std::vector<long long> d(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &t[i]);
scanf("%lld", &d[i]);
}
solve(N, K, std::move(t), std::move(d));
return 0;
}
|
delete
| 52 | 53 | 52 | 52 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define MAX_N (100010)
int main() {
int N, K;
scanf("%d%d", &N, &K);
map<int, vector<int>> sushi;
for (int i = 0; i < N; i++) {
int t, d;
scanf("%d%d", &t, &d);
sushi[t].push_back(d);
}
vector<int> top, other;
for (auto iter = sushi.begin(); iter != sushi.end(); iter++) {
vector<int> elements = iter->second;
sort(elements.begin(), elements.end(), greater<int>());
top.push_back(elements[0]);
for (int i = 1; i < (int)elements.size(); i++)
other.push_back(elements[i]);
}
sort(top.begin(), top.end(), greater<int>());
sort(other.begin(), other.end(), greater<int>());
ll res = 0;
for (int k = 1; k <= K; k++) {
if (k > (int)top.size())
continue;
ll local = 0;
for (int i = 0; i < k; i++)
local += (ll)top[i];
for (int i = k; i < K; i++)
local += (ll)other[i - k];
local += (ll)k * (ll)k;
res = max(res, local);
}
printf("%lld\n", res);
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define MAX_N (100010)
int main() {
int N, K;
scanf("%d%d", &N, &K);
map<int, vector<int>> sushi;
for (int i = 0; i < N; i++) {
int t, d;
scanf("%d%d", &t, &d);
sushi[t].push_back(d);
}
vector<int> top, other;
for (auto iter = sushi.begin(); iter != sushi.end(); iter++) {
vector<int> elements = iter->second;
sort(elements.begin(), elements.end(), greater<int>());
top.push_back(elements[0]);
for (int i = 1; i < (int)elements.size(); i++)
other.push_back(elements[i]);
}
sort(top.begin(), top.end(), greater<int>());
sort(other.begin(), other.end(), greater<int>());
ll res = 0;
for (int k = 1; k <= K; k++) {
if (k > (int)top.size() || K - k > (int)other.size())
continue;
ll local = 0;
for (int i = 0; i < k; i++)
local += (ll)top[i];
for (int i = k; i < K; i++)
local += (ll)other[i - k];
local += (ll)k * (ll)k;
res = max(res, local);
}
printf("%lld\n", res);
return 0;
}
|
replace
| 40 | 41 | 40 | 41 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl
template <class T> bool set_min(T &a, const T &b) {
return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &a : v)
is >> a;
return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &t : v)
os << "\t" << t;
return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
return os << "<" << v.first << ", " << v.second << ">";
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
class Solver {
public:
bool solve() {
int N, K;
cin >> N >> K;
vector<int> T(N), D(N);
rep(i, N) cin >> T[i] >> D[i];
map<int, pii> top1;
rep(i, N) set_max(top1[T[i]], make_pair(D[i], i));
set<int> top1_indices;
for (auto &p : top1)
top1_indices.insert(p.second.second);
vector<int> top1_list, other_list;
rep(i, N)(top1_indices.count(i) ? top1_list : other_list).push_back(D[i]);
while (other_list.size() < N)
other_list.push_back(0);
auto make_sum = [](vector<int> &l) {
sort(all(l), greater<int>());
vector<ll> sum(l.size() + 1);
rep(i, l.size()) sum[i + 1] = sum[i] + l[i];
return sum;
};
auto top1_sum = make_sum(top1_list);
auto other_sum = make_sum(other_list);
ll ans = 0;
repeat(k, 1, K + 1) {
ll l = K - k;
if (k >= top1_sum.size())
continue;
ll sum = top1_sum[k] + other_sum[l];
if (l > 0) {
int m = upper_bound(all(top1_list), other_list[l - 1], greater<int>()) -
begin(top1_list) - 1 + 1;
if (k < m and m < top1_sum.size()) {
sum = top1_sum[m] + other_sum[K - m];
}
}
set_max(ans, sum + (ll)k * k);
}
cout << ans << endl;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
Solver s;
s.solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl
template <class T> bool set_min(T &a, const T &b) {
return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &a : v)
is >> a;
return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &t : v)
os << "\t" << t;
return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
return os << "<" << v.first << ", " << v.second << ">";
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
class Solver {
public:
bool solve() {
int N, K;
cin >> N >> K;
vector<int> T(N), D(N);
rep(i, N) cin >> T[i] >> D[i];
map<int, pii> top1;
rep(i, N) set_max(top1[T[i]], make_pair(D[i], i));
set<int> top1_indices;
for (auto &p : top1)
top1_indices.insert(p.second.second);
vector<int> top1_list, other_list;
rep(i, N)(top1_indices.count(i) ? top1_list : other_list).push_back(D[i]);
while (other_list.size() < N)
other_list.push_back(0);
auto make_sum = [](vector<int> &l) {
sort(all(l), greater<int>());
vector<ll> sum(l.size() + 1);
rep(i, l.size()) sum[i + 1] = sum[i] + l[i];
return sum;
};
auto top1_sum = make_sum(top1_list);
auto other_sum = make_sum(other_list);
ll ans = 0;
repeat(k, 1, K + 1) {
ll l = K - k;
if (k >= top1_sum.size())
continue;
ll sum = top1_sum[k] + other_sum[l];
if (l > 0) {
int m = upper_bound(all(top1_list), other_list[l - 1], greater<int>()) -
begin(top1_list) - 1 + 1;
if (m - k > l)
m = k + l;
if (k < m and m < top1_sum.size()) {
sum = top1_sum[m] + other_sum[K - m];
}
}
set_max(ans, sum + (ll)k * k);
}
cout << ans << endl;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
Solver s;
s.solve();
return 0;
}
|
insert
| 77 | 77 | 77 | 80 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const ll mod = 1000000007;
const ll INF = 1e+12;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define int long long
// ある量を決めるとどうすべきかがgreedyに決まる
// 探索すべきものを絞る
// ちょうどを以上にしても大丈夫
// x種類選んだ状態とx+1種類選んだ状態との間の差分をいかに素早く更新できるようなデータの持ち方を工夫する
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> s(n);
rep(i, n) {
int t, d;
cin >> t >> d;
t--; // 配列のindexにとる時はその範囲に要注意!
s[t].push_back(d);
}
for (auto &v : s) {
sort(all(v), greater<int>());
if (v.empty())
v.push_back(-INF); // 番兵
}
sort(all(s), [&](vector<int> a, vector<int> b) { return a[0] > b[0]; });
priority_queue<int> pque;
int cur = 0;
rep(i, k) {
cur += s[i][0];
for (int j = 1; j < (int)s.size(); j++) {
pque.push(s[i][j]);
}
}
Rep(i, k, n) {
rep(j, (int)s[i].size()) { pque.push(s[i][j]); }
}
int res = cur + k * k;
for (int x = k - 1; x >= 1; x--) { // 何度も同じ計算をしない(例 累積和)
int v = s[x][0];
int w = pque.top();
if (v < w) {
cur += w;
pque.pop();
cur -= v;
pque.push(v);
}
res = max(res, cur + x * x);
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout << fixed << setprecision(10);
// init();
solve();
// cout << "finish" << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const ll mod = 1000000007;
const ll INF = 1e+12;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define int long long
// ある量を決めるとどうすべきかがgreedyに決まる
// 探索すべきものを絞る
// ちょうどを以上にしても大丈夫
// x種類選んだ状態とx+1種類選んだ状態との間の差分をいかに素早く更新できるようなデータの持ち方を工夫する
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> s(n);
rep(i, n) {
int t, d;
cin >> t >> d;
t--; // 配列のindexにとる時はその範囲に要注意!
s[t].push_back(d);
}
for (auto &v : s) {
sort(all(v), greater<int>());
if (v.empty())
v.push_back(-INF); // 番兵
}
sort(all(s), [&](vector<int> a, vector<int> b) { return a[0] > b[0]; });
priority_queue<int> pque;
int cur = 0;
rep(i, k) {
cur += s[i][0];
for (int j = 1; j < (int)s[i].size(); j++) {
pque.push(s[i][j]);
}
}
Rep(i, k, n) {
rep(j, (int)s[i].size()) { pque.push(s[i][j]); }
}
int res = cur + k * k;
for (int x = k - 1; x >= 1; x--) { // 何度も同じ計算をしない(例 累積和)
int v = s[x][0];
int w = pque.top();
if (v < w) {
cur += w;
pque.pop();
cur -= v;
pque.push(v);
}
res = max(res, cur + x * x);
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout << fixed << setprecision(10);
// init();
solve();
// cout << "finish" << endl;
return 0;
}
|
replace
| 90 | 91 | 90 | 91 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 100002;
int n, k;
long long ans;
struct cake {
int kind;
long long base;
};
bool mycmp(const cake &a, const cake &b) { return a.base > b.base; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
vector<cake> data(n);
vector<int> freq(n + 2);
for (int i = 0; i < n; ++i)
cin >> data[i].kind >> data[i].base;
sort(data.begin(), data.end(), mycmp);
priority_queue<int, vector<int>, greater<int>> pq;
long long tot = 0;
for (int i = 0; i < k; ++i) {
ans += data[i].base;
if (!freq[data[i].kind])
++tot;
++freq[data[i].kind];
if (freq[data[i].kind] > 1)
pq.push(data[i].base);
}
long long sum = ans;
ans += tot * tot;
for (int i = k; i < n; ++i) {
if (freq[data[i].kind] > 0)
continue;
sum += data[i].base;
sum -= pq.top();
pq.pop();
++freq[data[i].kind];
++tot;
ans = max(ans, sum + tot * tot);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 100002;
int n, k;
long long ans;
struct cake {
int kind;
long long base;
};
bool mycmp(const cake &a, const cake &b) { return a.base > b.base; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
vector<cake> data(n);
vector<int> freq(n + 2);
for (int i = 0; i < n; ++i)
cin >> data[i].kind >> data[i].base;
sort(data.begin(), data.end(), mycmp);
priority_queue<int, vector<int>, greater<int>> pq;
long long tot = 0;
for (int i = 0; i < k; ++i) {
ans += data[i].base;
if (!freq[data[i].kind])
++tot;
++freq[data[i].kind];
if (freq[data[i].kind] > 1)
pq.push(data[i].base);
}
long long sum = ans;
ans += tot * tot;
for (int i = k; i < n && !pq.empty(); ++i) {
if (freq[data[i].kind] > 0)
continue;
sum += data[i].base;
sum -= pq.top();
pq.pop();
++freq[data[i].kind];
++tot;
ans = max(ans, sum + tot * tot);
}
cout << ans << endl;
return 0;
}
|
replace
| 60 | 61 | 60 | 61 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
int n, k;
vector<pair<ll, int>> td;
map<int, int> mp;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
pque;
vector<ll> ans;
int main(void) {
// Here your code !
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) {
int t;
ll d;
scanf("%d %lld", &t, &d);
td.push_back(make_pair(d, t));
}
sort(td.begin(), td.end(), greater<pair<ll, int>>());
ll tmp_sum = 0;
for (int i = 0; i < k; ++i) {
mp[td[i].second] += 1;
if (mp[td[i].second] > 1) {
pque.push(make_pair(td[i].first, td[i].second));
}
tmp_sum += td[i].first;
}
ans.push_back(tmp_sum + pow(mp.size(), 2));
for (int i = k; i < n; ++i) {
if (mp.find(td[i].second) == mp.end()) {
pair<ll, int> e = pque.top();
pque.pop();
mp[e.second] -= 1;
tmp_sum -= e.first;
mp[td[i].second] += 1;
tmp_sum += td[i].first;
ans.push_back(tmp_sum + powl(mp.size(), 2));
}
}
ll last_ans = *max_element(ans.begin(), ans.end());
printf("%lld\n", last_ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
int n, k;
vector<pair<ll, int>> td;
map<int, int> mp;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
pque;
vector<ll> ans;
int main(void) {
// Here your code !
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) {
int t;
ll d;
scanf("%d %lld", &t, &d);
td.push_back(make_pair(d, t));
}
sort(td.begin(), td.end(), greater<pair<ll, int>>());
ll tmp_sum = 0;
for (int i = 0; i < k; ++i) {
mp[td[i].second] += 1;
if (mp[td[i].second] > 1) {
pque.push(make_pair(td[i].first, td[i].second));
}
tmp_sum += td[i].first;
}
ans.push_back(tmp_sum + pow(mp.size(), 2));
for (int i = k; i < n; ++i) {
if (mp.find(td[i].second) == mp.end() && !pque.empty()) {
pair<ll, int> e = pque.top();
pque.pop();
mp[e.second] -= 1;
tmp_sum -= e.first;
mp[td[i].second] += 1;
tmp_sum += td[i].first;
ans.push_back(tmp_sum + powl(mp.size(), 2));
}
}
ll last_ans = *max_element(ans.begin(), ans.end());
printf("%lld\n", last_ans);
return 0;
}
|
replace
| 47 | 48 | 47 | 48 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n) \
rep(i, n) { cout << a[i] << ' '; } \
cout << endl;
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<long long, long long> pll;
const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100000;
int main() {
ll n, k;
cin >> n >> k;
ll t[N], d[N];
rep(i, n) cin >> t[i] >> d[i];
ll maxd[N + 1];
rep(i, n + 1) maxd[i] = 0;
rep(i, n) maxd[t[i]] = max(d[i], maxd[t[i]]);
pll p[N];
rep(i, n) p[i] = pll(d[i], t[i]);
vector<pll> vpll;
rep(i, n + 1) if (maxd[i] != 0) vpll.push_back(pll(maxd[i], i));
sort(p, p + n, greater<pll>());
sort(vpll.begin(), vpll.end(), greater<pll>());
ll ans = 0, ct = 0;
ll cnt[N + 1];
rep(i, n + 1) cnt[i] = 0;
rep(i, k) {
ans += p[i].first;
cnt[p[i].second]++;
}
rep(i, n + 1) if (cnt[i] > 0) ct++;
ans += ct * ct;
ll anst = ans;
int rmi = k - 1;
for (int i = ct; i < vpll.size(); i++) {
while (cnt[p[rmi].second] <= 1)
rmi--;
cnt[p[rmi].second]--;
anst -= p[rmi].first;
rmi--;
anst += vpll[i].first;
anst += 2 * i + 1;
ans = max(anst, ans);
}
cout << ans << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n) \
rep(i, n) { cout << a[i] << ' '; } \
cout << endl;
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<long long, long long> pll;
const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100000;
int main() {
ll n, k;
cin >> n >> k;
ll t[N], d[N];
rep(i, n) cin >> t[i] >> d[i];
ll maxd[N + 1];
rep(i, n + 1) maxd[i] = 0;
rep(i, n) maxd[t[i]] = max(d[i], maxd[t[i]]);
pll p[N];
rep(i, n) p[i] = pll(d[i], t[i]);
vector<pll> vpll;
rep(i, n + 1) if (maxd[i] != 0) vpll.push_back(pll(maxd[i], i));
sort(p, p + n, greater<pll>());
sort(vpll.begin(), vpll.end(), greater<pll>());
ll ans = 0, ct = 0;
ll cnt[N + 1];
rep(i, n + 1) cnt[i] = 0;
rep(i, k) {
ans += p[i].first;
cnt[p[i].second]++;
}
rep(i, n + 1) if (cnt[i] > 0) ct++;
ans += ct * ct;
ll anst = ans;
for (int i = k - 1; i >= 0; i--) {
if (cnt[p[i].second] < 2)
continue;
if (ct >= vpll.size())
break;
cnt[p[i].second]--;
anst -= p[i].first;
anst += vpll[ct].first;
anst += 2 * ct + 1;
ct++;
ans = max(anst, ans);
}
cout << ans << endl;
}
|
replace
| 65 | 74 | 65 | 75 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
typedef long long lint;
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> v[N];
REP(i, N) {
int t, d;
cin >> t >> d;
t--;
v[t].push_back(d);
}
vector<lint> a, b;
REP(i, N) {
if (v[i].empty())
continue;
sort(ALL(v[i]), greater<int>());
a.push_back(v[i][0]);
FOR(j, 1, v[i].size()) b.push_back(v[i][j]);
}
sort(ALL(a), greater<lint>());
sort(ALL(b), greater<lint>());
// for(auto i:a)cerr<<i<<" ";
// cerr<<endl;
// for(auto i:b)cerr<<i<<" ";
// cerr<<endl;
vector<lint> sa, sb;
sa.push_back(0);
sb.push_back(0);
for (auto i : a)
sa.push_back(i + sa.back());
for (auto i : b)
sb.push_back(i + sb.back());
// for(auto i:sa)cerr<<i<<" ";
// cerr<<endl;
// for(auto i:sb)cerr<<i<<" ";
// cerr<<endl;
lint ans = 0;
FOR(i, 1, K + 1) {
lint temp = i * i;
temp += sa[i];
temp += sb[K - i];
ans = max(ans, temp);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
typedef long long lint;
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> v[N];
REP(i, N) {
int t, d;
cin >> t >> d;
t--;
v[t].push_back(d);
}
vector<lint> a, b;
REP(i, N) {
if (v[i].empty())
continue;
sort(ALL(v[i]), greater<int>());
a.push_back(v[i][0]);
FOR(j, 1, v[i].size()) b.push_back(v[i][j]);
}
sort(ALL(a), greater<lint>());
sort(ALL(b), greater<lint>());
// for(auto i:a)cerr<<i<<" ";
// cerr<<endl;
// for(auto i:b)cerr<<i<<" ";
// cerr<<endl;
vector<lint> sa, sb;
sa.push_back(0);
sb.push_back(0);
for (auto i : a)
sa.push_back(i + sa.back());
for (auto i : b)
sb.push_back(i + sb.back());
// for(auto i:sa)cerr<<i<<" ";
// cerr<<endl;
// for(auto i:sb)cerr<<i<<" ";
// cerr<<endl;
lint ans = 0;
FOR(i, 1, K + 1) {
if (sa.size() < i + 1)
continue;
if (sb.size() < K - i + 1)
continue;
lint temp = (lint)i * i;
temp += sa[i];
temp += sb[K - i];
ans = max(ans, temp);
}
cout << ans << endl;
return 0;
}
|
replace
| 56 | 57 | 56 | 61 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define sz(x) (int)x.size()
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<pair<int, int>> td(n);
for (int i = 0; i < n; i++) {
cin >> td[i].second >> td[i].first;
}
sort(td.rbegin(), td.rend());
//~ for (int i = 0; i < n; i++) {
//~ cout << td[i].first << " " << td[i].second << endl;
//~ }
//~ cout << endl;
vector<int> un;
vector<ll> nun;
ll un_sum = 0;
vector<bool> used(n + 1);
ll ans = 0;
nun.push_back(0);
for (int i = 0; i < n; i++) {
if (used[td[i].second]) {
if (nun.empty())
nun.push_back(td[i].first);
else
nun.push_back(td[i].first + nun.back());
} else {
used[td[i].second] = true;
un.push_back(td[i].first);
un_sum += td[i].first;
}
int x = un.size();
int need = k - x;
if (sz(nun) > need) {
ll total = (ll)x * x;
total += un_sum;
total += nun[need];
ans = max(ans, total);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define sz(x) (int)x.size()
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<pair<int, int>> td(n);
for (int i = 0; i < n; i++) {
cin >> td[i].second >> td[i].first;
}
sort(td.rbegin(), td.rend());
//~ for (int i = 0; i < n; i++) {
//~ cout << td[i].first << " " << td[i].second << endl;
//~ }
//~ cout << endl;
vector<int> un;
vector<ll> nun;
ll un_sum = 0;
vector<bool> used(n + 1);
ll ans = 0;
nun.push_back(0);
for (int i = 0; i < n; i++) {
if (used[td[i].second]) {
if (nun.empty())
nun.push_back(td[i].first);
else
nun.push_back(td[i].first + nun.back());
} else {
used[td[i].second] = true;
un.push_back(td[i].first);
un_sum += td[i].first;
}
int x = un.size();
int need = k - x;
if (sz(nun) > need && need >= 0) {
ll total = (ll)x * x;
total += un_sum;
total += nun[need];
ans = max(ans, total);
}
}
cout << ans;
return 0;
}
|
replace
| 38 | 39 | 38 | 39 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
// typedef pair<ll, P> T;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define f first
#define s second
#define pb push_back
ll mod = 1000000007;
const ll INF = 100000000000010000;
// ll bit[200010];
// ll kai[3010];
// void pres(double A, ll x = 20) { printf("%.20f\n", x); }
/*ll sum(ll i)
{
i += 1;
ll t = 0;
while (i > 0)
{
t = max(bit[i], t);
i -= i & -i;
}
return t;
}
void update(ll i, ll x)
{
i += 1;
while (i <= 200005)
{
bit[i] = max(bit[i], x);
i += i & -i;
}
}*/
/*`ll SAIKI(ll S) {
for (ll T = S; ; T = (T - 1) & S) {
}
}*/
void test() { cout << "888888888888888888888888" << endl; }
ll gcd(ll x, ll y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
ll pow_mod(ll x, ll y) {
if (y == 0)
return 1;
if (y % 2 == 1) {
return x * pow_mod(x, y - 1) % mod;
} else {
ll half = pow_mod(x, y / 2);
return half * half % mod;
}
}
ll div_mod(ll x) { return pow_mod(x, mod - 2); }
/*ll com(ll x, ll y) {
return kai[x] * div_mod(kai[y] * kai[x - y] % mod) % mod;
//毎回オーダーlog(n)かかる
}*/
/*void kaijo() {
kai[0] = 1;
for (ll i = 1; i <= 3004; i++) {
kai[i] = kai[i - 1] * i % mod;
}
}*/
ll N, K;
ll t[100010], d[100010];
vector<ll> syurui[100010];
vector<P> vec;
vector<ll> res;
ll syu, ans;
map<ll, ll> mp;
priority_queue<ll, vector<ll>, greater<ll>> que;
void solve() {
cin >> N >> K;
rep(i, N) {
cin >> t[i] >> d[i];
syurui[t[i]].pb(d[i]);
vec.push_back(P(d[i], t[i]));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
rep(i, K) {
ans += vec[i].first;
if (mp[vec[i].s] == 0) {
mp[vec[i].s]++;
syu++;
} else {
que.push(vec[i].f);
}
}
res.pb(ans + syu * syu);
for (ll i = K; i < N; i++) {
if (mp[vec[i].s] == 0) {
mp[vec[i].s]++;
syu++;
ans -= que.top();
que.pop();
ans += vec[i].first;
res.pb(ans + syu * syu);
}
}
for (ll a : res) {
ans = max(ans, a);
}
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
ll N;
cin >> N;
}
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
// typedef pair<ll, P> T;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define f first
#define s second
#define pb push_back
ll mod = 1000000007;
const ll INF = 100000000000010000;
// ll bit[200010];
// ll kai[3010];
// void pres(double A, ll x = 20) { printf("%.20f\n", x); }
/*ll sum(ll i)
{
i += 1;
ll t = 0;
while (i > 0)
{
t = max(bit[i], t);
i -= i & -i;
}
return t;
}
void update(ll i, ll x)
{
i += 1;
while (i <= 200005)
{
bit[i] = max(bit[i], x);
i += i & -i;
}
}*/
/*`ll SAIKI(ll S) {
for (ll T = S; ; T = (T - 1) & S) {
}
}*/
void test() { cout << "888888888888888888888888" << endl; }
ll gcd(ll x, ll y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
ll pow_mod(ll x, ll y) {
if (y == 0)
return 1;
if (y % 2 == 1) {
return x * pow_mod(x, y - 1) % mod;
} else {
ll half = pow_mod(x, y / 2);
return half * half % mod;
}
}
ll div_mod(ll x) { return pow_mod(x, mod - 2); }
/*ll com(ll x, ll y) {
return kai[x] * div_mod(kai[y] * kai[x - y] % mod) % mod;
//毎回オーダーlog(n)かかる
}*/
/*void kaijo() {
kai[0] = 1;
for (ll i = 1; i <= 3004; i++) {
kai[i] = kai[i - 1] * i % mod;
}
}*/
ll N, K;
ll t[100010], d[100010];
vector<ll> syurui[100010];
vector<P> vec;
vector<ll> res;
ll syu, ans;
map<ll, ll> mp;
priority_queue<ll, vector<ll>, greater<ll>> que;
void solve() {
cin >> N >> K;
rep(i, N) {
cin >> t[i] >> d[i];
syurui[t[i]].pb(d[i]);
vec.push_back(P(d[i], t[i]));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
rep(i, K) {
ans += vec[i].first;
if (mp[vec[i].s] == 0) {
mp[vec[i].s]++;
syu++;
} else {
que.push(vec[i].f);
}
}
res.pb(ans + syu * syu);
for (ll i = K; i < N; i++) {
if (que.size() == 0)
break;
if (mp[vec[i].s] == 0) {
mp[vec[i].s]++;
syu++;
ans -= que.top();
que.pop();
ans += vec[i].first;
res.pb(ans + syu * syu);
}
}
for (ll a : res) {
ans = max(ans, a);
}
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
ll N;
cin >> N;
}
|
insert
| 118 | 118 | 118 | 120 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
priority_queue<ll, vector<ll>, greater<ll>> que;
vector<P> vec;
ll n, k, t, d, sum, shu, a[100010], ans;
bool jud[100010];
int main(void) {
// Your code here!
cin >> n >> k;
for (ll i = 0; i < n; i++) {
cin >> t >> d;
vec.push_back(P(d, t));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (ll i = 0; i < k; i++) {
if (jud[vec[i].second]) {
que.push(vec[i].first);
} else {
jud[vec[i].second] = true;
shu++;
}
sum += vec[i].first;
}
a[shu] = sum;
for (ll i = k; i < n; i++) {
if (jud[vec[i].second])
continue;
jud[vec[i].second] = true;
shu++;
a[shu] = a[shu - 1] + vec[i].first - que.top();
que.pop();
}
for (ll i = 0; i <= n; i++) {
if (a[i] == 0)
continue;
ans = max(ans, a[i] + i * i);
}
cout << ans << endl;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
priority_queue<ll, vector<ll>, greater<ll>> que;
vector<P> vec;
ll n, k, t, d, sum, shu, a[100010], ans;
bool jud[100010];
int main(void) {
// Your code here!
cin >> n >> k;
for (ll i = 0; i < n; i++) {
cin >> t >> d;
vec.push_back(P(d, t));
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (ll i = 0; i < k; i++) {
if (jud[vec[i].second]) {
que.push(vec[i].first);
} else {
jud[vec[i].second] = true;
shu++;
}
sum += vec[i].first;
}
a[shu] = sum;
for (ll i = k; i < n; i++) {
if (jud[vec[i].second])
continue;
if (que.size() == 0)
continue;
jud[vec[i].second] = true;
shu++;
a[shu] = a[shu - 1] + vec[i].first - que.top();
que.pop();
}
for (ll i = 0; i <= n; i++) {
if (a[i] == 0)
continue;
ans = max(ans, a[i] + i * i);
}
cout << ans << endl;
}
|
insert
| 33 | 33 | 33 | 35 |
0
| |
p03148
|
C++
|
Runtime Error
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef int64_t int64;
typedef uint32_t uint;
typedef uint64_t uint64;
//---
template <typename T> inline void print(const T &rhs) {
std::cout << " = " << rhs << std::endl;
}
template <typename T> inline void print(const std::vector<T> &rhs) {
std::cout << " = [ ";
for (uint i = 0; i < rhs.size(); ++i) {
std::cout << rhs[i] << ' ';
}
std::cout << "]" << std::endl;
}
template <typename T>
inline void print(const std::vector<std::vector<T>> &rhs) {
std::cout << " = " << std::endl;
std::cout << "[[ ";
for (uint p = 0; p < rhs.size(); ++p) {
if (p != 0) {
std::cout << " [ ";
}
for (uint q = 0; q < rhs[p].size(); ++q) {
std::cout << rhs[p][q] << ' ';
}
if (p != rhs.size() - 1) {
std::cout << "]" << std::endl;
}
}
std::cout << "]]" << std::endl;
}
template <typename TL, typename TR>
inline void print(const std::vector<std::pair<TR, TL>> &rhs) {
std::cout << " = [";
uint i = 0;
for (; i < rhs.size() - 1; ++i) {
std::cout << "[f: " << rhs[i].first << ", s: " << rhs[i].second << "], ";
}
std::cout << "[f: " << rhs[i].first << ", s: " << rhs[i].second << "]]"
<< endl;
}
#define printn(var) \
{ \
printf("%s", #var); \
print(var); \
}
#define printn_all(var) \
{ \
printf("%s(%d): ", __func__, __LINE__); \
printf("%s", #var); \
print(var); \
}
//---
struct td {
int t;
int d;
};
bool cmp_d(struct td &lhs, struct td &rhs) { return lhs.d < rhs.d; }
bool cmp_gr_d(struct td &lhs, struct td &rhs) { return lhs.d > rhs.d; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
uint N, K;
cin >> N >> K;
vector<struct td> vTD(N);
for (uint i = 0; i < N; ++i) {
cin >> vTD[i].t >> vTD[i].d;
}
sort(vTD.begin(), vTD.end(), cmp_gr_d);
unordered_map<int, int>
htUN; // a hash table for counting number of used types
vector<struct td> vTD_rm; // for remove
vector<struct td> vTD_add; // for add
int64 sum = 0ll;
for (uint i = 0; i < K; ++i) {
sum += vTD[i].d;
++htUN[vTD[i].t];
if (htUN[vTD[i].t] >= 2) {
vTD_rm.push_back(vTD[i]);
}
}
// sort(vTD_rm.begin(), vTD_rm.end(), cmp_d); // dont need to sort. By
// reversing the iteration to vTD_rm.
for (uint i = K; i < N; ++i) {
if (htUN[vTD[i].t] != 0) {
continue;
}
vTD_add.push_back(vTD[i]);
}
// sort(vTD_add.begin(), vTD_add.end(), cmp_gr_d); // dont need to sort.
// Because of the vTD is already sorted.
int64 numT = K - vTD_rm.size(); // num of type
int64 point = sum + numT * numT;
uint ri = vTD_rm.size() - 1;
uint ai = 0;
while (ri >= 0 && ai < vTD_add.size()) {
if (htUN[vTD_rm[ri].t] == 1) {
--ri;
continue;
}
if (htUN[vTD_add[ai].t] >= 1) {
++ai;
continue;
}
sum -= vTD_rm[ri].d;
--htUN[vTD_rm[ri].t];
--ri;
sum += vTD_add[ai].d;
++htUN[vTD_add[ai].t];
++ai;
++numT;
int64 tmp = sum + numT * numT;
point = max(point, tmp);
}
cout << point << endl;
return 0;
}
|
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef int64_t int64;
typedef uint32_t uint;
typedef uint64_t uint64;
//---
template <typename T> inline void print(const T &rhs) {
std::cout << " = " << rhs << std::endl;
}
template <typename T> inline void print(const std::vector<T> &rhs) {
std::cout << " = [ ";
for (uint i = 0; i < rhs.size(); ++i) {
std::cout << rhs[i] << ' ';
}
std::cout << "]" << std::endl;
}
template <typename T>
inline void print(const std::vector<std::vector<T>> &rhs) {
std::cout << " = " << std::endl;
std::cout << "[[ ";
for (uint p = 0; p < rhs.size(); ++p) {
if (p != 0) {
std::cout << " [ ";
}
for (uint q = 0; q < rhs[p].size(); ++q) {
std::cout << rhs[p][q] << ' ';
}
if (p != rhs.size() - 1) {
std::cout << "]" << std::endl;
}
}
std::cout << "]]" << std::endl;
}
template <typename TL, typename TR>
inline void print(const std::vector<std::pair<TR, TL>> &rhs) {
std::cout << " = [";
uint i = 0;
for (; i < rhs.size() - 1; ++i) {
std::cout << "[f: " << rhs[i].first << ", s: " << rhs[i].second << "], ";
}
std::cout << "[f: " << rhs[i].first << ", s: " << rhs[i].second << "]]"
<< endl;
}
#define printn(var) \
{ \
printf("%s", #var); \
print(var); \
}
#define printn_all(var) \
{ \
printf("%s(%d): ", __func__, __LINE__); \
printf("%s", #var); \
print(var); \
}
//---
struct td {
int t;
int d;
};
bool cmp_d(struct td &lhs, struct td &rhs) { return lhs.d < rhs.d; }
bool cmp_gr_d(struct td &lhs, struct td &rhs) { return lhs.d > rhs.d; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
uint N, K;
cin >> N >> K;
vector<struct td> vTD(N);
for (uint i = 0; i < N; ++i) {
cin >> vTD[i].t >> vTD[i].d;
}
sort(vTD.begin(), vTD.end(), cmp_gr_d);
unordered_map<int, int>
htUN; // a hash table for counting number of used types
vector<struct td> vTD_rm; // for remove
vector<struct td> vTD_add; // for add
int64 sum = 0ll;
for (uint i = 0; i < K; ++i) {
sum += vTD[i].d;
++htUN[vTD[i].t];
if (htUN[vTD[i].t] >= 2) {
vTD_rm.push_back(vTD[i]);
}
}
// sort(vTD_rm.begin(), vTD_rm.end(), cmp_d); // dont need to sort. By
// reversing the iteration to vTD_rm.
for (uint i = K; i < N; ++i) {
if (htUN[vTD[i].t] != 0) {
continue;
}
vTD_add.push_back(vTD[i]);
}
// sort(vTD_add.begin(), vTD_add.end(), cmp_gr_d); // dont need to sort.
// Because of the vTD is already sorted.
int64 numT = K - vTD_rm.size(); // num of type
int64 point = sum + numT * numT;
int ri = vTD_rm.size() - 1;
int ai = 0;
while (ri >= 0 && ai < vTD_add.size()) {
if (htUN[vTD_rm[ri].t] == 1) {
--ri;
continue;
}
if (htUN[vTD_add[ai].t] >= 1) {
++ai;
continue;
}
sum -= vTD_rm[ri].d;
--htUN[vTD_rm[ri].t];
--ri;
sum += vTD_add[ai].d;
++htUN[vTD_add[ai].t];
++ai;
++numT;
int64 tmp = sum + numT * numT;
point = max(point, tmp);
}
cout << point << endl;
return 0;
}
|
replace
| 107 | 109 | 107 | 109 |
0
| |
p03148
|
C++
|
Runtime Error
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int dx[] = {1, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, -1, 0};
const int Maxn = 100010;
int n, k;
vector<ll> v[Maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
ll t, d;
cin >> t >> d;
--t;
v[t].emplace_back(d);
}
for (int i = 0; i < n; ++i) {
sort(v[i].begin(), v[i].end());
}
vector<ll> y0, y1;
for (int i = 0; i < n; ++i) {
if (v[i].empty())
continue;
y1.emplace_back(v[i].back());
v[i].pop_back();
y0.insert(y0.end(), v[i].begin(), v[i].end());
}
sort(y0.begin(), y0.end(), greater<ll>());
sort(y1.begin(), y1.end(), greater<ll>());
int kind = max(0, k - (int)y0.size());
ll sum = 0;
for (int i = 0; i < kind; ++i) {
sum += y1[i];
}
for (int i = 0; i < k - kind; ++i) {
sum += y0[i];
}
ll ans = sum + 1LL * kind * kind;
while (kind <= min(k + 1, (int)y1.size())) {
ans = max(ans, sum + 1LL * kind * kind);
sum += y1[kind];
sum -= y0[k - kind - 1];
++kind;
}
cout << ans << endl;
}
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int dx[] = {1, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, -1, 0};
const int Maxn = 100010;
int n, k;
vector<ll> v[Maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
ll t, d;
cin >> t >> d;
--t;
v[t].emplace_back(d);
}
for (int i = 0; i < n; ++i) {
sort(v[i].begin(), v[i].end());
}
vector<ll> y0, y1;
for (int i = 0; i < n; ++i) {
if (v[i].empty())
continue;
y1.emplace_back(v[i].back());
v[i].pop_back();
y0.insert(y0.end(), v[i].begin(), v[i].end());
}
sort(y0.begin(), y0.end(), greater<ll>());
sort(y1.begin(), y1.end(), greater<ll>());
int kind = max(0, k - (int)y0.size());
ll sum = 0;
for (int i = 0; i < kind; ++i) {
sum += y1[i];
}
for (int i = 0; i < k - kind; ++i) {
sum += y0[i];
}
ll ans = sum + 1LL * kind * kind;
while (kind <= min(k + 1, (int)y1.size())) {
ans = max(ans, sum + 1LL * kind * kind);
sum += y1[kind];
if (kind >= k)
break;
sum -= y0[k - kind - 1];
++kind;
}
cout << ans << endl;
}
|
insert
| 68 | 68 | 68 | 70 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
ll n, k, t, d;
cin >> n >> k;
vector<pair<ll, ll>> A, B, C;
int var[200001] = {0};
rep(i, n) cin >> t >> d, A.push_back(make_pair(d, t));
sort(A.begin(), A.end(), greater<pair<ll, ll>>());
rep(i, k) B.push_back(A[i]), var[A[i].second]++;
rep(i, n - k) C.push_back(A[i + k]);
reverse(B.begin(), B.end());
ll c = 0, ans = 0, sum = 0, x = 0;
rep(i, k) sum += B[i].first;
rep(i, n) if (var[i + 1]) x++;
ans = sum + x * x;
rep(i, k) {
if (var[B[i].second] > 1) {
while (c < n - k && var[C[c].second] != 0)
c++;
if (c == n - k)
break;
var[B[i].second]--;
sum -= B[i].first;
B[i].first = C[i].first;
B[i].second = C[i].second;
var[B[i].second]++;
sum += B[i].first;
x++;
}
ans = max(ans, sum + x * x);
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
ll n, k, t, d;
cin >> n >> k;
vector<pair<ll, ll>> A, B, C;
int var[200001] = {0};
rep(i, n) cin >> t >> d, A.push_back(make_pair(d, t));
sort(A.begin(), A.end(), greater<pair<ll, ll>>());
rep(i, k) B.push_back(A[i]), var[A[i].second]++;
rep(i, n - k) C.push_back(A[i + k]);
reverse(B.begin(), B.end());
ll c = 0, ans = 0, sum = 0, x = 0;
rep(i, k) sum += B[i].first;
rep(i, n) if (var[i + 1]) x++;
ans = sum + x * x;
rep(i, k) {
if (var[B[i].second] > 1) {
while (c < n - k && var[C[c].second] != 0)
c++;
if (c == n - k)
break;
var[B[i].second]--;
sum -= B[i].first;
B[i].first = C[c].first;
B[i].second = C[c].second;
var[B[i].second]++;
sum += B[i].first;
x++;
}
ans = max(ans, sum + x * x);
}
cout << ans;
}
|
replace
| 26 | 28 | 26 | 28 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;
// ref : https://atcoder.jp/contests/abc116/submissions/4327163
int main() {
ll n, k;
cin >> n >> k;
vector<P> sushi(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
sushi.push_back({d, t});
}
sort(sushi.begin(), sushi.end());
reverse(sushi.begin(), sushi.end());
ll ans = 0;
vector<ll> used(n, 0);
stack<ll> sta;
ll res = 0;
ll kind = 0;
rep(i, k) {
res += sushi[i].first;
if (used[sushi[i].second]++ == 0)
kind++;
else
sta.push(sushi[i].first);
}
ans = res + kind * kind;
for (int i = k; i < n && sta.size(); i++) {
if (used[sushi[i].second]++)
continue;
ll a = sta.top();
sta.pop();
res -= a;
res += sushi[i].first;
kind++;
ans = max(ans, res + kind * kind);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;
// ref : https://atcoder.jp/contests/abc116/submissions/4327163
int main() {
ll n, k;
cin >> n >> k;
vector<P> sushi(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
t--;
sushi[i] = {d, t};
}
sort(sushi.begin(), sushi.end());
reverse(sushi.begin(), sushi.end());
ll ans = 0;
vector<ll> used(n, 0);
stack<ll> sta;
ll res = 0;
ll kind = 0;
rep(i, k) {
res += sushi[i].first;
if (used[sushi[i].second]++ == 0)
kind++;
else
sta.push(sushi[i].first);
}
ans = res + kind * kind;
for (int i = k; i < n && sta.size(); i++) {
if (used[sushi[i].second]++)
continue;
ll a = sta.top();
sta.pop();
res -= a;
res += sushi[i].first;
kind++;
ans = max(ans, res + kind * kind);
}
cout << ans << endl;
return 0;
}
|
replace
| 21 | 22 | 21 | 23 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl
template <class T> bool set_min(T &a, const T &b) {
return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &a : v)
is >> a;
return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &t : v)
os << "\t" << t;
return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
return os << "<" << v.first << ", " << v.second << ">";
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
class Solver {
public:
bool solve() {
int N, K;
cin >> N >> K;
vector<int> T(N), D(N);
rep(i, N) cin >> T[i] >> D[i];
map<int, pii> top1;
rep(i, N) set_max(top1[T[i]], make_pair(D[i], i));
set<int> top1_indices;
for (auto &p : top1)
top1_indices.insert(p.second.second);
vector<int> top1_list, other_list;
rep(i, N)(top1_indices.count(i) ? top1_list : other_list).push_back(D[i]);
while (other_list.size() < N)
other_list.push_back(0);
auto make_sum = [](vector<int> &l) {
sort(all(l), greater<int>());
vector<ll> sum(l.size() + 1);
rep(i, l.size()) sum[i + 1] = sum[i] + l[i];
return sum;
};
auto top1_sum = make_sum(top1_list);
auto other_sum = make_sum(other_list);
ll ans = 0;
repeat(k, 1, K + 1) {
ll l = K - k;
if (k >= top1_sum.size())
continue;
ll sum = top1_sum[k] + other_sum[l];
if (l > 0) {
int m = upper_bound(all(top1_list), other_list[l - 1], greater<int>()) -
begin(top1_list) - 1 + 1;
if (k < m and m < top1_sum.size()) {
sum = top1_sum[m] + other_sum[K - m];
}
}
set_max(ans, sum + (ll)k * k);
}
cout << ans << endl;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
Solver s;
s.solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl
template <class T> bool set_min(T &a, const T &b) {
return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &a : v)
is >> a;
return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &t : v)
os << "\t" << t;
return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
return os << "<" << v.first << ", " << v.second << ">";
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
class Solver {
public:
bool solve() {
int N, K;
cin >> N >> K;
vector<int> T(N), D(N);
rep(i, N) cin >> T[i] >> D[i];
map<int, pii> top1;
rep(i, N) set_max(top1[T[i]], make_pair(D[i], i));
set<int> top1_indices;
for (auto &p : top1)
top1_indices.insert(p.second.second);
vector<int> top1_list, other_list;
rep(i, N)(top1_indices.count(i) ? top1_list : other_list).push_back(D[i]);
while (other_list.size() < N)
other_list.push_back(0);
auto make_sum = [](vector<int> &l) {
sort(all(l), greater<int>());
vector<ll> sum(l.size() + 1);
rep(i, l.size()) sum[i + 1] = sum[i] + l[i];
return sum;
};
auto top1_sum = make_sum(top1_list);
auto other_sum = make_sum(other_list);
ll ans = 0;
repeat(k, 1, K + 1) {
ll l = K - k;
if (k >= top1_sum.size())
continue;
ll sum = top1_sum[k] + other_sum[l];
if (l > 0) {
int m = upper_bound(all(top1_list), other_list[l - 1], greater<int>()) -
begin(top1_list) - 1 + 1;
// if(k < m and m < top1_sum.size()) {
// sum = top1_sum[m] + other_sum[K - m];
// }
}
set_max(ans, sum + (ll)k * k);
}
cout << ans << endl;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
Solver s;
s.solve();
return 0;
}
|
replace
| 77 | 80 | 77 | 80 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(int argc, const char *argv[]) {
int n, k;
cin >> n >> k;
map<int, vector<ll>> sushi;
for (int i = 0; i < n; ++i) {
int t;
ll d;
cin >> t >> d;
sushi[t].push_back(d);
}
vector<ll> g1, g2;
for (auto &p : sushi) {
auto &vd = p.second;
auto max_it = max_element(vd.begin(), vd.end());
g1.push_back(*max_it);
for (auto it = vd.begin(); it != vd.end(); it++) {
if (it != max_it) {
g2.push_back(*it);
}
}
}
sort(g1.begin(), g1.end(), greater<ll>());
sort(g2.begin(), g2.end(), greater<ll>());
vector<ll> g1_psum{0}, g2_psum{0};
partial_sum(g1.begin(), g1.end(), back_inserter(g1_psum));
partial_sum(g2.begin(), g2.end(), back_inserter(g2_psum));
ll ans = 0;
for (ll i = 1; i <= k && i <= g1.size(); ++i) {
ll sum = i * i + g1_psum[i] + g2_psum[k - i];
ans = max(ans, sum);
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(int argc, const char *argv[]) {
int n, k;
cin >> n >> k;
map<int, vector<ll>> sushi;
for (int i = 0; i < n; ++i) {
int t;
ll d;
cin >> t >> d;
sushi[t].push_back(d);
}
vector<ll> g1, g2;
for (auto &p : sushi) {
auto &vd = p.second;
auto max_it = max_element(vd.begin(), vd.end());
g1.push_back(*max_it);
for (auto it = vd.begin(); it != vd.end(); it++) {
if (it != max_it) {
g2.push_back(*it);
}
}
}
sort(g1.begin(), g1.end(), greater<ll>());
sort(g2.begin(), g2.end(), greater<ll>());
vector<ll> g1_psum{0}, g2_psum{0};
partial_sum(g1.begin(), g1.end(), back_inserter(g1_psum));
partial_sum(g2.begin(), g2.end(), back_inserter(g2_psum));
ll ans = 0;
for (ll i = 1; i <= k && i <= g1.size(); ++i) {
if (k - i >= g2_psum.size()) {
continue;
}
ll sum = i * i + g1_psum[i] + g2_psum[k - i];
ans = max(ans, sum);
}
cout << ans << '\n';
return 0;
}
|
insert
| 37 | 37 | 37 | 41 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
template <class S, class T> using P = pair<S, T>;
template <class... T> using TP = tuple<T...>;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using str = string;
using vll = V<ll>;
using vvll = V<vll>;
using vvvll = V<vvll>;
using pl = P<ll, ll>;
using tl = TP<ll, ll, ll>;
using vpl = V<pl>;
using vvpl = V<vpl>;
using vtl = V<tl>;
using vvtl = V<vtl>;
using vs = V<str>;
using vvs = V<vs>;
using vd = V<dbl>;
using vvd = V<vd>;
using vvvd = V<vvd>;
using qll = queue<ll>;
using qpl = queue<pl>;
using stll = stack<ll>;
using stpl = stack<pl>;
using mapll = map<ll, ll>;
using setll = set<ll>;
using pqll = priority_queue<ll>;
// #define int ll
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define sz size()
#define bgn begin()
#define en end()
#define asn assign
#define emp empty()
#define fr front()
#define bk back()
#define clr clear()
#define ins insert
#define ers erase
#define res resize
#define tp top()
#define p_q priority_queue
#define inv inverse()
#define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++)
#define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--)
#define REP(i, a) FOR((i), 0, (ll)(a)-1)
#define REP0(i, a) FOR((i), 0, (ll)(a))
#define REP1(i, a) FOR((i), 1, (ll)(a))
#define rREP(i, a) rFOR((i), 0, (ll)(a)-1)
#define rREP0(i, a) rFOR((i), 0, (ll)(a))
#define rREP1(i, a) rFOR((i), 1, (ll)(a))
#define ROR(v, i) for (auto &(i) : (v))
#define IOTA(a, n) iota((a).bgn, (a).en, (n))
#define SORT(a) sort((a).bgn, (a).en)
#define rSORT(a) sort((a).rbegin(), (a).rend())
#define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en)
#define PREVP(a) prev_permutation((a).bgn, (a).en)
#define NEXTP(a) next_permutation((a).bgn, (a).en)
#define BINS(a, b) binary_search((a).bgn, (a).en, (b))
#define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn)
#define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn)
#define CNT(a, b) count((a).bgn, (a).en, b)
#define SUM(a) accumulate((a).bgn, (a).en, 0)
#define REV(a) reverse((a).bgn, (a).en)
#define REGS(a, b) regex_search((a), regex(b))
#define REGM(a, b) regex_match((a), regex(b))
#define yn(a) cout << ((a) ? "yes" : "no") << "\n";
#define Yn(a) cout << ((a) ? "Yes" : "No") << "\n";
#define YN(a) cout << ((a) ? "YES" : "NO") << "\n";
#define imp(a) cout << ((a) ? "possible" : "impossible") << "\n";
#define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n";
#define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n";
#define fs(a) cout << ((a) ? "second" : "first") << "\n";
#define Fs(a) cout << ((a) ? "Second" : "First") << "\n";
#define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n";
// #define say(a) cout <<(a);
// #define sal(a) cout <<(a)<<"\n";
#define sak cout << "\n";
#define sas cout << " ";
#define sat cout << "\t";
#define dbg(a) cerr << (#a) << ": " << (a) << "\n";
#define dbgs(...) \
dal(#__VA_ARGS__); \
dal(__VA_ARGS__);
#define c2l(a) ((ll)(a - 48))
#define a2l(a) ((ll)(a - 97))
#define A2l(a) ((ll)(a - 65))
#define l2c(a) ((char)(a + 48))
#define l2a(a) ((char)(a + 97))
#define l2A(a) ((char)(a + 65))
#define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1))
#define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1))
#define Dig2(a, b) (((a) >> (b)) & 1)
#define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10)
#define Pow2(a) ((ll)(1) << (a))
#define Pow10(a) ((ll)(pow(10.0, double(a))))
#define LSB(a) ((a) & (-(a)))
/*#define llin(a) ll (a);cin >>(a);
#define llin2(a,b) ll (a),(b);cin >>(a)>>(b);
#define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c);
#define stin(a) string (a);cin >>(a);*/
#define vin(v) \
ROR((v), (i)) { cin >> (i); };
#define vllin(N, v) \
vll(v)((N)); \
vin(v);
#define vllin2(N, a, b) \
vll(a)(N), (b)(N); \
REP(i, N) { cin >> (a)[i] >> (b)[i]; };
#define vsin(N, v) \
vs(v)((N)); \
vin(v);
#define rdn(a, b) ((a) / (b))
#define rou(a, b) \
((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \
: (((a) / (b)) + 1))
#define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1))
#define powll(a, b) (ll)(pow((double)(a), (double)(b)))
#define Triangle(x1, y1, x2, y2, x3, y3) \
(((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2)))
#define tg(t, i) get<i>(t)
#define Id(x) get<0>(x)
#define Act(x) get<1>(x)
#define InvAct(x) get<2>(x)
#define mg(id, act) mt(id, act, lam(l))
// #define MonoidSet(T) TP<T, function<T(T, T)>>
#define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>>
#define CompareSet(T) TP<T, function<bool(T, T)>>
#define lam(lr) ([](auto l, auto r) { return (lr); })
#define elam(lr) ([=](auto l, auto r) { return (lr); })
#define clam(lr) ([&](auto l, auto r) { return (lr); })
#define lamr(lr) ([](auto l, auto r) { lr })
#define elamr(lr) ([=](auto l, auto r) { lr })
#define clamr(lr) ([&](auto l, auto r) { lr })
#define min(...) Operation(MIN, __VA_ARGS__)
#define max(...) Operation(MAX, __VA_ARGS__)
#define gcd(...) Operation(GCD, __VA_ARGS__)
#define lcm(...) Operation(LCM, __VA_ARGS__)
#define vmin(...) VOperation(MIN, __VA_ARGS__)
#define vmax(...) VOperation(MAX, __VA_ARGS__)
#define vgcd(...) VOperation(GCD, __VA_ARGS__)
#define vlcm(...) VOperation(LCM, __VA_ARGS__)
#define vsum(...) VOperation(ADD, __VA_ARGS__)
#define vpro(...) VOperation(MUL, __VA_ARGS__)
#define emin(a, ...) ((a) = min((a), __VA_ARGS__))
#define emax(a, ...) ((a) = max((a), __VA_ARGS__))
#define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__))
#define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__))
#define ope Operation
#define vope VOperation
#define svll SumV<ll>
#define svvll SumV2<ll>
#define li(...) \
ll __VA_ARGS__; \
Input(__VA_ARGS__);
#define si(...) \
str __VA_ARGS__; \
Input(__VA_ARGS__);
// #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__);
#define vlr(size, ...) \
vll __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vlc(size, ...) \
vll __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vli(size, ...) \
vll __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vsr(size, ...) \
vs __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vsc(size, ...) \
vs __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vsi(size, ...) \
vs __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vli2(rowSize, columnSize, ...) \
vvll __VA_ARGS__; \
vInitInput2(rowSize, columnSize, __VA_ARGS__);
#define vplr(size, ...) \
vpl __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vplc(size, ...) \
vpl __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vpli(size, ...) \
vpl __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
// const ll MOD = 924844033;
// const ll MOD = 9007199254740881;
const ll INF = 1LL << 60; // 1.15e18
const double PI = acos(-1.0);
const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1};
const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1};
const str alp = "abcdefghijklmnopqrstuvwxyz";
const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T> auto GetVectorValueType(T v) { return v; }
template <class T> auto GetVectorValueType(V<T> v) {
return GetVectorValueType(T());
}
template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) {
return in >> p.fi >> p.se;
}
template <class T> istream &operator>>(istream &in, V<T> &v) {
REP(i, v.sz) in >> v[i];
return in;
}
void Input() {}
template <class Var, class... Args> void Input(Var &var, Args &...args) {
cin >> var;
Input(args...);
}
void vInit(ll size) {}
template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) {
v.res(size);
vInit(size, args...);
}
void vInput(ll size) {}
template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) {
REP(i, size) cin >> v[i];
vInput(size, args...);
}
void vInputR(ll size) {}
template <class T, class... Args>
void vInputR(ll size, V<T> &v, Args &...args) {
REP(i, size) cin >> v[i];
vInputR(size, args...);
}
void vInputNumC(ll num) {}
template <class T, class... Args>
void vInputNumC(ll num, V<T> &v, Args &...args) {
cin >> v[num];
vInputNumC(num, args...);
}
void vInputC(ll size) {}
template <class... Args> void vInputC(ll size, Args &...args) {
REP(i, size) vInputNumC(i, args...);
}
void vInitInputR(ll size) {}
template <class... Args> void vInitInputR(ll size, Args &...args) {
vInit(size, args...);
vInputR(size, args...);
}
void vInitInputC(ll size) {}
template <class... Args> void vInitInputC(ll size, Args &...args) {
vInit(size, args...);
vInputC(size, args...);
}
void vInit2(ll rowSize, ll columnSize) {}
template <class T, class... Args>
void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) {
v.asn(rowSize, V<T>(columnSize));
vInit2(rowSize, columnSize, args...);
}
void vInput2(ll rowSize, ll columnSize) {}
template <class T, class... Args>
void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) {
REP(r, rowSize) {
REP(c, columnSize) { cin >> v[r][c]; }
}
vInput2(rowSize, columnSize, args...);
}
void vInitInput2(ll rowSize, ll columnSize) {}
template <class... Args>
void vInitInput2(ll rowSize, ll columnSize, Args &...args) {
vInit2(rowSize, columnSize, args...);
vInput2(rowSize, columnSize, args...);
}
template <class S, class T>
ostream &operator<<(ostream &out, const P<S, T> &p) {
return out << "[" << p.fi << ", " << p.se << "]";
}
template <class T> ostream &operator<<(ostream &out, V<T> &v) {
if (v.emp)
return out << "{}";
else {
auto itr = v.bgn;
out << "{" << *itr;
itr++;
while (itr != v.en) {
out << ", " << *itr;
itr++;
}
out << "}";
return out;
}
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &m) {
if (m.emp)
return out << "<[]>";
else {
auto itr = m.bgn;
out << "< [" << (itr->fi) << ": " << (itr->se);
itr++;
while (itr != m.en) {
out << "], [" << (itr->fi) << ": " << (itr->se);
itr++;
}
out << "] >";
return out;
}
}
template <class T> ostream &operator<<(ostream &out, const set<T> &s) {
if (s.emp)
return out << "<>";
else {
auto itr = s.bgn;
out << "<" << *itr;
itr++;
while (itr != s.en) {
out << ", " << *itr;
itr++;
}
out << ">";
return out;
}
}
void say() {}
template <class T> void say(T t) { cout << t; }
template <class Head, class... Body> void say(Head head, Body... body) {
cout << head << " ";
say(body...);
}
void sal() { cout << "\n"; }
template <class... Args> void sal(Args... args) {
say(args...);
cout << "\n";
}
void day() {}
template <class T> void day(T t) { cerr << t; }
template <class Head, class... Body> void day(Head head, Body... body) {
cerr << head << " ";
day(body...);
}
void dal() { cerr << "\n"; }
template <class... Args> void dal(Args... args) {
day(args...);
cerr << "\n";
}
void salv() {}
template <class T> void salv(V<T> v) {
if (v.emp)
sal();
else {
auto itr = v.bgn;
say(*itr);
itr++;
while (itr != v.en) {
sas;
say(*itr);
itr++;
}
sak;
}
}
template <class T> void salv(VV<T> v) {
if (v.emp)
sal();
else {
ROR(v, i) salv(i);
}
}
template <class T, class... Args> void salv(T v, Args... args) {
salv(v);
salv(args...);
}
template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) {
if (l < r)
swap(l, r);
return r ? Gcd(r, l % r) : l;
}
template <class L, class R> auto Lcm(L l, R r) {
if (!l || !r)
return 0;
return l / Gcd(l, r) * r;
}
/*
auto LES = mp(INF, lam(return l < r;));
auto GRT = mp(-INF, lam(return l > r;));
auto EQ = mp(0, lam(return l == r;));
auto ADD = mp(0, lam(return l + r;));
auto SUB = mp(0, lam(return l - r;));
auto MUL = mp(1, lam(return l * r;));
auto DIV = mp(1, lam(return l / r;));
auto MDL = mp(1, lam(return l % r;));
auto XOR = mp(0, lam(return l ^ r;));
auto OR = mp(0, lam(return l | r;));
auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;));
auto MIN = mp(INF, lam(return (l < r) ? l : r;));
auto MAX = mp(-INF, lam(return (l > r) ? l : r;));
auto GCD = mp(0, lam(return Gcd(l, r);));
auto LCM = mp(1, lam(return Lcm(l, r);));
*/
auto LES = mp(INF, lam(l < r));
auto GRT = mp(-INF, lam(l > r));
auto EQ = mp(0, lam(l == r));
auto ADD = mt(0, lam(l + r), lam(l - r));
auto MUL = mt(1, lam(l *r), lam(l / r));
auto XOR = mt(0, lam(l ^ r), lam(l ^ r));
auto OR = mg(0, lam(l | r));
auto AND = mg(((ll)(1) << 63) - 1, lam(l &r));
auto MIN = mg(0, lam((l < r) ? l : r));
auto MAX = mg(0, lam((l > r) ? l : r));
auto GCD = mg(0, lam(Gcd(l, r)));
auto LCM = mg(0, lam(Lcm(l, r)));
template <class OperationType> auto Operation(OperationType A) { return Id(A); }
template <class OperationType, class T> auto Operation(OperationType A, T x) {
return x;
}
template <class OperationType, class T, class... Args>
auto Operation(OperationType A, T x, Args... args) {
auto tmp = Operation(A, args...);
return Act(A)(x, tmp);
}
template <class OperationType> auto VOperation(OperationType A) {
return Id(A);
}
template <class OperationType, class T> auto VOperation(OperationType A, T x) {
return x;
}
template <class OperationType, class T>
auto VOperation(OperationType A, V<T> v) {
if (v.emp) {
decltype(GetVectorValueType(T())) tmp = Id(A);
return tmp;
}
auto tmp = VOperation(A, v[0]);
FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i]));
return tmp;
}
template <class OperationType, class T, class... Args>
auto VOperation(OperationType A, T x, Args... args) {
auto xResult = VOperation(A, x);
auto tmp = VOperation(A, args...);
return Act(A)(xResult, tmp);
}
ll Bset(ll a, ll b, ll c) {
if (c)
a |= b;
else
a &= ~b;
return a;
}
struct UFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
UFT(ll tsizeget, ll modeget = 0) {
tsize = tsizeget;
mode = modeget;
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
}
ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
par[x] += par[y];
par[y] = x;
} else {
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
}
ll size(ll x) { return -par[root(x)]; }
};
template <class T> struct pUFT {
public:
ll tsize;
ll now;
vll par;
vll rank;
vll mtime;
vvll sizepi;
VV<T> sizepv;
V<T> elm;
GroupSet(T) Add;
pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); }
void init() {
now = 0;
par.asn(tsize, -1);
rank.asn(tsize, 0);
mtime.asn(tsize, INF);
sizepi.asn(tsize, {0});
sizepv.asn(tsize, {});
}
void set(ll x, T s) {
elm[x] = s;
sizepv[x] = {s};
}
ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); }
bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); }
ll merge(ll x, ll y) {
now++;
x = root(x, now);
y = root(y, now);
if (x != y) {
if (rank[x] < rank[y]) {
elm[y] = Act(Add)(elm[x], elm[y]);
sizepi[y].pb(now);
sizepv[y].pb(elm[y]);
par[x] = y;
mtime[x] = now;
} else {
elm[x] = Act(Add)(elm[x], elm[y]);
sizepi[x].pb(now);
sizepv[x].pb(elm[x]);
par[y] = x;
mtime[y] = now;
if (rank[x] == rank[y])
rank[x]++;
}
}
return now;
}
T size(ll x, ll t) {
x = root(x, t);
return sizepv[x][UPB(sizepi[x], t) - 1];
}
};
struct wUFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
vll dweight;
wUFT(ll tsizeget, ll modeget = 0) {
tsize = tsizeget;
mode = modeget;
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
dweight.asn(tsize, 0);
}
ll root(ll x) {
if (par[x] < 0)
return x;
else {
ll r = root(par[x]);
dweight[x] += dweight[par[x]];
return par[x] = r;
}
}
ll weight(ll x) {
root(x);
return dweight[x];
}
ll diff(ll x, ll y) { return weight(y) - weight(x); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y, ll w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
par[x] += par[y];
par[y] = x;
} else {
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = y;
dweight[x] = -w;
} else {
par[x] += par[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
dweight[y] = w;
}
}
}
ll size(ll x) { return -par[root(x)]; }
};
template <class T> struct sUFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
GroupSet(T) Add;
V<T> elm;
sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0)
: tsize(tsize), Add(Add), mode(mode) {
init();
}
void init() {
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
elm.asn(tsize, Id(Add));
}
ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
elm[x] = Act(Add)(elm[x], elm[y]);
par[y] = x;
} else {
if (rank[x] < rank[y]) {
elm[y] = Act(Add)(elm[x], elm[y]);
par[x] = y;
} else {
elm[x] = Act(Add)(elm[x], elm[y]);
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
}
T size(ll x) { return elm[root(x)]; }
T &operator[](ll x) { return elm[x]; }
};
template <typename valtype> class SegT {
public:
ll size;
vector<valtype> v;
valtype initv;
function<valtype(valtype x, valtype y)> calc;
SegT() {}
SegT(const SegT &segt) {}
SegT(ll sizeget, ll modeget = 0) {
sizeset(sizeget);
modeset(modeget);
init();
}
SegT(vector<valtype> cpyvec, ll modeget = 0) {
sizeset(cpyvec.sz);
modeset(modeget);
init();
copy(cpyvec);
}
SegT(ll sizeget, valtype initvget,
function<valtype(valtype x, valtype y)> calcget) {
sizeset(sizeget);
initv = initvget;
calc = calcget;
init();
}
SegT(vector<valtype> cpyvec, valtype initvget,
function<valtype(valtype x, valtype y)> calcget) {
sizeset(cpyvec.sz);
initv = initvget;
calc = calcget;
init();
copy(cpyvec);
}
void sizeset(ll rsize) {
size = DigN2(rsize);
if (rsize == Pow2(size - 1))
size--;
return;
}
void modeset(ll mode) {
switch (mode) {
case 0:
initv = 0;
calc = [](valtype x, valtype y) { return x + y; };
break;
case 1:
initv = INF;
calc = [](valtype x, valtype y) { return min(x, y); };
break;
case 2:
initv = -INF;
calc = [](valtype x, valtype y) { return max(x, y); };
break;
}
return;
}
void init() { v.asn(Pow2(size + 1) - 1, initv); }
void copy(vector<valtype> cpyvec) {
REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]);
}
ll i2v(ll i) const {
if (i < 0 || i >= Pow2(size))
return -1;
return Pow2(size) + i - 1;
}
ll top(ll i) const {
if (i == 0)
return -1;
return (i - 1) / 2;
}
pl bot(ll i) const {
if (i + 1 >= Pow2(size))
return mp(-1, -1);
return mp(2 * i + 1, 2 * i + 2);
}
void set(ll i, valtype x) {
i = i2v(i);
v[i] = x;
while (i > 0) {
i = top(i);
v[i] = calc(v[bot(i).fi], v[bot(i).se]);
}
return;
}
void add(ll i, valtype x) {
set(i, v[i2v(i)] + x);
return;
}
valtype operator[](const ll &i) const { return v[i2v(i)]; }
// valtype que(ll a = 0, ll b = Pow2(size) - 1) {
valtype que(ll a, ll b) {
if (a == b)
return v[i2v(a)];
if (a > b)
return initv; // swap(a, b);
valtype ans = initv;
ll ai = i2v(a);
ll bi = i2v(b);
FOR(i, 1, size + 1) {
if (a > b)
break;
if (a % Pow2(i)) {
ans = calc(ans, v[ai]);
a += Pow2(i - 1);
ai = top(ai) + 1;
} else {
ai = top(ai);
}
if (a > b)
break;
if ((b + 1) % Pow2(i)) {
ans = calc(ans, v[bi]);
b -= Pow2(i - 1);
bi = top(bi) - 1;
} else {
bi = top(bi);
}
if (a > b)
break;
}
return ans;
}
valtype que(ll b) { return que(0, b); }
valtype que() { return que(0, Pow2(size) - 1); }
};
/*template <class Type> class DP {
public:
vector<Type> v;
Type initv;
vll size, block;
DP() {}
DP(const DP &dp) {}
template<class... Args> DP(Args... args) {
block.asn(1, 1);
Initialize(args...);
}
void Initialize(Type initv_) {
initv = initv_;
v.asn(block.bk, initv);
}
template<class... Args> void Initialize(ll val, Args... args) {
size.pb(val);
block.pb(block.bk*val);
Initialize(args...);
}
};*/
pl Bezout(ll a, ll b) {
if (b != 0) {
pl xy;
xy = Bezout(b, a % b);
return mp(xy.se, xy.fi - ((a / b) * xy.se));
} else {
return mp(1, 0);
}
}
pl Bez(ll a, ll b, ll c) {
pl xy;
ll x, y, z, gc;
xy = Bezout(a, b);
gc = gcd(a, b);
if (c % gc != 0)
return mp(-1, -1);
x = xy.fi * (c / gc);
y = xy.se * (c / gc);
if (x < 0)
z = rup(-x, (b / gc));
if (x >= 0)
z = -x / (b / gc);
x += z * (b / gc);
y -= z * (a / gc);
return mp(x, y);
}
ll DigS10(ll n) {
ll ans = 0;
while (1) {
ans += n % 10;
n /= 10;
if (!n)
break;
}
return ans;
}
ll isP(ll n) {
if (n <= 1)
return 0;
FOR(i, 2, (ll)sqrt(n) + 1) {
if (n % i == 0)
return 0;
}
return 1;
}
ll Tot(ll n) {
if (n <= 0)
return 0;
ll ans = n, x = 2;
while (x * x <= n) {
if (n % x == 0) {
ans -= ans / x;
while (n % x == 0)
n /= x;
}
x++;
}
if (n > 1)
ans -= ans / n;
return ans;
}
template <class T> struct Graph {
public:
ll vSize;
ll eMode;
ll mapMode;
GroupSet(T) Add;
CompareSet(T) Less;
CompareSet(T) Equal;
VV<P<T, ll>> adj;
map<pl, T> len;
Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD,
CompareSet(T) Less = LES, CompareSet(T) Equal = EQ)
: vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less),
Equal(Equal) {}
void Init() { adj.asn(vSize, V<P<T, ll>>()); }
void AddE(ll x, ll y, T cost) {
iAddE(x, y, cost);
if (!eMode)
iAddE(y, x, cost);
}
void iAddE(ll x, ll y, T cost) {
adj[x].pb(mp(cost, y));
if (mapMode)
len[mp(x, y)] = cost;
}
P<bool, T> getE(ll x, ll y) {
if (!len.count(mp(x, y)))
return mp(false, Id(Less));
return mp(true, len[mp(x, y)]);
}
V<T> Dijk(ll x) {
V<T> ans(vSize, Id(Less));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans[now.se] = segt[now.se].fi;
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se));
continue;
}
if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) {
segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se));
continue;
}
}
}
return ans;
}
V<P<T, vll>> rDijk(ll x) {
V<P<T, vll>> ans(vSize, mp(Id(Less), vll()));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans[now.se].fi = segt[now.se].fi;
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se));
ans[i.se].se = {now.se};
continue;
}
if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) {
segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se));
ans[i.se].se = {now.se};
continue;
}
if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) {
ans[i.se].se.pb(now.se);
continue;
}
}
}
return ans;
}
T Prim(ll x = 0) {
T ans = Id(Add);
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans = Act(Add)(ans, segt[now.se].fi);
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, i);
continue;
}
if (Act(Less)(i.fi, segt[i.se].fi)) {
segt.set(i.se, i);
continue;
}
}
}
return ans;
}
P<T, V<P<T, vll>>> rPrim(ll x = 0) {
P<T, V<P<T, vll>>> ans =
mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll())));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans.se[now.se].fi = segt[now.se].fi;
ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi);
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, i);
ans.se[i.se].se = {now.se};
continue;
}
if (Act(Less)(i.fi, segt[i.se].fi)) {
segt.set(i.se, i);
ans.se[i.se].se = {now.se};
continue;
}
if (Act(Equal)(i.fi, segt[i.se].fi)) {
ans.se[i.se].se.pb(now.se);
continue;
}
}
}
return ans;
}
};
template <class T> struct Sum {
public:
V<T> v, s;
ll size;
GroupSet(T) Add;
Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) {
Init();
Calc();
}
void Init() { s.asn(size + 1, Id(Add)); }
void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); }
T operator()(ll x) {
if (x < -1)
x = -1;
if (x > size - 1)
x = size - 1;
return s[x + 1];
}
T operator()(ll l, ll r) {
if (l < 0)
l = 0;
if (r >= size)
r = size - 1;
if (l > r)
return Id(Add);
return InvAct(Add)(s[r + 1], s[l]);
}
};
using sumll = Sum<ll>;
template <class T> struct Sum2 {
public:
VV<T> v, s;
ll RowSize, ColumnSize;
GroupSet(T) Add;
Sum2(VV<T> v, GroupSet(T) Add = ADD)
: v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) {
Init();
Calc();
}
void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); }
void Calc() {
REP1(r, RowSize) {
REP1(c, ColumnSize) {
// s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r -
//1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)),
//operator()(r - 2, c - 2));
s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]);
}
}
REP1(r, RowSize) {
REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); }
}
}
T operator()(ll r, ll c) {
if (r < -1)
r = -1;
if (c < -1)
c = -1;
if (r > RowSize - 1)
r = RowSize - 1;
if (c > ColumnSize - 1)
c = ColumnSize - 1;
return s[r + 1][c + 1];
}
T operator()(ll r1, ll c1, ll r2, ll c2) {
if (r1 < 0)
r1 = 0;
if (c1 < 0)
c1 = 0;
if (r2 >= RowSize)
r2 = RowSize - 1;
if (c2 >= ColumnSize)
c2 = ColumnSize - 1;
if (r1 > r2)
return Id(Add);
if (c1 > c2)
return Id(Add);
return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]),
Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1]));
}
};
using sumll2 = Sum2<ll>;
template <class T> struct Point2 {
public:
VV<T> v;
ll h, w;
Point2() : h(0), w(0) {}
Point2(ll h, ll w) : h(h), w(w) { asn(h, w); }
Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); }
Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) {
asn(h, w);
copy(cv);
}
void assign(ll h, ll w) { v.asn(h, V<T>(w)); }
void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); }
void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; }
T &operator()(ll h, ll w) { return v[h][w]; }
T &operator()(pl p) { return v[p.fi][p.se]; }
T &operator[](pl p) { return v[p.fi][p.se]; }
};
template <class T> using P2 = Point2<T>;
template <ll Mod> struct Modll {
public:
ll v;
Modll() : v(0) {}
Modll(ll _v) { set(_v % Mod + Mod); }
Modll &set(ll _v) {
v = (_v < Mod) ? _v : (_v - Mod);
return *this;
}
Modll pow(ll n) const {
Modll x = *this, ans = 1;
while (n) {
if (n & 1)
ans *= x;
x *= x;
n >>= 1;
}
return ans;
}
Modll inverse() const { return (*this).pow(Mod - 2); }
Modll operator+(const Modll &m) const { return Modll().set(v + m.v); }
Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); }
Modll operator*(const Modll &m) const {
return Modll().set((ull(v) * m.v) % Mod);
}
Modll operator/(const Modll &m) const { return *this * m.inv; }
Modll &operator+=(const Modll &m) { return *this = *this + m; }
Modll &operator-=(const Modll &m) { return *this = *this - m; }
Modll &operator*=(const Modll &m) { return *this = *this * m; }
Modll &operator/=(const Modll &m) { return *this = *this / m; }
Modll operator-() const { return Modll(0) - *this; }
explicit operator bool() const { return v != 0; }
friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; }
friend ostream &operator<<(ostream &out, const Modll &m) {
return out << m.v;
}
};
using mll = Modll<MOD>;
using vmll = V<mll>;
using vvmll = V<vmll>;
using vvvmll = V<vvmll>;
vmll MFactMemo(2, 1);
vmll MIFactMemo(2, 1);
mll mFact(ll n) {
if (MFactMemo.sz <= n) {
ll oldsize = MFactMemo.sz;
MFactMemo.res(n + 1, 1);
FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i;
}
return MFactMemo[n];
}
mll miFact(ll n) {
if (MIFactMemo.sz <= n) {
ll oldsize = MIFactMemo.sz;
MIFactMemo.res(n + 1, 1);
MIFactMemo.bk = mFact(n).inv;
rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i;
}
return MIFactMemo[n];
}
mll mComb(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return mFact(n) * miFact(k) * miFact(n - k);
}
ll LIS(vll v, ll m = 0) {
if (v.sz > 0) {
ll ans = 0;
vll dp(v.sz, INF);
FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; }
FOR(i, 0, v.sz - 1) {
if (dp[i] == INF)
break;
ans++;
}
return ans;
} else {
return 0;
}
}
ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) {
ll ng = mi - 1, ok = ma, mid;
while (ok - ng > 1) {
mid = (ng + ok) / 2;
(f(mid) ? ok : ng) = mid;
}
return ok;
}
template <class T, class M = decltype(MUL), class S = decltype(ADD)>
VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) {
VV<T> ans;
ll ii = A.sz;
if (!ii)
return ans;
ll jj = A[0].sz;
if (!jj)
return ans;
ll jj2 = B.sz;
if (!jj2)
return ans;
if (jj != jj2)
return ans;
ll kk = B[0].sz;
if (!kk)
return ans;
ans.asn(ii, V<T>(kk, 0));
REP(i, ii) {
REP(k, kk) {
REP(j, jj) {
ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k]));
}
}
}
return ans;
}
vvll CombMemo(1000, vll(1000, -1));
ll Comb(ll n, ll k) {
if ((n < 0) || (k < 0))
return 0;
if (CombMemo[n][k] == -1) {
if (n < k)
CombMemo[n][k] = 0;
else {
if (n == 0)
CombMemo[n][k] = 1;
else if (k == 0)
CombMemo[n][k] = 1;
else if (n == k)
CombMemo[n][k] = 1;
else
CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k);
}
}
return CombMemo[n][k];
}
template <class T> map<T, ll> Dict(V<T> v) {
map<T, ll> m;
if (!v.sz)
return m;
SORT(v);
UNIQUE(v);
REP(i, v.sz) { m[v[i]] = i; }
return m;
}
template <class T> vll Cmprs(V<T> v) {
auto m = Dict(v);
vll ans(v.sz);
REP(i, v.sz) { ans[i] = m[v[i]]; }
return ans;
}
template <class T> vll PCmprs(V<T> v) {
if (v.sz == 0)
return V<T>();
vll tmp(v.sz);
vll ans(v.sz);
IOTA(tmp, 0);
IOTA(ans, 0);
sort(tmp.bgn, tmp.en, clam(v[l] < v[r]));
sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r]));
return ans;
}
ll BblCnt(vll rv) {
vll v = PCmprs(rv);
SegT<ll> b(v.sz, 0);
ll ans = 0;
REP(i, v.sz) {
ans += (i - b.que(0, v[i]));
b.add(v[i], 1);
}
return ans;
}
pl NGrid(pl p, ll i, ll H, ll W) {
p = mp(p.fi + DX[i], p.se + DY[i]);
if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W)
return mp(INF, INF);
return p;
}
vvll llGrid(vs v) {
vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0));
REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#');
return r;
}
template <class T> auto ven(T val) { return val; }
template <> auto ven<int>(int val) { return (ll)val; }
template <class T, class... Args> auto ven(T val, Args... args) {
auto tmp = ven(args...);
return V<decltype(tmp)>(val, tmp);
}
template <class T> void zind(T &v) { v--; }
template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); }
template <class T, class... Args> void zind(T &v, Args &...args) {
zind(v);
zind(args...);
}
void Solve();
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(20) << fixed;
Solve();
}
void Solve() {
li(N, K);
vlc(N, t, d);
vpl sushi(N);
REP(i, N) sushi[i] = mp(d[i], t[i] - 1);
sort(sushi.bgn, sushi.en, lam(l.fi > r.fi));
vll used(N, 0);
ll nowp = 0;
vll yobi;
REP(i, K) {
if (used[sushi[i].se])
yobi.pb(sushi[i].fi);
used[sushi[i].se] = 1;
nowp += sushi[i].fi;
}
ll uc = 0;
REP(i, N) uc += used[i];
nowp += uc * uc;
ll ma = nowp;
ll x = 0;
while (x < yobi.sz) {
if (!used[sushi[K].se]) {
used[sushi[K].se] = 1;
nowp -= uc * uc;
uc++;
nowp += uc * uc;
nowp -= yobi[yobi.sz - 1 - x];
nowp += sushi[K].fi;
emax(ma, nowp);
x++;
}
K++;
}
sal(ma);
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
template <class S, class T> using P = pair<S, T>;
template <class... T> using TP = tuple<T...>;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using str = string;
using vll = V<ll>;
using vvll = V<vll>;
using vvvll = V<vvll>;
using pl = P<ll, ll>;
using tl = TP<ll, ll, ll>;
using vpl = V<pl>;
using vvpl = V<vpl>;
using vtl = V<tl>;
using vvtl = V<vtl>;
using vs = V<str>;
using vvs = V<vs>;
using vd = V<dbl>;
using vvd = V<vd>;
using vvvd = V<vvd>;
using qll = queue<ll>;
using qpl = queue<pl>;
using stll = stack<ll>;
using stpl = stack<pl>;
using mapll = map<ll, ll>;
using setll = set<ll>;
using pqll = priority_queue<ll>;
// #define int ll
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define sz size()
#define bgn begin()
#define en end()
#define asn assign
#define emp empty()
#define fr front()
#define bk back()
#define clr clear()
#define ins insert
#define ers erase
#define res resize
#define tp top()
#define p_q priority_queue
#define inv inverse()
#define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++)
#define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--)
#define REP(i, a) FOR((i), 0, (ll)(a)-1)
#define REP0(i, a) FOR((i), 0, (ll)(a))
#define REP1(i, a) FOR((i), 1, (ll)(a))
#define rREP(i, a) rFOR((i), 0, (ll)(a)-1)
#define rREP0(i, a) rFOR((i), 0, (ll)(a))
#define rREP1(i, a) rFOR((i), 1, (ll)(a))
#define ROR(v, i) for (auto &(i) : (v))
#define IOTA(a, n) iota((a).bgn, (a).en, (n))
#define SORT(a) sort((a).bgn, (a).en)
#define rSORT(a) sort((a).rbegin(), (a).rend())
#define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en)
#define PREVP(a) prev_permutation((a).bgn, (a).en)
#define NEXTP(a) next_permutation((a).bgn, (a).en)
#define BINS(a, b) binary_search((a).bgn, (a).en, (b))
#define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn)
#define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn)
#define CNT(a, b) count((a).bgn, (a).en, b)
#define SUM(a) accumulate((a).bgn, (a).en, 0)
#define REV(a) reverse((a).bgn, (a).en)
#define REGS(a, b) regex_search((a), regex(b))
#define REGM(a, b) regex_match((a), regex(b))
#define yn(a) cout << ((a) ? "yes" : "no") << "\n";
#define Yn(a) cout << ((a) ? "Yes" : "No") << "\n";
#define YN(a) cout << ((a) ? "YES" : "NO") << "\n";
#define imp(a) cout << ((a) ? "possible" : "impossible") << "\n";
#define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n";
#define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n";
#define fs(a) cout << ((a) ? "second" : "first") << "\n";
#define Fs(a) cout << ((a) ? "Second" : "First") << "\n";
#define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n";
// #define say(a) cout <<(a);
// #define sal(a) cout <<(a)<<"\n";
#define sak cout << "\n";
#define sas cout << " ";
#define sat cout << "\t";
#define dbg(a) cerr << (#a) << ": " << (a) << "\n";
#define dbgs(...) \
dal(#__VA_ARGS__); \
dal(__VA_ARGS__);
#define c2l(a) ((ll)(a - 48))
#define a2l(a) ((ll)(a - 97))
#define A2l(a) ((ll)(a - 65))
#define l2c(a) ((char)(a + 48))
#define l2a(a) ((char)(a + 97))
#define l2A(a) ((char)(a + 65))
#define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1))
#define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1))
#define Dig2(a, b) (((a) >> (b)) & 1)
#define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10)
#define Pow2(a) ((ll)(1) << (a))
#define Pow10(a) ((ll)(pow(10.0, double(a))))
#define LSB(a) ((a) & (-(a)))
/*#define llin(a) ll (a);cin >>(a);
#define llin2(a,b) ll (a),(b);cin >>(a)>>(b);
#define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c);
#define stin(a) string (a);cin >>(a);*/
#define vin(v) \
ROR((v), (i)) { cin >> (i); };
#define vllin(N, v) \
vll(v)((N)); \
vin(v);
#define vllin2(N, a, b) \
vll(a)(N), (b)(N); \
REP(i, N) { cin >> (a)[i] >> (b)[i]; };
#define vsin(N, v) \
vs(v)((N)); \
vin(v);
#define rdn(a, b) ((a) / (b))
#define rou(a, b) \
((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \
: (((a) / (b)) + 1))
#define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1))
#define powll(a, b) (ll)(pow((double)(a), (double)(b)))
#define Triangle(x1, y1, x2, y2, x3, y3) \
(((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2)))
#define tg(t, i) get<i>(t)
#define Id(x) get<0>(x)
#define Act(x) get<1>(x)
#define InvAct(x) get<2>(x)
#define mg(id, act) mt(id, act, lam(l))
// #define MonoidSet(T) TP<T, function<T(T, T)>>
#define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>>
#define CompareSet(T) TP<T, function<bool(T, T)>>
#define lam(lr) ([](auto l, auto r) { return (lr); })
#define elam(lr) ([=](auto l, auto r) { return (lr); })
#define clam(lr) ([&](auto l, auto r) { return (lr); })
#define lamr(lr) ([](auto l, auto r) { lr })
#define elamr(lr) ([=](auto l, auto r) { lr })
#define clamr(lr) ([&](auto l, auto r) { lr })
#define min(...) Operation(MIN, __VA_ARGS__)
#define max(...) Operation(MAX, __VA_ARGS__)
#define gcd(...) Operation(GCD, __VA_ARGS__)
#define lcm(...) Operation(LCM, __VA_ARGS__)
#define vmin(...) VOperation(MIN, __VA_ARGS__)
#define vmax(...) VOperation(MAX, __VA_ARGS__)
#define vgcd(...) VOperation(GCD, __VA_ARGS__)
#define vlcm(...) VOperation(LCM, __VA_ARGS__)
#define vsum(...) VOperation(ADD, __VA_ARGS__)
#define vpro(...) VOperation(MUL, __VA_ARGS__)
#define emin(a, ...) ((a) = min((a), __VA_ARGS__))
#define emax(a, ...) ((a) = max((a), __VA_ARGS__))
#define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__))
#define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__))
#define ope Operation
#define vope VOperation
#define svll SumV<ll>
#define svvll SumV2<ll>
#define li(...) \
ll __VA_ARGS__; \
Input(__VA_ARGS__);
#define si(...) \
str __VA_ARGS__; \
Input(__VA_ARGS__);
// #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__);
#define vlr(size, ...) \
vll __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vlc(size, ...) \
vll __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vli(size, ...) \
vll __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vsr(size, ...) \
vs __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vsc(size, ...) \
vs __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vsi(size, ...) \
vs __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vli2(rowSize, columnSize, ...) \
vvll __VA_ARGS__; \
vInitInput2(rowSize, columnSize, __VA_ARGS__);
#define vplr(size, ...) \
vpl __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
#define vplc(size, ...) \
vpl __VA_ARGS__; \
vInitInputC(size, __VA_ARGS__);
#define vpli(size, ...) \
vpl __VA_ARGS__; \
vInitInputR(size, __VA_ARGS__);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
// const ll MOD = 924844033;
// const ll MOD = 9007199254740881;
const ll INF = 1LL << 60; // 1.15e18
const double PI = acos(-1.0);
const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1};
const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1};
const str alp = "abcdefghijklmnopqrstuvwxyz";
const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T> auto GetVectorValueType(T v) { return v; }
template <class T> auto GetVectorValueType(V<T> v) {
return GetVectorValueType(T());
}
template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) {
return in >> p.fi >> p.se;
}
template <class T> istream &operator>>(istream &in, V<T> &v) {
REP(i, v.sz) in >> v[i];
return in;
}
void Input() {}
template <class Var, class... Args> void Input(Var &var, Args &...args) {
cin >> var;
Input(args...);
}
void vInit(ll size) {}
template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) {
v.res(size);
vInit(size, args...);
}
void vInput(ll size) {}
template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) {
REP(i, size) cin >> v[i];
vInput(size, args...);
}
void vInputR(ll size) {}
template <class T, class... Args>
void vInputR(ll size, V<T> &v, Args &...args) {
REP(i, size) cin >> v[i];
vInputR(size, args...);
}
void vInputNumC(ll num) {}
template <class T, class... Args>
void vInputNumC(ll num, V<T> &v, Args &...args) {
cin >> v[num];
vInputNumC(num, args...);
}
void vInputC(ll size) {}
template <class... Args> void vInputC(ll size, Args &...args) {
REP(i, size) vInputNumC(i, args...);
}
void vInitInputR(ll size) {}
template <class... Args> void vInitInputR(ll size, Args &...args) {
vInit(size, args...);
vInputR(size, args...);
}
void vInitInputC(ll size) {}
template <class... Args> void vInitInputC(ll size, Args &...args) {
vInit(size, args...);
vInputC(size, args...);
}
void vInit2(ll rowSize, ll columnSize) {}
template <class T, class... Args>
void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) {
v.asn(rowSize, V<T>(columnSize));
vInit2(rowSize, columnSize, args...);
}
void vInput2(ll rowSize, ll columnSize) {}
template <class T, class... Args>
void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) {
REP(r, rowSize) {
REP(c, columnSize) { cin >> v[r][c]; }
}
vInput2(rowSize, columnSize, args...);
}
void vInitInput2(ll rowSize, ll columnSize) {}
template <class... Args>
void vInitInput2(ll rowSize, ll columnSize, Args &...args) {
vInit2(rowSize, columnSize, args...);
vInput2(rowSize, columnSize, args...);
}
template <class S, class T>
ostream &operator<<(ostream &out, const P<S, T> &p) {
return out << "[" << p.fi << ", " << p.se << "]";
}
template <class T> ostream &operator<<(ostream &out, V<T> &v) {
if (v.emp)
return out << "{}";
else {
auto itr = v.bgn;
out << "{" << *itr;
itr++;
while (itr != v.en) {
out << ", " << *itr;
itr++;
}
out << "}";
return out;
}
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &m) {
if (m.emp)
return out << "<[]>";
else {
auto itr = m.bgn;
out << "< [" << (itr->fi) << ": " << (itr->se);
itr++;
while (itr != m.en) {
out << "], [" << (itr->fi) << ": " << (itr->se);
itr++;
}
out << "] >";
return out;
}
}
template <class T> ostream &operator<<(ostream &out, const set<T> &s) {
if (s.emp)
return out << "<>";
else {
auto itr = s.bgn;
out << "<" << *itr;
itr++;
while (itr != s.en) {
out << ", " << *itr;
itr++;
}
out << ">";
return out;
}
}
void say() {}
template <class T> void say(T t) { cout << t; }
template <class Head, class... Body> void say(Head head, Body... body) {
cout << head << " ";
say(body...);
}
void sal() { cout << "\n"; }
template <class... Args> void sal(Args... args) {
say(args...);
cout << "\n";
}
void day() {}
template <class T> void day(T t) { cerr << t; }
template <class Head, class... Body> void day(Head head, Body... body) {
cerr << head << " ";
day(body...);
}
void dal() { cerr << "\n"; }
template <class... Args> void dal(Args... args) {
day(args...);
cerr << "\n";
}
void salv() {}
template <class T> void salv(V<T> v) {
if (v.emp)
sal();
else {
auto itr = v.bgn;
say(*itr);
itr++;
while (itr != v.en) {
sas;
say(*itr);
itr++;
}
sak;
}
}
template <class T> void salv(VV<T> v) {
if (v.emp)
sal();
else {
ROR(v, i) salv(i);
}
}
template <class T, class... Args> void salv(T v, Args... args) {
salv(v);
salv(args...);
}
template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) {
if (l < r)
swap(l, r);
return r ? Gcd(r, l % r) : l;
}
template <class L, class R> auto Lcm(L l, R r) {
if (!l || !r)
return 0;
return l / Gcd(l, r) * r;
}
/*
auto LES = mp(INF, lam(return l < r;));
auto GRT = mp(-INF, lam(return l > r;));
auto EQ = mp(0, lam(return l == r;));
auto ADD = mp(0, lam(return l + r;));
auto SUB = mp(0, lam(return l - r;));
auto MUL = mp(1, lam(return l * r;));
auto DIV = mp(1, lam(return l / r;));
auto MDL = mp(1, lam(return l % r;));
auto XOR = mp(0, lam(return l ^ r;));
auto OR = mp(0, lam(return l | r;));
auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;));
auto MIN = mp(INF, lam(return (l < r) ? l : r;));
auto MAX = mp(-INF, lam(return (l > r) ? l : r;));
auto GCD = mp(0, lam(return Gcd(l, r);));
auto LCM = mp(1, lam(return Lcm(l, r);));
*/
auto LES = mp(INF, lam(l < r));
auto GRT = mp(-INF, lam(l > r));
auto EQ = mp(0, lam(l == r));
auto ADD = mt(0, lam(l + r), lam(l - r));
auto MUL = mt(1, lam(l *r), lam(l / r));
auto XOR = mt(0, lam(l ^ r), lam(l ^ r));
auto OR = mg(0, lam(l | r));
auto AND = mg(((ll)(1) << 63) - 1, lam(l &r));
auto MIN = mg(0, lam((l < r) ? l : r));
auto MAX = mg(0, lam((l > r) ? l : r));
auto GCD = mg(0, lam(Gcd(l, r)));
auto LCM = mg(0, lam(Lcm(l, r)));
template <class OperationType> auto Operation(OperationType A) { return Id(A); }
template <class OperationType, class T> auto Operation(OperationType A, T x) {
return x;
}
template <class OperationType, class T, class... Args>
auto Operation(OperationType A, T x, Args... args) {
auto tmp = Operation(A, args...);
return Act(A)(x, tmp);
}
template <class OperationType> auto VOperation(OperationType A) {
return Id(A);
}
template <class OperationType, class T> auto VOperation(OperationType A, T x) {
return x;
}
template <class OperationType, class T>
auto VOperation(OperationType A, V<T> v) {
if (v.emp) {
decltype(GetVectorValueType(T())) tmp = Id(A);
return tmp;
}
auto tmp = VOperation(A, v[0]);
FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i]));
return tmp;
}
template <class OperationType, class T, class... Args>
auto VOperation(OperationType A, T x, Args... args) {
auto xResult = VOperation(A, x);
auto tmp = VOperation(A, args...);
return Act(A)(xResult, tmp);
}
ll Bset(ll a, ll b, ll c) {
if (c)
a |= b;
else
a &= ~b;
return a;
}
struct UFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
UFT(ll tsizeget, ll modeget = 0) {
tsize = tsizeget;
mode = modeget;
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
}
ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
par[x] += par[y];
par[y] = x;
} else {
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
}
ll size(ll x) { return -par[root(x)]; }
};
template <class T> struct pUFT {
public:
ll tsize;
ll now;
vll par;
vll rank;
vll mtime;
vvll sizepi;
VV<T> sizepv;
V<T> elm;
GroupSet(T) Add;
pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); }
void init() {
now = 0;
par.asn(tsize, -1);
rank.asn(tsize, 0);
mtime.asn(tsize, INF);
sizepi.asn(tsize, {0});
sizepv.asn(tsize, {});
}
void set(ll x, T s) {
elm[x] = s;
sizepv[x] = {s};
}
ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); }
bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); }
ll merge(ll x, ll y) {
now++;
x = root(x, now);
y = root(y, now);
if (x != y) {
if (rank[x] < rank[y]) {
elm[y] = Act(Add)(elm[x], elm[y]);
sizepi[y].pb(now);
sizepv[y].pb(elm[y]);
par[x] = y;
mtime[x] = now;
} else {
elm[x] = Act(Add)(elm[x], elm[y]);
sizepi[x].pb(now);
sizepv[x].pb(elm[x]);
par[y] = x;
mtime[y] = now;
if (rank[x] == rank[y])
rank[x]++;
}
}
return now;
}
T size(ll x, ll t) {
x = root(x, t);
return sizepv[x][UPB(sizepi[x], t) - 1];
}
};
struct wUFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
vll dweight;
wUFT(ll tsizeget, ll modeget = 0) {
tsize = tsizeget;
mode = modeget;
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
dweight.asn(tsize, 0);
}
ll root(ll x) {
if (par[x] < 0)
return x;
else {
ll r = root(par[x]);
dweight[x] += dweight[par[x]];
return par[x] = r;
}
}
ll weight(ll x) {
root(x);
return dweight[x];
}
ll diff(ll x, ll y) { return weight(y) - weight(x); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y, ll w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
par[x] += par[y];
par[y] = x;
} else {
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = y;
dweight[x] = -w;
} else {
par[x] += par[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
dweight[y] = w;
}
}
}
ll size(ll x) { return -par[root(x)]; }
};
template <class T> struct sUFT {
public:
ll tsize;
ll mode;
vll par;
vll rank;
GroupSet(T) Add;
V<T> elm;
sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0)
: tsize(tsize), Add(Add), mode(mode) {
init();
}
void init() {
par.asn(tsize, -1);
if (!mode)
rank.res(tsize, 0);
elm.asn(tsize, Id(Add));
}
ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
bool isRoot(ll x) { return x == root(x); }
bool same(ll x, ll y) { return root(x) == root(y); }
void merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (mode) {
elm[x] = Act(Add)(elm[x], elm[y]);
par[y] = x;
} else {
if (rank[x] < rank[y]) {
elm[y] = Act(Add)(elm[x], elm[y]);
par[x] = y;
} else {
elm[x] = Act(Add)(elm[x], elm[y]);
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
}
T size(ll x) { return elm[root(x)]; }
T &operator[](ll x) { return elm[x]; }
};
template <typename valtype> class SegT {
public:
ll size;
vector<valtype> v;
valtype initv;
function<valtype(valtype x, valtype y)> calc;
SegT() {}
SegT(const SegT &segt) {}
SegT(ll sizeget, ll modeget = 0) {
sizeset(sizeget);
modeset(modeget);
init();
}
SegT(vector<valtype> cpyvec, ll modeget = 0) {
sizeset(cpyvec.sz);
modeset(modeget);
init();
copy(cpyvec);
}
SegT(ll sizeget, valtype initvget,
function<valtype(valtype x, valtype y)> calcget) {
sizeset(sizeget);
initv = initvget;
calc = calcget;
init();
}
SegT(vector<valtype> cpyvec, valtype initvget,
function<valtype(valtype x, valtype y)> calcget) {
sizeset(cpyvec.sz);
initv = initvget;
calc = calcget;
init();
copy(cpyvec);
}
void sizeset(ll rsize) {
size = DigN2(rsize);
if (rsize == Pow2(size - 1))
size--;
return;
}
void modeset(ll mode) {
switch (mode) {
case 0:
initv = 0;
calc = [](valtype x, valtype y) { return x + y; };
break;
case 1:
initv = INF;
calc = [](valtype x, valtype y) { return min(x, y); };
break;
case 2:
initv = -INF;
calc = [](valtype x, valtype y) { return max(x, y); };
break;
}
return;
}
void init() { v.asn(Pow2(size + 1) - 1, initv); }
void copy(vector<valtype> cpyvec) {
REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]);
}
ll i2v(ll i) const {
if (i < 0 || i >= Pow2(size))
return -1;
return Pow2(size) + i - 1;
}
ll top(ll i) const {
if (i == 0)
return -1;
return (i - 1) / 2;
}
pl bot(ll i) const {
if (i + 1 >= Pow2(size))
return mp(-1, -1);
return mp(2 * i + 1, 2 * i + 2);
}
void set(ll i, valtype x) {
i = i2v(i);
v[i] = x;
while (i > 0) {
i = top(i);
v[i] = calc(v[bot(i).fi], v[bot(i).se]);
}
return;
}
void add(ll i, valtype x) {
set(i, v[i2v(i)] + x);
return;
}
valtype operator[](const ll &i) const { return v[i2v(i)]; }
// valtype que(ll a = 0, ll b = Pow2(size) - 1) {
valtype que(ll a, ll b) {
if (a == b)
return v[i2v(a)];
if (a > b)
return initv; // swap(a, b);
valtype ans = initv;
ll ai = i2v(a);
ll bi = i2v(b);
FOR(i, 1, size + 1) {
if (a > b)
break;
if (a % Pow2(i)) {
ans = calc(ans, v[ai]);
a += Pow2(i - 1);
ai = top(ai) + 1;
} else {
ai = top(ai);
}
if (a > b)
break;
if ((b + 1) % Pow2(i)) {
ans = calc(ans, v[bi]);
b -= Pow2(i - 1);
bi = top(bi) - 1;
} else {
bi = top(bi);
}
if (a > b)
break;
}
return ans;
}
valtype que(ll b) { return que(0, b); }
valtype que() { return que(0, Pow2(size) - 1); }
};
/*template <class Type> class DP {
public:
vector<Type> v;
Type initv;
vll size, block;
DP() {}
DP(const DP &dp) {}
template<class... Args> DP(Args... args) {
block.asn(1, 1);
Initialize(args...);
}
void Initialize(Type initv_) {
initv = initv_;
v.asn(block.bk, initv);
}
template<class... Args> void Initialize(ll val, Args... args) {
size.pb(val);
block.pb(block.bk*val);
Initialize(args...);
}
};*/
pl Bezout(ll a, ll b) {
if (b != 0) {
pl xy;
xy = Bezout(b, a % b);
return mp(xy.se, xy.fi - ((a / b) * xy.se));
} else {
return mp(1, 0);
}
}
pl Bez(ll a, ll b, ll c) {
pl xy;
ll x, y, z, gc;
xy = Bezout(a, b);
gc = gcd(a, b);
if (c % gc != 0)
return mp(-1, -1);
x = xy.fi * (c / gc);
y = xy.se * (c / gc);
if (x < 0)
z = rup(-x, (b / gc));
if (x >= 0)
z = -x / (b / gc);
x += z * (b / gc);
y -= z * (a / gc);
return mp(x, y);
}
ll DigS10(ll n) {
ll ans = 0;
while (1) {
ans += n % 10;
n /= 10;
if (!n)
break;
}
return ans;
}
ll isP(ll n) {
if (n <= 1)
return 0;
FOR(i, 2, (ll)sqrt(n) + 1) {
if (n % i == 0)
return 0;
}
return 1;
}
ll Tot(ll n) {
if (n <= 0)
return 0;
ll ans = n, x = 2;
while (x * x <= n) {
if (n % x == 0) {
ans -= ans / x;
while (n % x == 0)
n /= x;
}
x++;
}
if (n > 1)
ans -= ans / n;
return ans;
}
template <class T> struct Graph {
public:
ll vSize;
ll eMode;
ll mapMode;
GroupSet(T) Add;
CompareSet(T) Less;
CompareSet(T) Equal;
VV<P<T, ll>> adj;
map<pl, T> len;
Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD,
CompareSet(T) Less = LES, CompareSet(T) Equal = EQ)
: vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less),
Equal(Equal) {}
void Init() { adj.asn(vSize, V<P<T, ll>>()); }
void AddE(ll x, ll y, T cost) {
iAddE(x, y, cost);
if (!eMode)
iAddE(y, x, cost);
}
void iAddE(ll x, ll y, T cost) {
adj[x].pb(mp(cost, y));
if (mapMode)
len[mp(x, y)] = cost;
}
P<bool, T> getE(ll x, ll y) {
if (!len.count(mp(x, y)))
return mp(false, Id(Less));
return mp(true, len[mp(x, y)]);
}
V<T> Dijk(ll x) {
V<T> ans(vSize, Id(Less));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans[now.se] = segt[now.se].fi;
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se));
continue;
}
if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) {
segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se));
continue;
}
}
}
return ans;
}
V<P<T, vll>> rDijk(ll x) {
V<P<T, vll>> ans(vSize, mp(Id(Less), vll()));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans[now.se].fi = segt[now.se].fi;
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se));
ans[i.se].se = {now.se};
continue;
}
if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) {
segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se));
ans[i.se].se = {now.se};
continue;
}
if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) {
ans[i.se].se.pb(now.se);
continue;
}
}
}
return ans;
}
T Prim(ll x = 0) {
T ans = Id(Add);
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans = Act(Add)(ans, segt[now.se].fi);
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, i);
continue;
}
if (Act(Less)(i.fi, segt[i.se].fi)) {
segt.set(i.se, i);
continue;
}
}
}
return ans;
}
P<T, V<P<T, vll>>> rPrim(ll x = 0) {
P<T, V<P<T, vll>>> ans =
mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll())));
if (x < 0 || x >= vSize)
return ans;
SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1),
clamr(if (l.se < 0) return r; if (r.se < 0) return l;
return Act(Less)(l.fi, r.fi) ? l : r;));
segt.set(x, mp(Id(Add), x));
P<T, ll> now;
while ((now = segt.que(0, vSize - 1)).se >= 0) {
ans.se[now.se].fi = segt[now.se].fi;
ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi);
segt.set(now.se, mp(Id(Less), -2));
ROR(adj[now.se], i) {
if (segt[i.se].se == -2)
continue;
if (segt[i.se].se == -1) {
segt.set(i.se, i);
ans.se[i.se].se = {now.se};
continue;
}
if (Act(Less)(i.fi, segt[i.se].fi)) {
segt.set(i.se, i);
ans.se[i.se].se = {now.se};
continue;
}
if (Act(Equal)(i.fi, segt[i.se].fi)) {
ans.se[i.se].se.pb(now.se);
continue;
}
}
}
return ans;
}
};
template <class T> struct Sum {
public:
V<T> v, s;
ll size;
GroupSet(T) Add;
Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) {
Init();
Calc();
}
void Init() { s.asn(size + 1, Id(Add)); }
void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); }
T operator()(ll x) {
if (x < -1)
x = -1;
if (x > size - 1)
x = size - 1;
return s[x + 1];
}
T operator()(ll l, ll r) {
if (l < 0)
l = 0;
if (r >= size)
r = size - 1;
if (l > r)
return Id(Add);
return InvAct(Add)(s[r + 1], s[l]);
}
};
using sumll = Sum<ll>;
template <class T> struct Sum2 {
public:
VV<T> v, s;
ll RowSize, ColumnSize;
GroupSet(T) Add;
Sum2(VV<T> v, GroupSet(T) Add = ADD)
: v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) {
Init();
Calc();
}
void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); }
void Calc() {
REP1(r, RowSize) {
REP1(c, ColumnSize) {
// s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r -
//1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)),
//operator()(r - 2, c - 2));
s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]);
}
}
REP1(r, RowSize) {
REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); }
}
}
T operator()(ll r, ll c) {
if (r < -1)
r = -1;
if (c < -1)
c = -1;
if (r > RowSize - 1)
r = RowSize - 1;
if (c > ColumnSize - 1)
c = ColumnSize - 1;
return s[r + 1][c + 1];
}
T operator()(ll r1, ll c1, ll r2, ll c2) {
if (r1 < 0)
r1 = 0;
if (c1 < 0)
c1 = 0;
if (r2 >= RowSize)
r2 = RowSize - 1;
if (c2 >= ColumnSize)
c2 = ColumnSize - 1;
if (r1 > r2)
return Id(Add);
if (c1 > c2)
return Id(Add);
return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]),
Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1]));
}
};
using sumll2 = Sum2<ll>;
template <class T> struct Point2 {
public:
VV<T> v;
ll h, w;
Point2() : h(0), w(0) {}
Point2(ll h, ll w) : h(h), w(w) { asn(h, w); }
Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); }
Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) {
asn(h, w);
copy(cv);
}
void assign(ll h, ll w) { v.asn(h, V<T>(w)); }
void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); }
void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; }
T &operator()(ll h, ll w) { return v[h][w]; }
T &operator()(pl p) { return v[p.fi][p.se]; }
T &operator[](pl p) { return v[p.fi][p.se]; }
};
template <class T> using P2 = Point2<T>;
template <ll Mod> struct Modll {
public:
ll v;
Modll() : v(0) {}
Modll(ll _v) { set(_v % Mod + Mod); }
Modll &set(ll _v) {
v = (_v < Mod) ? _v : (_v - Mod);
return *this;
}
Modll pow(ll n) const {
Modll x = *this, ans = 1;
while (n) {
if (n & 1)
ans *= x;
x *= x;
n >>= 1;
}
return ans;
}
Modll inverse() const { return (*this).pow(Mod - 2); }
Modll operator+(const Modll &m) const { return Modll().set(v + m.v); }
Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); }
Modll operator*(const Modll &m) const {
return Modll().set((ull(v) * m.v) % Mod);
}
Modll operator/(const Modll &m) const { return *this * m.inv; }
Modll &operator+=(const Modll &m) { return *this = *this + m; }
Modll &operator-=(const Modll &m) { return *this = *this - m; }
Modll &operator*=(const Modll &m) { return *this = *this * m; }
Modll &operator/=(const Modll &m) { return *this = *this / m; }
Modll operator-() const { return Modll(0) - *this; }
explicit operator bool() const { return v != 0; }
friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; }
friend ostream &operator<<(ostream &out, const Modll &m) {
return out << m.v;
}
};
using mll = Modll<MOD>;
using vmll = V<mll>;
using vvmll = V<vmll>;
using vvvmll = V<vvmll>;
vmll MFactMemo(2, 1);
vmll MIFactMemo(2, 1);
mll mFact(ll n) {
if (MFactMemo.sz <= n) {
ll oldsize = MFactMemo.sz;
MFactMemo.res(n + 1, 1);
FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i;
}
return MFactMemo[n];
}
mll miFact(ll n) {
if (MIFactMemo.sz <= n) {
ll oldsize = MIFactMemo.sz;
MIFactMemo.res(n + 1, 1);
MIFactMemo.bk = mFact(n).inv;
rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i;
}
return MIFactMemo[n];
}
mll mComb(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return mFact(n) * miFact(k) * miFact(n - k);
}
ll LIS(vll v, ll m = 0) {
if (v.sz > 0) {
ll ans = 0;
vll dp(v.sz, INF);
FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; }
FOR(i, 0, v.sz - 1) {
if (dp[i] == INF)
break;
ans++;
}
return ans;
} else {
return 0;
}
}
ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) {
ll ng = mi - 1, ok = ma, mid;
while (ok - ng > 1) {
mid = (ng + ok) / 2;
(f(mid) ? ok : ng) = mid;
}
return ok;
}
template <class T, class M = decltype(MUL), class S = decltype(ADD)>
VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) {
VV<T> ans;
ll ii = A.sz;
if (!ii)
return ans;
ll jj = A[0].sz;
if (!jj)
return ans;
ll jj2 = B.sz;
if (!jj2)
return ans;
if (jj != jj2)
return ans;
ll kk = B[0].sz;
if (!kk)
return ans;
ans.asn(ii, V<T>(kk, 0));
REP(i, ii) {
REP(k, kk) {
REP(j, jj) {
ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k]));
}
}
}
return ans;
}
vvll CombMemo(1000, vll(1000, -1));
ll Comb(ll n, ll k) {
if ((n < 0) || (k < 0))
return 0;
if (CombMemo[n][k] == -1) {
if (n < k)
CombMemo[n][k] = 0;
else {
if (n == 0)
CombMemo[n][k] = 1;
else if (k == 0)
CombMemo[n][k] = 1;
else if (n == k)
CombMemo[n][k] = 1;
else
CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k);
}
}
return CombMemo[n][k];
}
template <class T> map<T, ll> Dict(V<T> v) {
map<T, ll> m;
if (!v.sz)
return m;
SORT(v);
UNIQUE(v);
REP(i, v.sz) { m[v[i]] = i; }
return m;
}
template <class T> vll Cmprs(V<T> v) {
auto m = Dict(v);
vll ans(v.sz);
REP(i, v.sz) { ans[i] = m[v[i]]; }
return ans;
}
template <class T> vll PCmprs(V<T> v) {
if (v.sz == 0)
return V<T>();
vll tmp(v.sz);
vll ans(v.sz);
IOTA(tmp, 0);
IOTA(ans, 0);
sort(tmp.bgn, tmp.en, clam(v[l] < v[r]));
sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r]));
return ans;
}
ll BblCnt(vll rv) {
vll v = PCmprs(rv);
SegT<ll> b(v.sz, 0);
ll ans = 0;
REP(i, v.sz) {
ans += (i - b.que(0, v[i]));
b.add(v[i], 1);
}
return ans;
}
pl NGrid(pl p, ll i, ll H, ll W) {
p = mp(p.fi + DX[i], p.se + DY[i]);
if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W)
return mp(INF, INF);
return p;
}
vvll llGrid(vs v) {
vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0));
REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#');
return r;
}
template <class T> auto ven(T val) { return val; }
template <> auto ven<int>(int val) { return (ll)val; }
template <class T, class... Args> auto ven(T val, Args... args) {
auto tmp = ven(args...);
return V<decltype(tmp)>(val, tmp);
}
template <class T> void zind(T &v) { v--; }
template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); }
template <class T, class... Args> void zind(T &v, Args &...args) {
zind(v);
zind(args...);
}
void Solve();
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(20) << fixed;
Solve();
}
void Solve() {
li(N, K);
vlc(N, t, d);
vpl sushi(N);
REP(i, N) sushi[i] = mp(d[i], t[i] - 1);
sort(sushi.bgn, sushi.en, lam(l.fi > r.fi));
vll used(N, 0);
ll nowp = 0;
vll yobi;
REP(i, K) {
if (used[sushi[i].se])
yobi.pb(sushi[i].fi);
used[sushi[i].se] = 1;
nowp += sushi[i].fi;
}
ll uc = 0;
REP(i, N) uc += used[i];
nowp += uc * uc;
ll ma = nowp;
ll x = 0;
while (x < yobi.sz && K < N) {
if (!used[sushi[K].se]) {
used[sushi[K].se] = 1;
nowp -= uc * uc;
uc++;
nowp += uc * uc;
nowp -= yobi[yobi.sz - 1 - x];
nowp += sushi[K].fi;
emax(ma, nowp);
x++;
}
K++;
}
sal(ma);
}
|
replace
| 1,412 | 1,413 | 1,412 | 1,413 |
0
| |
p03148
|
C++
|
Runtime Error
|
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<pair<int, int>> in(n);
for (int i = 0; i < n; i++) {
cin >> in[i].first >> in[i].second;
in[i].first--;
}
sort(in.begin(), in.end());
reverse(in.begin(), in.end());
vector<int> kind(n), to_max(n);
for (int i = 0; i < n; i++) {
int x = in[i].first;
if (kind[x] == 0) {
kind[x]++;
to_max[i]++;
}
}
vector<int> mx, els;
for (int i = 0; i < n; i++) {
if (to_max[i])
mx.push_back(in[i].second);
else
els.push_back(in[i].second);
}
sort(mx.rbegin(), mx.rend());
sort(els.rbegin(), els.rend());
vector<long long> a((int)mx.size() + 1), b((int)els.size() + 1);
for (int i = 0; i < (int)mx.size(); i++) {
a[i + 1] = a[i] + mx[i];
}
for (int i = 0; i < (int)els.size(); i++) {
b[i + 1] = b[i] + els[i];
}
long long ans = 0;
int sa = (int)a.size() - 1;
for (int i = 1; i <= sa; i++) {
long long tmp = 1LL * i * i;
if (i > k)
break;
tmp += a[i] + b[k - i];
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
|
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<pair<int, int>> in(n);
for (int i = 0; i < n; i++) {
cin >> in[i].first >> in[i].second;
in[i].first--;
}
sort(in.begin(), in.end());
reverse(in.begin(), in.end());
vector<int> kind(n), to_max(n);
for (int i = 0; i < n; i++) {
int x = in[i].first;
if (kind[x] == 0) {
kind[x]++;
to_max[i]++;
}
}
vector<int> mx, els;
for (int i = 0; i < n; i++) {
if (to_max[i])
mx.push_back(in[i].second);
else
els.push_back(in[i].second);
}
sort(mx.rbegin(), mx.rend());
sort(els.rbegin(), els.rend());
vector<long long> a((int)mx.size() + 1), b((int)els.size() + 1);
for (int i = 0; i < (int)mx.size(); i++) {
a[i + 1] = a[i] + mx[i];
}
for (int i = 0; i < (int)els.size(); i++) {
b[i + 1] = b[i] + els[i];
}
long long ans = 0;
int sa = (int)a.size() - 1;
for (int i = 1; i <= sa; i++) {
long long tmp = 1LL * i * i;
if (i > k)
break;
tmp += a[i] + b[min((int)b.size() - 1, k - i)];
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
ll solve(int N, int K, vi &t, vi &d) {
map<int, vi> sushi_list;
for (int i = 0; i < N; i++) {
sushi_list[t[i]].push_back(d[i]);
}
vi winner, looser;
for (auto p : sushi_list) {
vi q = p.second;
sort(q.begin(), q.end());
int w = q.back();
winner.push_back(w);
q.pop_back();
looser.insert(looser.begin(), q.begin(), q.end());
}
sort(winner.begin(), winner.end(), greater<int>());
sort(looser.begin(), looser.end(), greater<int>());
int W = winner.size();
int L = looser.size();
vl winner_cumsum(W + 1);
vl looser_cumsum(L + 1);
winner_cumsum[0] = 0;
looser_cumsum[0] = 0;
for (int i = 0; i < W; ++i) {
winner_cumsum[i + 1] = winner_cumsum[i] + winner[i];
}
for (int i = 0; i < L; ++i) {
looser_cumsum[i + 1] = looser_cumsum[i] + looser[i];
}
ll best = 0;
for (int n_unique = 1; n_unique <= W; ++n_unique) {
if (n_unique <= K && (K - n_unique <= L)) {
ll tmp = winner_cumsum[n_unique] + looser[K - n_unique] +
(ll)n_unique * n_unique;
best = max(best, tmp);
}
}
return best;
}
int main() {
int N, K;
cin >> N >> K;
vi t, d;
for (int i = 0; i < N; i++) {
int t_, d_;
cin >> t_ >> d_;
t.push_back(t_);
d.push_back(d_);
}
ll ans = solve(N, K, t, d);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
ll solve(int N, int K, vi &t, vi &d) {
map<int, vi> sushi_list;
for (int i = 0; i < N; i++) {
sushi_list[t[i]].push_back(d[i]);
}
vi winner, looser;
for (auto p : sushi_list) {
vi q = p.second;
sort(q.begin(), q.end());
int w = q.back();
winner.push_back(w);
q.pop_back();
looser.insert(looser.begin(), q.begin(), q.end());
}
sort(winner.begin(), winner.end(), greater<int>());
sort(looser.begin(), looser.end(), greater<int>());
int W = winner.size();
int L = looser.size();
vl winner_cumsum(W + 1);
vl looser_cumsum(L + 1);
winner_cumsum[0] = 0;
looser_cumsum[0] = 0;
for (int i = 0; i < W; ++i) {
winner_cumsum[i + 1] = winner_cumsum[i] + winner[i];
}
for (int i = 0; i < L; ++i) {
looser_cumsum[i + 1] = looser_cumsum[i] + looser[i];
}
ll best = 0;
for (int n_unique = 1; n_unique <= W; ++n_unique) {
if (n_unique <= K && (K - n_unique <= L)) {
ll tmp = winner_cumsum[n_unique] + looser_cumsum[K - n_unique] +
(ll)n_unique * n_unique;
best = max(best, tmp);
}
}
return best;
}
int main() {
int N, K;
cin >> N >> K;
vi t, d;
for (int i = 0; i < N; i++) {
int t_, d_;
cin >> t_ >> d_;
t.push_back(t_);
d.push_back(d_);
}
ll ans = solve(N, K, t, d);
cout << ans << endl;
return 0;
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
int main() {
int N, K;
cin >> N >> K;
map<int, vll> sushi_list;
for (int i = 0; i < N; ++i) {
int t;
cin >> t;
ll d;
cin >> d;
sushi_list[t].push_back(d);
}
vll winner, loser;
for (auto p : sushi_list) {
vll q = p.second;
sort(q.begin(), q.end(), greater<ll>());
int M = q.size();
winner.push_back(q[0]);
if (M > 1) {
for (int i = 1; i < M; ++i)
loser.push_back(q[i]);
}
}
sort(winner.begin(), winner.end(), greater<ll>());
sort(loser.begin(), loser.end(), greater<ll>());
int W = winner.size(), L = loser.size();
vll w_cum(W + 1), l_cum(L + 1);
w_cum[0] = 0, l_cum[0] = 0;
for (int i = 0; i < W; ++i) {
w_cum[i + 1] = w_cum[i] + winner[i];
}
for (int i = 0; i < L; ++i) {
l_cum[i + 1] = l_cum[i] + loser[i];
}
ll ans = 0;
int K_ = min(K, W);
for (int x = 1; x <= K_; ++x) {
ll tmp = x * x + w_cum[x] + l_cum[K - x];
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
int main() {
int N, K;
cin >> N >> K;
map<int, vll> sushi_list;
for (int i = 0; i < N; ++i) {
int t;
cin >> t;
ll d;
cin >> d;
sushi_list[t].push_back(d);
}
vll winner, loser;
for (auto p : sushi_list) {
vll q = p.second;
sort(q.begin(), q.end(), greater<ll>());
int M = q.size();
winner.push_back(q[0]);
if (M > 1) {
for (int i = 1; i < M; ++i)
loser.push_back(q[i]);
}
}
sort(winner.begin(), winner.end(), greater<ll>());
sort(loser.begin(), loser.end(), greater<ll>());
int W = winner.size(), L = loser.size();
vll w_cum(W + 1), l_cum(L + 1);
w_cum[0] = 0, l_cum[0] = 0;
for (int i = 0; i < W; ++i) {
w_cum[i + 1] = w_cum[i] + winner[i];
}
for (int i = 0; i < L; ++i) {
l_cum[i + 1] = l_cum[i] + loser[i];
}
ll ans = 0;
int K_ = min(K, W);
for (int x = 1; x <= K_; ++x) {
if (K - x <= L) {
ll tmp = (ll)x * x + w_cum[x] + l_cum[K - x];
ans = max(ans, tmp);
}
}
cout << ans << endl;
return 0;
}
|
replace
| 50 | 52 | 50 | 54 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
int main() {
ll n, k;
cin >> n >> k;
vector<P> p(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
p[i] = make_pair(d, t);
}
sort(rall(p));
map<ll, ll> mp;
priority_queue<P, vector<P>, greater<P>> prQ;
ll ans = 0;
rep(i, k) {
ll d = p[i].first;
ll t = p[i].second;
ans += d;
mp[t]++;
if (mp[t] >= 2) {
prQ.push(make_pair(d, t));
}
}
ll x = mp.size();
ans += x * x;
ll now = ans;
exrep(i, k, n - 1) {
ll d = p[i].first;
ll t = p[i].second;
if (mp[t] > 0) {
continue;
}
auto z = prQ.top();
prQ.pop();
now += d - z.first;
now += 2 * x + 1;
mp[t]++;
x++;
chmax(ans, now);
}
out(ans);
re0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
int main() {
ll n, k;
cin >> n >> k;
vector<P> p(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
p[i] = make_pair(d, t);
}
sort(rall(p));
map<ll, ll> mp;
priority_queue<P, vector<P>, greater<P>> prQ;
ll ans = 0;
rep(i, k) {
ll d = p[i].first;
ll t = p[i].second;
ans += d;
mp[t]++;
if (mp[t] >= 2) {
prQ.push(make_pair(d, t));
}
}
ll x = mp.size();
ans += x * x;
ll now = ans;
exrep(i, k, n - 1) {
if (prQ.empty()) {
break;
}
ll d = p[i].first;
ll t = p[i].second;
if (mp[t] > 0) {
continue;
}
auto z = prQ.top();
prQ.pop();
now += d - z.first;
now += 2 * x + 1;
mp[t]++;
x++;
chmax(ans, now);
}
out(ans);
re0;
}
|
insert
| 60 | 60 | 60 | 63 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;
const ld pi = acos(-1);
const ll inf = (ll)1e12 + 1;
// const int mod=1000000007;
const int mod = 998244353;
int n, m, k;
const int N = 3e5 + 10;
struct Set_with_sum {
struct node {
int no;
ll sum;
node *l, *r;
node() : no(0), sum(0ll), l(NULL), r(NULL) {}
};
node *root;
int lo = 0, hi;
Set_with_sum(int hii) : root(new node), hi(hii){};
void insert(node *p, int l, int r, int id) {
if (l == r) {
p->sum += id;
p->no++;
return;
}
int mid = (l + r) >> 1;
if (p->l == NULL)
p->l = new node;
if (p->r == NULL)
p->r = new node;
if (id > mid) {
insert(p->r, mid + 1, r, id);
} else {
insert(p->l, l, mid, id);
}
p->sum = p->l->sum + p->r->sum;
p->no = p->l->no + p->r->no;
}
void erase(node *p, int l, int r, int id) {
if (l == r) {
p->sum -= id;
p->no--;
return;
}
int mid = (l + r) >> 1;
if (id > mid) {
erase(p->r, mid + 1, r, id);
} else {
erase(p->l, l, mid, id);
}
p->sum = p->l->sum + p->r->sum;
p->no = p->l->no + p->r->no;
}
ll query(node *p, int l, int r, int k) {
if (!k)
return 0;
if (l == r)
return (ll)k * l;
if (p->l == NULL)
p->l = new node;
if (p->r == NULL)
p->r = new node;
int m = (l + r) >> 1;
if (p->r->no > k) {
return query(p->r, m + 1, r, k);
} else {
return p->r->sum + query(p->l, l, m, k - p->r->no);
}
}
ll query(int k) { return query(root, 0, hi, k); }
void insert(int no) { insert(root, 0, hi, no); }
void erase(int no) { erase(root, 0, hi, no); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> k >> n;
Set_with_sum s(1e9 + 1);
priority_queue<pii, vpi> pq;
for (int i = 0, u, v; i < n; i++) {
cin >> u >> v;
s.insert(v);
pq.emplace(v, u);
}
vb mark(n + 1);
ll diff = 0;
ll a = 0, already = 0;
while (!pq.empty()) {
auto f = pq.top();
pq.pop();
if (!mark[f.se]) {
mark[f.se] = true;
already += f.fi;
diff++;
s.erase(f.fi);
a = max(a, already + s.query(k - (int)diff) + diff * diff);
if (diff == k)
break;
}
}
cout << a << el;
return 0;
}
|
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;
const ld pi = acos(-1);
const ll inf = (ll)1e12 + 1;
// const int mod=1000000007;
const int mod = 998244353;
int n, m, k;
const int N = 3e5 + 10;
struct Set_with_sum {
struct node {
int no;
ll sum;
node *l, *r;
node() : no(0), sum(0ll), l(NULL), r(NULL) {}
};
node *root;
int lo = 0, hi;
Set_with_sum(int hii) : root(new node), hi(hii){};
void insert(node *p, int l, int r, int id) {
if (l == r) {
p->sum += id;
p->no++;
return;
}
int mid = (l + r) >> 1;
if (p->l == NULL)
p->l = new node;
if (p->r == NULL)
p->r = new node;
if (id > mid) {
insert(p->r, mid + 1, r, id);
} else {
insert(p->l, l, mid, id);
}
p->sum = p->l->sum + p->r->sum;
p->no = p->l->no + p->r->no;
}
void erase(node *p, int l, int r, int id) {
if (l == r) {
p->sum -= id;
p->no--;
return;
}
int mid = (l + r) >> 1;
if (id > mid) {
erase(p->r, mid + 1, r, id);
} else {
erase(p->l, l, mid, id);
}
p->sum = p->l->sum + p->r->sum;
p->no = p->l->no + p->r->no;
}
ll query(node *p, int l, int r, int k) {
if (!k)
return 0;
if (l == r)
return (ll)k * l;
if (p->l == NULL)
p->l = new node;
if (p->r == NULL)
p->r = new node;
int m = (l + r) >> 1;
if (p->r->no > k) {
return query(p->r, m + 1, r, k);
} else {
return p->r->sum + query(p->l, l, m, k - p->r->no);
}
}
ll query(int k) { return query(root, 0, hi, k); }
void insert(int no) { insert(root, 0, hi, no); }
void erase(int no) { erase(root, 0, hi, no); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
Set_with_sum s(1e9 + 1);
priority_queue<pii, vpi> pq;
for (int i = 0, u, v; i < n; i++) {
cin >> u >> v;
s.insert(v);
pq.emplace(v, u);
}
vb mark(n + 1);
ll diff = 0;
ll a = 0, already = 0;
while (!pq.empty()) {
auto f = pq.top();
pq.pop();
if (!mark[f.se]) {
mark[f.se] = true;
already += f.fi;
diff++;
s.erase(f.fi);
a = max(a, already + s.query(k - (int)diff) + diff * diff);
if (diff == k)
break;
}
}
cout << a << el;
return 0;
}
|
replace
| 129 | 130 | 129 | 130 |
0
| |
p03148
|
C++
|
Runtime Error
|
/*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 62;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
return f.second > s.second;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll nCr(ll n, ll r) {
if (r == 0 || r == n) {
return 1;
} else if (r == 1) {
return n;
}
return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r) {
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--)
ret *= i;
return ret;
}
//-----------------------ここから-----------
int main(void) {
ll n, k;
cin >> n >> k;
vector<pair<ll, ll>> vec(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
vec[i] = {d, t};
}
sort(all(vec), greater<pair<ll, ll>>());
ll now = 0;
map<ll, ll> mp;
set<ll> uni;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
other;
for (int i = 0; i < k; i++) {
if (uni.find(vec[i].second) != uni.end()) {
other.push(vec[i]);
mp[vec[i].second]++;
} else {
uni.insert(vec[i].second);
mp[vec[i].second]++;
}
now += vec[i].first;
}
ll ans = now + (ll)uni.size() * (ll)uni.size();
for (int i = k; i < n; i++) {
if (uni.find(vec[i].second) == uni.end()) {
uni.insert(vec[i].second);
now += vec[i].first;
mp[vec[i].second]++;
pair<ll, ll> rem = other.top();
now -= rem.first;
mp[rem.second]--;
other.pop();
ans = max(now + (ll)uni.size() * (ll)uni.size(), ans);
}
}
cout << ans << endl;
}
|
/*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 62;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
return f.second > s.second;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll nCr(ll n, ll r) {
if (r == 0 || r == n) {
return 1;
} else if (r == 1) {
return n;
}
return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r) {
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--)
ret *= i;
return ret;
}
//-----------------------ここから-----------
int main(void) {
ll n, k;
cin >> n >> k;
vector<pair<ll, ll>> vec(n);
rep(i, n) {
ll t, d;
cin >> t >> d;
vec[i] = {d, t};
}
sort(all(vec), greater<pair<ll, ll>>());
ll now = 0;
map<ll, ll> mp;
set<ll> uni;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
other;
for (int i = 0; i < k; i++) {
if (uni.find(vec[i].second) != uni.end()) {
other.push(vec[i]);
mp[vec[i].second]++;
} else {
uni.insert(vec[i].second);
mp[vec[i].second]++;
}
now += vec[i].first;
}
ll ans = now + (ll)uni.size() * (ll)uni.size();
for (int i = k; i < n; i++) {
if (uni.find(vec[i].second) == uni.end()) {
if (other.empty())
continue;
uni.insert(vec[i].second);
now += vec[i].first;
mp[vec[i].second]++;
pair<ll, ll> rem = other.top();
now -= rem.first;
mp[rem.second]--;
other.pop();
ans = max(now + (ll)uni.size() * (ll)uni.size(), ans);
}
}
cout << ans << endl;
}
|
insert
| 101 | 101 | 101 | 103 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n) \
rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int k, n;
cin >> n >> k;
priority_queue<P> q1;
priority_queue<P, vector<P>, greater<P>> q2;
rep(i, n) {
int t, d;
cin >> t >> d;
q1.push(P(d, t));
}
int ans = 0, sum = 0;
vi v(n, 0);
int x = 0;
rep(i, k) {
P p = q1.top();
q1.pop();
sum += p.F;
if (v[p.S] == 0)
x++;
v[p.S]++;
if (v[p.S] > 1)
q2.push(p);
}
ans = sum + x * x;
rep(i, n - k) {
P p = q1.top();
q1.pop();
if (v[p.S] || q2.empty())
continue;
P p2 = q2.top();
q2.pop();
sum -= p2.F;
sum += p.F;
v[p.S]++;
x++;
ans = max(ans, sum + x * x);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n) \
rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int k, n;
cin >> n >> k;
priority_queue<P> q1;
priority_queue<P, vector<P>, greater<P>> q2;
rep(i, n) {
int t, d;
cin >> t >> d;
q1.push(P(d, t));
}
int ans = 0, sum = 0;
vi v(n + 1, 0);
int x = 0;
rep(i, k) {
P p = q1.top();
q1.pop();
sum += p.F;
if (v[p.S] == 0)
x++;
v[p.S]++;
if (v[p.S] > 1)
q2.push(p);
}
ans = sum + x * x;
rep(i, n - k) {
P p = q1.top();
q1.pop();
if (v[p.S] || q2.empty())
continue;
P p2 = q2.top();
q2.pop();
sum -= p2.F;
sum += p.F;
v[p.S]++;
x++;
ans = max(ans, sum + x * x);
}
cout << ans << endl;
}
|
replace
| 43 | 44 | 43 | 44 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, k, ans = 0;
cin >> n >> k;
vector<pair<ll, ll>> x(n);
vector<bool> b(n, false);
for (int i = 0; i < n; i++) {
ll t, d;
cin >> t >> d;
x[i].first = d;
x[i].second = t;
}
sort(x.begin(), x.end());
reverse(x.begin(), x.end());
map<ll, ll> mp;
for (int i = 0; i < k; i++) {
ans += x[i].first;
b[i] = true;
mp[x[i].second]++;
}
ans += mp.size() * mp.size();
int j = k;
ll sub = 0, cnt = mp.size(), sans = ans;
for (int i = k - 1; i * (n - 1 - i) >= 0 && j < n && i >= 0;) {
while (i >= 0 && (!b[i] || mp[x[i].second] <= 1)) {
i--;
}
while (j < n && (b[j] || mp[x[j].second] >= 1)) {
j++;
}
if (i * (n - 1 - i) >= 0 && j < n)
sub = x[j].first - x[i].first + 2 * cnt + 1;
else
break;
sans += sub;
if (sans > ans)
ans = sans;
cnt++;
mp[x[i].second]--;
b[i] = false;
mp[x[j].second]++;
b[j] = true;
i = j;
j++;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, k, ans = 0;
cin >> n >> k;
vector<pair<ll, ll>> x(n);
vector<bool> b(n, false);
for (int i = 0; i < n; i++) {
ll t, d;
cin >> t >> d;
x[i].first = d;
x[i].second = t;
}
sort(x.begin(), x.end());
reverse(x.begin(), x.end());
map<ll, ll> mp;
for (int i = 0; i < k; i++) {
ans += x[i].first;
b[i] = true;
mp[x[i].second]++;
}
ans += mp.size() * mp.size();
int j = k;
ll sub = 0, cnt = mp.size(), sans = ans;
for (int i = k - 1; i * (n - 1 - i) >= 0 && j < n && i >= 0;) {
while (i >= 0 && (!b[i] || mp[x[i].second] <= 1)) {
i--;
}
while (j < n && (b[j] || mp[x[j].second] >= 1)) {
j++;
}
if (i * (n - 1 - i) >= 0 && j < n)
sub = x[j].first - x[i].first + 2 * cnt + 1;
else
break;
sans += sub;
if (sans > ans)
ans = sans;
cnt++;
mp[x[i].second]--;
b[i] = false;
mp[x[j].second]++;
b[j] = true;
j++;
}
cout << ans << endl;
return 0;
}
|
delete
| 45 | 46 | 45 | 45 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int INF = 2147483647;
const ll INFL = 9223372036854775807;
const ll mod = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
VVLL v(n, VLL(2));
REP(i, n) cin >> v[i][1] >> v[i][0];
REP(i, n) v[i][1]--;
sort(ALL(v));
reverse(ALL(v));
VLL tmp;
VLL a(n, 0);
ll ans = 0;
ll res = 0;
ll cnt = 0;
REP(i, k) {
if (a[v[i][1]] == 0) {
a[v[i][1]] = v[i][0];
cnt++;
res += v[i][0];
} else {
res += v[i][0];
tmp.push_back(min(a[v[i][1]], v[i][0]));
a[v[i][1]] = max(a[v[i][1]], v[i][0]);
}
}
ans = res + cnt * cnt;
reverse(ALL(tmp));
ll l = tmp.size();
ll b = 0;
FOR(i, k, n) {
if (a[v[i][1]])
continue;
res -= tmp[b];
res += v[i][0];
a[v[i][1]] = 1;
cnt++;
b++;
ans = max(ans, res + cnt * cnt);
if (b == l)
break;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int INF = 2147483647;
const ll INFL = 9223372036854775807;
const ll mod = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
VVLL v(n, VLL(2));
REP(i, n) cin >> v[i][1] >> v[i][0];
REP(i, n) v[i][1]--;
sort(ALL(v));
reverse(ALL(v));
VLL tmp;
VLL a(n, 0);
ll ans = 0;
ll res = 0;
ll cnt = 0;
REP(i, k) {
if (a[v[i][1]] == 0) {
a[v[i][1]] = v[i][0];
cnt++;
res += v[i][0];
} else {
res += v[i][0];
tmp.push_back(min(a[v[i][1]], v[i][0]));
a[v[i][1]] = max(a[v[i][1]], v[i][0]);
}
}
ans = res + cnt * cnt;
reverse(ALL(tmp));
ll l = tmp.size();
ll b = 0;
if (l == 0) {
cout << ans << endl;
return 0;
}
FOR(i, k, n) {
if (a[v[i][1]])
continue;
res -= tmp[b];
res += v[i][0];
a[v[i][1]] = 1;
cnt++;
b++;
ans = max(ans, res + cnt * cnt);
if (b == l)
break;
}
cout << ans << endl;
return 0;
}
|
insert
| 70 | 70 | 70 | 74 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);
int main() {
ll N, K;
cin >> N >> K;
vector<pair<ll, ll>> sushi(N);
vector<vector<ll>> sushi1(N), sushi3(N);
ll t;
ll d;
for (ll i = 0; i < N; ++i) {
cin >> t >> d;
t--;
sushi[i] = mp(d, t);
sushi1[t].push_back(d);
}
sort(sushi.begin(), sushi.end(), greater<pair<ll, ll>>());
vector<ll> sushi2;
ll seed = 0, score = 0;
map<ll, ll> amap;
for (ll i = 0; i < K; ++i) {
if (amap[sushi[i].se] == 0)
seed++;
amap[sushi[i].se]++;
score += sushi[i].fi;
sushi3[sushi[i].se].push_back(sushi[i].fi);
}
for (ll i = 0; i < N; ++i) {
sort(sushi3[i].begin(), sushi3[i].end());
for (ll j = 0; j < sushi3[i].size() - 1; ++j)
sushi2.push_back(sushi3[i][j]);
}
sort(sushi2.begin(), sushi2.end(), greater<ll>());
vector<ll> ans;
ans.push_back(score + seed * seed);
for (ll i = K; i < N; ++i) {
if (amap[sushi[i].se] > 0 || sushi2.size() == 0)
continue;
score = score - sushi2[sushi2.size() - 1] + sushi[i].fi;
seed++;
ans.push_back(score + seed * seed);
sushi2.pop_back();
amap[sushi[i].se]++;
}
sort(ans.begin(), ans.end(), greater<ll>());
cout << ans[0] << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);
int main() {
ll N, K;
cin >> N >> K;
vector<pair<ll, ll>> sushi(N);
vector<vector<ll>> sushi1(N), sushi3(N);
ll t;
ll d;
for (ll i = 0; i < N; ++i) {
cin >> t >> d;
t--;
sushi[i] = mp(d, t);
sushi1[t].push_back(d);
}
sort(sushi.begin(), sushi.end(), greater<pair<ll, ll>>());
vector<ll> sushi2;
ll seed = 0, score = 0;
map<ll, ll> amap;
for (ll i = 0; i < K; ++i) {
if (amap[sushi[i].se] == 0)
seed++;
amap[sushi[i].se]++;
score += sushi[i].fi;
sushi3[sushi[i].se].push_back(sushi[i].fi);
}
for (ll i = 0; i < N; ++i) {
sort(sushi3[i].begin(), sushi3[i].end());
if (sushi3[i].size() > 0)
for (ll j = 0; j < sushi3[i].size() - 1; ++j)
sushi2.push_back(sushi3[i][j]);
}
sort(sushi2.begin(), sushi2.end(), greater<ll>());
vector<ll> ans;
ans.push_back(score + seed * seed);
for (ll i = K; i < N; ++i) {
if (amap[sushi[i].se] > 0 || sushi2.size() == 0)
continue;
score = score - sushi2[sushi2.size() - 1] + sushi[i].fi;
seed++;
ans.push_back(score + seed * seed);
sushi2.pop_back();
amap[sushi[i].se]++;
}
sort(ans.begin(), ans.end(), greater<ll>());
cout << ans[0] << endl;
}
|
replace
| 51 | 53 | 51 | 54 |
-11
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<pair<int, int>> A(N);
vector<int> B(N);
int res = 0;
for (int i = 0; i < N; i++) {
cin >> A[i].second >> A[i].first;
}
sort(A.rbegin(), A.rend());
int num = 0;
int sum = 0;
vector<int> cnt(N + 1, 0);
vector<int> st;
for (int i = 0; i < K; i++) {
sum += A[i].first;
// cerr << i << " " << A[i].second << endl;
if (cnt[A[i].second] == 0) {
cnt[A[i].second]++;
num++;
} else {
cnt[A[i].second]++;
st.push_back(i);
}
}
// cerr << sum << " " << num << endl;
res = max(res, sum + num * num);
for (int i = K; i < N; i++) {
if (cnt[A[i].second] == 0) {
int j = st.back();
st.pop_back();
cnt[A[j].second]--;
sum -= A[j].first;
cnt[A[i].second]++;
sum += A[i].first;
num++;
res = max(res, sum + num * num);
// cerr << i << endl;
// cerr << sum << " " << num << endl;
}
}
cout << res << endl;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<pair<int, int>> A(N);
vector<int> B(N);
int res = 0;
for (int i = 0; i < N; i++) {
cin >> A[i].second >> A[i].first;
}
sort(A.rbegin(), A.rend());
int num = 0;
int sum = 0;
vector<int> cnt(N + 1, 0);
vector<int> st;
for (int i = 0; i < K; i++) {
sum += A[i].first;
// cerr << i << " " << A[i].second << endl;
if (cnt[A[i].second] == 0) {
cnt[A[i].second]++;
num++;
} else {
cnt[A[i].second]++;
st.push_back(i);
}
}
// cerr << sum << " " << num << endl;
res = max(res, sum + num * num);
for (int i = K; i < N; i++) {
if (cnt[A[i].second] == 0) {
if (st.size() == 0)
break;
int j = st.back();
st.pop_back();
cnt[A[j].second]--;
sum -= A[j].first;
cnt[A[i].second]++;
sum += A[i].first;
num++;
res = max(res, sum + num * num);
// cerr << i << endl;
// cerr << sum << " " << num << endl;
}
}
cout << res << endl;
}
|
insert
| 44 | 44 | 44 | 46 |
0
| |
p03148
|
C++
|
Time Limit Exceeded
|
// #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int N, K;
pii S[100010];
int tn[100010];
priority_queue<pii, vector<pii>, greater<pii>> pq;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t, d;
cin >> N >> K;
rep(i, 0, N) { cin >> S[i].second >> S[i].first; }
sort(S, S + N);
int res = 0;
set<int> tns;
rrep(i, N - K, N) {
res += S[i].first;
pq.push(S[i]);
tn[S[i].second]++;
tns.insert(S[i].second);
}
int tnss = tns.size();
res += tnss * tnss;
int ans = res;
rrep(i, 0, N - K) {
if (tn[S[i].second] == 0) {
while (true) {
auto p = pq.top();
pq.pop();
if (tn[p.second] > 1) {
tn[p.second]--;
tn[S[i].second]++;
tnss++;
res -= p.first - S[i].first;
res += tnss * tnss - (tnss - 1) * (tnss - 1);
chmax(ans, res);
break;
}
}
}
}
cout << ans << endl;
return 0;
}
|
// #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int N, K;
pii S[100010];
int tn[100010];
priority_queue<pii, vector<pii>, greater<pii>> pq;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t, d;
cin >> N >> K;
rep(i, 0, N) { cin >> S[i].second >> S[i].first; }
sort(S, S + N);
int res = 0;
set<int> tns;
rrep(i, N - K, N) {
res += S[i].first;
pq.push(S[i]);
tn[S[i].second]++;
tns.insert(S[i].second);
}
int tnss = tns.size();
res += tnss * tnss;
int ans = res;
rrep(i, 0, N - K) {
if (tn[S[i].second] == 0) {
while (!pq.empty()) {
auto p = pq.top();
pq.pop();
if (tn[p.second] > 1) {
tn[p.second]--;
tn[S[i].second]++;
tnss++;
res -= p.first - S[i].first;
res += tnss * tnss - (tnss - 1) * (tnss - 1);
chmax(ans, res);
break;
}
}
}
}
cout << ans << endl;
return 0;
}
|
replace
| 93 | 94 | 93 | 94 |
TLE
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define FOR(i, m, n) for (long long i = (m); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i)
o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T> void chmax(T &a, const T b) { a = max<T>(a, b); }
template <class T> void chmin(T &a, const T b) { a = min<T>(a, b); }
void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }
int main() {
ll N, K;
cin >> N >> K;
V<P<ll, ll>> dt;
REP(i, N) {
ll t, d;
cin >> t >> d;
dt.push_back({d, t});
}
sort(ALL(dt), greater<P<ll, ll>>());
ll ans = 0, sum = 0;
priority_queue<ll, V<ll>, greater<ll>> pq;
map<ll, ll> mp;
REP(i, K) {
if (mp[dt[i].second] == 0) {
mp[dt[i].second]++;
sum += dt[i].first;
} else {
pq.push(dt[i].first);
sum += dt[i].first;
}
}
chmax(ans, sum + (ll)mp.size() * (ll)mp.size());
FOR(i, K, N) {
if (mp[dt[i].second] == 0) {
mp[dt[i].second]++;
sum += dt[i].first;
sum -= pq.top();
pq.pop();
}
chmax(ans, sum + (ll)mp.size() * (ll)mp.size());
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define FOR(i, m, n) for (long long i = (m); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i)
o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T> void chmax(T &a, const T b) { a = max<T>(a, b); }
template <class T> void chmin(T &a, const T b) { a = min<T>(a, b); }
void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }
int main() {
ll N, K;
cin >> N >> K;
V<P<ll, ll>> dt;
REP(i, N) {
ll t, d;
cin >> t >> d;
dt.push_back({d, t});
}
sort(ALL(dt), greater<P<ll, ll>>());
ll ans = 0, sum = 0;
priority_queue<ll, V<ll>, greater<ll>> pq;
map<ll, ll> mp;
REP(i, K) {
if (mp[dt[i].second] == 0) {
mp[dt[i].second]++;
sum += dt[i].first;
} else {
pq.push(dt[i].first);
sum += dt[i].first;
}
}
chmax(ans, sum + (ll)mp.size() * (ll)mp.size());
FOR(i, K, N) {
if (pq.size() && mp[dt[i].second] == 0) {
mp[dt[i].second]++;
sum += dt[i].first;
sum -= pq.top();
pq.pop();
}
chmax(ans, sum + (ll)mp.size() * (ll)mp.size());
}
cout << ans << endl;
return 0;
}
|
replace
| 114 | 115 | 114 | 115 |
0
| |
p03148
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
const ll N = 1e9 + 7;
const int n_max = 1e5 + 10;
int main() {
ll n, k;
cin >> n >> k;
vector<Pll> dt(n);
set<ll> tst;
rep(i, n) {
ll t, d;
cin >> t >> d;
t--;
dt.at(i) = Pll(d, t);
tst.insert(t);
}
ll tmax = tst.size();
sort(dt.begin(), dt.end(), greater<Pll>());
// vector<vector<ll>> dp(n, vector<ll>(k,-1));
priority_queue<Pll, vector<Pll>, greater<Pll>> pq;
vector<ll> num(n, 0), p(n, 0);
ll count = 0, type = 0;
ll sum = 0;
rep(i, k) {
sum += dt.at(i).first;
pq.push(dt.at(i));
if (!num.at(dt.at(i).second))
type++;
num.at(dt.at(i).second)++;
count++;
}
p.at(type) = sum + type * type;
while (type < min(k, tmax)) {
Pll temp = pq.top();
pq.pop();
if (num.at(temp.second) == 1)
continue;
sum -= temp.first;
while (count < n) {
if (num.at(dt.at(count).second))
count++;
else {
sum += dt.at(count).first;
pq.push(dt.at(count));
num.at(dt.at(count).second)++;
count++;
break;
}
}
type++;
p.at(type) = sum + type * type;
}
cout << *max_element(p.begin(), p.end()) << endl;
return 0;
}
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
const ll N = 1e9 + 7;
const int n_max = 1e5 + 10;
int main() {
ll n, k;
cin >> n >> k;
vector<Pll> dt(n);
set<ll> tst;
rep(i, n) {
ll t, d;
cin >> t >> d;
t--;
dt.at(i) = Pll(d, t);
tst.insert(t);
}
ll tmax = tst.size();
sort(dt.begin(), dt.end(), greater<Pll>());
// vector<vector<ll>> dp(n, vector<ll>(k,-1));
priority_queue<Pll, vector<Pll>, greater<Pll>> pq;
vector<ll> num(n, 0), p(n + 10, 0);
ll count = 0, type = 0;
ll sum = 0;
rep(i, k) {
sum += dt.at(i).first;
pq.push(dt.at(i));
if (!num.at(dt.at(i).second))
type++;
num.at(dt.at(i).second)++;
count++;
}
p.at(type) = sum + type * type;
while (type < min(k, tmax)) {
Pll temp = pq.top();
pq.pop();
if (num.at(temp.second) == 1)
continue;
sum -= temp.first;
while (count < n) {
if (num.at(dt.at(count).second))
count++;
else {
sum += dt.at(count).first;
pq.push(dt.at(count));
num.at(dt.at(count).second)++;
count++;
break;
}
}
type++;
p.at(type) = sum + type * type;
}
cout << *max_element(p.begin(), p.end()) << endl;
return 0;
}
|
replace
| 37 | 38 | 37 | 38 |
0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.