#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int64_t INF = 1e18;
const int64_t mINF = 1e9;
const int64_t MOD = 998244353;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
struct Thing
{
int64_t val;
int i;
int j;
int t;
Thing(int64_t val_, int i_, int j_, int t_) : val(val_), i(i_), j(j_), t(t_) {}
bool operator < (const Thing& o) const
{
int64_t len1 = j - i;
int64_t len2 = o.j - o.i;
if(val * len2 != o.val * len1) return (val * len2) > (o.val * len1);
if(t != o.t) return t > o.t;
if(i != o.i) return i > o.i;
return j > o.j;
}
};
struct Solution
{
int64_t ans;
set<int> ai;
set<int> bi;
set<Thing> s;
vector<int64_t> a;
vector<int64_t> b;
Solution() {}
void solve()
{
int nr;
int nc;
cin >> nr >> nc;
a.resize(nr);
b.resize(nc);
for(int i = 0; i < nr; i++)
{
cin >> a[i];
}
for(int i = 0; i < nc; i++)
{
cin >> b[i];
}
//t = 0 = a // t = 1 = b
for(int i = 1; i < nr; i++)
{
s.insert(Thing(a[i] - a[i - 1], i - 1, i, 0));
}
for(int i = 1; i < nc; i++)
{
s.insert(Thing(b[i] - b[i - 1], i - 1, i, 1));
}
s.insert(Thing(-INF, -1, -2, - 1));
for(int i = 0; i < nr; i++)
{
ai.insert(i);
}
for(int i = 0; i < nc; i++)
{
bi.insert(i);
}
ans = 0;
while((int) s.size() != 1)
{
Thing tmp = *(s.begin());
s.erase(s.begin());
int i = tmp.i;
int j = tmp.j;
int t = tmp.t;
if(t == 1) RemoveCol(i, j);
else RemoveRow(i, j);
}
cout << ans << "\n";
}
void RemoveCol(int i, int j)
{
bi.erase(j);
set<int>::iterator it = bi.lower_bound(j);
if(it != bi.end())
{
int nx = (*it);
s.erase(Thing(b[nx] - b[j], j, nx, 1));
s.insert(Thing(b[nx] - b[i], i, nx, 1));
}
else
{
int len = j - i;
for(int c = 0; c < len; c++)
{
ans += a.back();
b.pop_back();
}
}
}
void RemoveRow(int i, int j)
{
ai.erase(j);
set<int>::iterator it = ai.lower_bound(j);
if(it != ai.end())
{
int nx = (*it);
s.erase(Thing(a[nx] - a[j], j, nx, 0));
s.insert(Thing(a[nx] - a[i], i, nx, 0));
}
else
{
int len = j - i;
for(int c = 0; c < len; c++)
{
ans += b.back();
a.pop_back();
}
}
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
484 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
0 ms |
316 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
173 ms |
10148 KB |
Output is correct |
5 |
Correct |
76 ms |
9512 KB |
Output is correct |
6 |
Correct |
52 ms |
4236 KB |
Output is correct |
7 |
Correct |
616 ms |
24572 KB |
Output is correct |
8 |
Incorrect |
598 ms |
24568 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
484 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
0 ms |
316 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
173 ms |
10148 KB |
Output is correct |
33 |
Correct |
76 ms |
9512 KB |
Output is correct |
34 |
Correct |
52 ms |
4236 KB |
Output is correct |
35 |
Correct |
616 ms |
24572 KB |
Output is correct |
36 |
Incorrect |
598 ms |
24568 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |