Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5;
int n, cnt_edge[maxn];
vector<int> g[maxn];
namespace dsu {
int pa[maxn], size[maxn], rk[maxn];
vector<int> root;
void Init(void) {
root.clear();
for (int i = 0; i < n; ++i) {
pa[i] = i;
size[i] = 1;
rk[i] = 0;
root.push_back(i);
}
}
int Find(int i) { return i == pa[i] ? i : pa[i] = Find(pa[i]); }
void Unite(int i, int j) {
i = Find(i);
j = Find(j);
if (i == j) return;
if (rk[i] > rk[j]) swap(i, j);
pa[i] = j;
size[j] += size[i];
if (rk[i] == rk[j]) ++rk[j];
}
void Update(void) {
vector<int> new_root;
for (int i : root) {
if (i == pa[i]) {
new_root.push_back(i);
}
}
root = new_root;
}
} // namespace dsu
vector<int> cc[maxn];
int main(void) {
int m;
scanf("%d%d", &n, &m);
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
dsu::Init();
for (int i = 0; i < n; ++i) {
for (int j : dsu::root) {
cnt_edge[j] = 0;
}
for (int j : g[i]) {
++cnt_edge[dsu::Find(j)];
}
for (int j : dsu::root) {
if (dsu::Find(i) != j && cnt_edge[j] < dsu::size[j]) {
dsu::Unite(i, j);
}
}
dsu::Update();
}
int cnt_cc = 0;
for (int i = 0; i < n; ++i) {
cc[dsu::Find(i)].push_back(i);
cnt_cc += dsu::Find(i) == i;
}
printf("%d\n", cnt_cc);
for (int i = 0; i < n; ++i) {
if (dsu::Find(i) == i) {
printf("%d", (int)cc[i].size());
for (int j : cc[i]) {
printf(" %d", j + 1);
}
putchar('\n');
}
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
long long nextLong() {
long long x;
scanf("%I64d", &x);
return x;
}
double nextDouble() {
double x;
scanf("%lf", &x);
return x;
}
const int BUFSIZE = 1000000;
char buf[BUFSIZE + 1];
string nextString() {
scanf("%s", buf);
return buf;
}
string nextLine() {
gets(buf);
return buf;
}
int stringToInt(string s) {
stringstream in(s);
int x;
in >> x;
return x;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
Point operator-(Point op) const { return Point(x - op.x, y - op.y); }
Point operator+(Point op) const { return Point(x + op.x, y + op.y); }
Point operator*(double op) const { return Point(x * op, y * op); }
double operator*(Point op) const { return x * op.x + y * op.y; }
double operator%(Point op) const { return x * op.y - y * op.x; }
double length2() { return x * x + y * y; }
double length() { return sqrt(length2()); }
};
Point nextPoint() {
double x = nextDouble();
double y = nextDouble();
return Point(x, y);
}
class EdgeSet {
vector<set<int> > m_set;
public:
EdgeSet() : m_set(501000) {}
void add(int x, int y) {
if (x > y) {
swap(x, y);
}
m_set[x].insert(y);
}
bool check(int x, int y) {
if (x > y) {
swap(x, y);
}
return m_set[x].find(y) != m_set[x].end();
}
};
int main() {
int n = nextInt();
int m = nextInt();
EdgeSet missedEdges;
for (int i = 0; i < m; ++i) {
int x = nextInt() - 1;
int y = nextInt() - 1;
missedEdges.add(x, y);
}
vector<bool> used(n, false);
vector<int> unused;
for (int i = 0; i < n; ++i) {
unused.push_back(i);
}
int resSize = 0;
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) {
if (!used[i]) {
++resSize;
queue<int> q;
q.push(i);
used[i] = true;
res[i] = resSize;
while (!q.empty()) {
int at = q.front();
q.pop();
for (int j = 0; j < unused.size(); ++j) {
int to = unused[j];
int x = at;
int y = to;
if (x > y) {
swap(x, y);
}
if (!missedEdges.check(x, y)) {
used[to] = true;
res[to] = resSize;
q.push(to);
}
}
int newSize = 0;
for (int j = 0; j < unused.size(); ++j) {
int x = unused[j];
if (!used[x]) {
unused[newSize++] = x;
}
}
unused.resize(newSize);
}
}
}
vector<vector<int> > ans(resSize);
for (int i = 0; i < n; ++i) {
ans[res[i] - 1].push_back(i);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) {
printf("%d", ans[i].size());
for (int j = 0; j < ans[i].size(); ++j) {
printf(" %d", ans[i][j] + 1);
}
printf("\n");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | import java.io.*;
import java.util.*;
public class Main {
static FastReader in;
static PrintWriter out;
static int g [][];
static int deg [];
static int edges [][];
static ArrayList<ArrayList<Integer>> answer = new ArrayList<ArrayList<Integer>>();
public static void solve () {
int n = in.nextInt();
int m = in.nextInt();
g = new int [n][];
deg = new int [n];
edges = new int [m][2];
for (int i = 0; i < m; i++) {
int v1 = in.nextInt() - 1;
int v2 = in.nextInt() - 1;
deg[v1]++;
deg[v2]++;
edges[i][0] = v1;
edges[i][1] = v2;
}
for (int i = 0; i < n; i++) {
g [i] = new int [deg[i]];
deg[i] = 0;
}
for (int i = 0; i < m; i++) {
int v1 = edges[i][0];
int v2 = edges[i][1];
g[v1][deg[v1]++] = v2;
g[v2][deg[v2]++] = v1;
}
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
set.add(i);
}
LinkedList<Integer> q = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
if (set.contains(i)) {
q.add(i);
ArrayList<Integer> list = new ArrayList<Integer>();
set.remove(i);
while (!q.isEmpty()) {
int v = q.pollFirst();
list.add(v + 1);
HashSet<Integer> removeSet = new HashSet<Integer>();
for (int j = 0; j < g[v].length; j++) {
int to = g[v][j];
if (set.contains(to)) {
removeSet.add(to);
set.remove(to);
}
}
for (int to : set) {
q.add(to);
}
if (set.size() > 0) {
set.clear();
}
for (int rmv : removeSet) {
set.add(rmv);
}
}
answer.add(list);
}
}
out.println(answer.size());
for (int i = 0; i < answer.size(); i++) {
ArrayList<Integer> list = answer.get(i);
out.print(list.size());
for (int v : list) {
out.print(" "+v);
}
out.println();
}
}
public static void main(String[] args) {
in = new FastReader(System.in);
// in = new FastReader(new FileInputStream("input.txt"));
out = new PrintWriter(System.out);
// out = new PrintWriter(new FileOutputStream("output.txt"));
int t = 1;
while (t-- > 0)
solve();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
Integer nextInt() {
return Integer.parseInt(next());
}
Long nextLong() {
return Long.parseLong(next());
}
Double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
} | JAVA |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
const long long int mod = 1e9 + 7;
const int maxn = 3005;
set<long long int> unvisited;
vector<long long int> g[500005];
vector<vector<long long int>> cmps;
void dfs(long long int u) {
vector<long long int> temp;
(cmps.back()).push_back(u);
for (auto v : unvisited) {
if (!binary_search((g[u]).begin(), (g[u]).end(), v)) {
temp.push_back(v);
}
}
for (auto v : temp) {
unvisited.erase(v);
}
for (auto v : temp) {
dfs(v);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m;
cin >> n >> m;
for (long long int i = 1; i <= m; i++) {
long long int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (long long int i = 1; i <= n; i++) sort((g[i]).begin(), (g[i]).end());
for (long long int i = 1; i <= n; i++) {
unvisited.insert(i);
}
long long int cnt = 0;
for (long long int i = 1; i <= n; i++) {
if (unvisited.count(i)) {
unvisited.erase(i);
cmps.emplace_back();
dfs(i);
cnt++;
}
}
cout << cnt << " ";
for (long long int i = 0; i < cnt; i++) {
cout << cmps[i].size() << " ";
for (auto v : cmps[i]) cout << v << " ";
cout << " ";
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
set<pair<int, int>> edge;
set<pair<int, int>> v;
int cnt[MAXN];
void dfs(int &V, vector<int> &a) {
a.push_back(V);
vector<int> q;
int now;
for (auto i : v) {
now = i.second;
if (!edge.count({min(V, now), max(V, now)})) {
q.push_back(now);
}
}
int qs = q.size();
for (int i = qs - 1; i >= 0; --i) v.erase({-cnt[q[i]], q[i]});
for (int i = 0; i < qs; ++i) dfs(q[i], a);
}
int n, m, a1, a2;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> a1 >> a2;
edge.insert({min(a1, a2), max(a1, a2)});
++cnt[a1];
++cnt[a2];
}
for (int i = 1; i <= n; ++i) {
v.insert({-cnt[i], i});
}
vector<vector<int>> ans;
vector<int> k;
int p;
while (!v.empty()) {
ans.push_back(k);
p = (*v.begin()).second;
v.erase({-cnt[p], p});
dfs(p, ans[ans.size() - 1]);
}
cout << ans.size() << '\n';
for (auto i : ans) {
cout << i.size() << " ";
for (auto j : i) cout << j << " ";
cout << '\n';
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 5e5 + 10;
vector<int> adj[maxn];
int ind;
bool haveB[maxn];
int tmp[maxn], par[maxn], sz[maxn];
int find(int v) { return (par[v] < 0 ? v : par[v] = find(par[v])); }
void Union(int v, int u) {
v = find(v);
u = find(u);
if (v == u) return;
if (sz[v] > sz[u]) swap(v, u);
par[v] = u;
sz[u] += sz[v];
}
void input() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &v, &u);
u--;
v--;
adj[v].push_back(u);
adj[u].push_back(v);
}
int mn = maxn;
for (int i = 0; i < n; i++) {
par[i] = -1;
sz[i] = 1;
sort(adj[i].begin(), adj[i].end());
if (adj[i].size() < mn) {
mn = adj[i].size();
ind = i;
}
}
}
bool inA[maxn];
void fill_have() {
for (auto v : adj[ind]) inA[v] = 1;
for (int i = 0; i < n; i++)
if (i != ind && !inA[i]) par[i] = ind;
for (auto v : adj[ind]) {
for (auto u : adj[v])
if (inA[u]) tmp[v]++;
if (adj[v].size() - tmp[v] < n - adj[ind].size()) haveB[v] = 1;
}
}
void CG() {
for (auto i : adj[ind])
for (auto v : adj[ind])
if (*lower_bound(adj[i].begin(), adj[i].end(), v) != v) Union(i, v);
}
void solve() {
fill_have();
CG();
sz[ind] = n - 1 - adj[ind].size();
for (auto v : adj[ind])
if (haveB[v]) Union(v, ind);
}
vector<int> ans[maxn];
void output() {
for (int i = 0; i < n; i++) {
int p = find(i);
if (p == -1)
ans[i].push_back(i + 1);
else
ans[p].push_back(i + 1);
}
int cnt = 0;
for (int i = 0; i < n; i++)
if (ans[i].size() > 0) cnt++;
cout << cnt << endl;
for (int i = 0; i < n; i++)
if (ans[i].size() > 0) {
printf("%d ", ans[i].size());
for (auto v : ans[i]) printf("%d ", v);
cout << endl;
}
}
int main() {
input();
solve();
output();
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, m;
unordered_set<long long> e;
set<long long> s;
vector<vector<long long>> ans;
queue<long long> q;
void bfs(long long node) {
s.erase(node);
q.push(node);
vector<long long> visited;
while (!q.empty()) {
node = q.front();
visited.push_back(node);
q.pop();
vector<long long> to_erase;
for (auto it = s.begin(); it != s.end(); it++) {
if (e.count(n * node + *it)) continue;
to_erase.push_back(*it);
}
for (auto x : to_erase) s.erase(x), q.push(x);
}
ans.push_back(visited);
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (long long i = 1; i <= n; i++) s.insert(i);
for (long long i = 1; i <= m; i++) {
long long a, b;
cin >> a >> b;
e.insert(n * a + b);
e.insert(n * b + a);
}
for (long long i = 1; i <= n; i++) {
if (!s.count(i)) continue;
bfs(i);
}
cout << ans.size() << '\n';
for (auto x : ans) {
cout << x.size() << ' ';
for (auto y : x) cout << y << " ";
cout << '\n';
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > g;
set<int> st;
vector<vector<int> > ans;
vector<int> cur;
bool yes(int v, int u) {
if (g[v].size() == 0) {
return false;
}
int l = 0, r = int(g[v].size()) - 1;
while (l != r) {
int mid = (l + r) / 2;
if (g[v][mid] >= u) {
r = mid;
} else {
l = mid + 1;
}
}
return g[v][l] == u;
}
void dfs(int v) {
st.erase(st.find(v));
cur.push_back(v);
int u = 1;
while (st.upper_bound(u) != st.end()) {
int to = *st.upper_bound(u);
if (!yes(v, to)) {
dfs(to);
}
u = to;
}
}
int main(int argc, char *argv[]) {
int m;
scanf("%d%d", &n, &m);
g.resize(n + 1);
while (m--) {
int from, to;
scanf("%d%d", &from, &to);
g[from].push_back(to);
g[to].push_back(from);
}
for (int i = 1; i <= n; ++i) {
sort(g[i].begin(), g[i].end());
st.insert(i);
}
for (int i = 1; i <= n; ++i) {
if (st.find(i) != st.end()) {
cur.clear();
dfs(i);
ans.push_back(cur);
}
}
printf("%d\n", int(ans.size()));
for (int i = 0; i < ans.size(); ++i) {
printf("%d ", int(ans[i].size()));
for (int j = 0; j < ans[i].size(); ++j) {
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m;
set<int> s;
const int nax = 5e5 + 4;
int link[nax];
set<pair<int, int>> edge;
vector<vector<int>> group;
inline void make_set(int i) { link[i] = i; }
int Find(int a) {
if (link[a] == a) return a;
return (link[a] = Find(link[a]));
}
void Unite(int a, int b) {
a = Find(a);
b = Find(b);
if (a == b) return;
link[a] = b;
}
int component() {
set<int> had;
for (int i = 1; i <= n; ++i) {
had.insert(Find(i));
group[Find(i)].push_back(i);
}
return ((int)had.size());
}
void bfs(int i) {
queue<int> q;
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
s.erase(u);
vector<int> temp;
for (auto &c : s) {
if (edge.find({min(u, c), max(u, c)}) == edge.end()) {
Unite(u, c);
q.push(c);
temp.push_back(c);
}
}
for (auto &c : temp) s.erase(c);
}
}
void solve() {
cin >> n >> m;
group.resize(n + 1);
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
edge.insert({min(u, v), max(u, v)});
}
for (int i = 1; i <= n; ++i) make_set(i);
for (int i = 1; i <= n; ++i) s.insert(i);
for (int i = 1; i <= n; ++i) {
if (s.find(i) != s.end()) bfs(i);
}
cout << component() << '\n';
for (int i = 1; i <= n; ++i) {
if (!group[i].empty()) {
cout << group[i].size() << ' ';
for (auto &c : group[i]) cout << c << ' ';
cout << '\n';
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) {
solve();
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int ata[(int)(5e5 + 10)], S[(int)(5e5 + 10)], S2[(int)(5e5 + 10)], i, j, k, n,
m, x, y, z;
int s, H[(int)(5e5 + 10)];
vector<int> arr[(int)(5e5 + 10)], v[(int)(5e5 + 10)];
set<int> first;
int atabul(int x) {
if (ata[x] == x) return x;
return ata[x] = atabul(ata[x]);
}
int main() {
cin >> n >> m;
for (i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
arr[max(x, y)].push_back(min(x, y));
}
set<int>::iterator it, it2;
for (i = 1; i <= n; i++) {
S[i] = 1;
ata[i] = i;
for (j = 0; j < arr[i].size(); j++) S2[atabul(arr[i][j])]--;
for (it = first.begin(); it != first.end();) {
int t = *it;
if (S2[t]) {
S[i] += S[t];
ata[t] = i;
it2 = it++;
first.erase(it2);
} else
it++;
}
for (it = first.begin(); it != first.end(); it++) S2[*it] = S[*it];
first.insert(i);
S2[i] = S[i];
}
for (i = 1; i <= n; i++) {
int t = atabul(i);
if (!H[t]) {
H[t] = 1;
s++;
}
v[t].push_back(i);
}
cout << s << '\n';
for (i = 1; i <= n; i++) {
if (v[i].size()) {
cout << v[i].size() << ' ';
for (j = 0; j < v[i].size(); j++) cout << v[i][j] << ' ';
puts("");
}
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
long long nextLong() {
long long x;
scanf("%I64d", &x);
return x;
}
double nextDouble() {
double x;
scanf("%lf", &x);
return x;
}
const int BUFSIZE = 1000000;
char buf[BUFSIZE + 1];
string nextString() {
scanf("%s", buf);
return buf;
}
string nextLine() {
gets(buf);
return buf;
}
int stringToInt(string s) {
stringstream in(s);
int x;
in >> x;
return x;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
Point operator-(Point op) const { return Point(x - op.x, y - op.y); }
Point operator+(Point op) const { return Point(x + op.x, y + op.y); }
Point operator*(double op) const { return Point(x * op, y * op); }
double operator*(Point op) const { return x * op.x + y * op.y; }
double operator%(Point op) const { return x * op.y - y * op.x; }
double length2() { return x * x + y * y; }
double length() { return sqrt(length2()); }
};
Point nextPoint() {
double x = nextDouble();
double y = nextDouble();
return Point(x, y);
}
class EdgeSet {
set<pair<int, int> > m_set;
public:
void add(int x, int y) {
if (x > y) {
swap(x, y);
}
m_set.insert(make_pair(x, y));
}
bool check(int x, int y) {
if (x > y) {
swap(x, y);
}
return m_set.find(make_pair(x, y)) != m_set.end();
}
};
int main() {
int n = nextInt();
int m = nextInt();
EdgeSet missedEdges;
for (int i = 0; i < m; ++i) {
int x = nextInt() - 1;
int y = nextInt() - 1;
missedEdges.add(x, y);
}
vector<bool> used(n, false);
vector<int> unused;
for (int i = 0; i < n; ++i) {
unused.push_back(i);
}
int resSize = 0;
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) {
if (!used[i]) {
++resSize;
queue<int> q;
q.push(i);
used[i] = true;
res[i] = resSize;
while (!q.empty()) {
int at = q.front();
q.pop();
for (int j = 0; j < unused.size(); ++j) {
int to = unused[j];
int x = at;
int y = to;
if (x > y) {
swap(x, y);
}
if (!missedEdges.check(x, y)) {
used[to] = true;
res[to] = resSize;
q.push(to);
}
}
int newSize = 0;
for (int j = 0; j < unused.size(); ++j) {
int x = unused[j];
if (!used[x]) {
unused[newSize++] = x;
}
}
unused.resize(newSize);
}
}
}
vector<vector<int> > ans(resSize);
for (int i = 0; i < n; ++i) {
ans[res[i] - 1].push_back(i);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) {
printf("%d", ans[i].size());
for (int j = 0; j < ans[i].size(); ++j) {
printf(" %d", ans[i][j] + 1);
}
printf("\n");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
std::set<int> not_used;
std::vector<int> comp;
int n;
std::vector<std::pair<int, int>> edges;
int fastscan() {
int number = 0;
register int c = getchar();
for (; c > 47 && c < 58; c = getchar()) number = number * 10 + c - 48;
return number;
}
int have_edge(int a, int b) {
if (a < b) std::swap(a, b);
std::pair<int, int> p{a, b};
return !std::binary_search(edges.begin(), edges.end(), p);
}
void dfs(int x) {
comp.push_back(x);
std::vector<int> go;
for (auto it = not_used.begin(); it != not_used.end(); ++it)
if (have_edge(x, *it)) go.push_back(*it);
for (size_t i = 0; i < go.size(); i++) not_used.erase(go[i]);
for (size_t i = 0; i < go.size(); i++) dfs(go[i]);
}
int main() {
n = fastscan();
int m = fastscan();
for (int i = 0; i < m; i++) {
int a = fastscan();
int b = fastscan();
a--;
b--;
if (a < b) std::swap(a, b);
edges.push_back({a, b});
}
std::sort(edges.begin(), edges.end());
for (int i = 0; i < n; i++) not_used.insert(i);
std::vector<std::vector<int>> ans;
for (int i = 0; i < n; i++) {
if (not_used.count(i)) {
not_used.erase(i);
dfs(i);
ans.push_back(comp);
comp.clear();
}
}
printf("%d\n", ans.size());
for (size_t i = 0; i < ans.size(); i++) {
printf("%d ", ans[i].size());
for (size_t j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j] + 1);
printf("\n");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 45;
map<pair<int, int>, bool> mapa;
int n, m;
vector<int> comp[N];
int main() {
scanf("%i%i", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%i%i", &a, &b);
if (a > b) {
swap(a, b);
}
mapa[pair<int, int>(a, b)] = 1;
}
set<int> s;
for (int i = 1; i <= n; i++) {
s.insert(i);
}
int br = 0;
for (int i = 1; i <= n; i++) {
if (s.find(i) == s.end()) {
continue;
}
queue<int> q;
q.push(i);
s.erase(i);
while (!q.empty()) {
int u = q.front();
comp[br].push_back(u);
q.pop();
vector<int> brisi;
for (auto f : s) {
int t = u, r = f;
if (t > r) {
swap(t, r);
}
if (!mapa.count(pair<int, int>(t, r))) {
q.push(f);
brisi.push_back(f);
}
}
for (auto f : brisi) {
s.erase(f);
}
}
br++;
}
printf("%i\n", br);
for (int i = 0; i < br; i++) {
printf("%i ", comp[i].size());
for (int j = 0; j < comp[i].size(); j++) {
printf("%i ", comp[i][j]);
}
printf("\n");
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5 * 100 * 1000 + 1000;
vector<int> v[maxn];
vector<int> group[maxn];
int _next[maxn];
bool mark[maxn];
int n;
int find_next(int x) {
if (_next[x] == x) return x;
return _next[x] = find_next(_next[x]);
}
void dfs(int x, int cnt) {
group[cnt].push_back(x);
mark[x] = true;
_next[x] = x + 1;
for (int i = find_next(1), j = 0; i <= n; i = find_next(i + 1)) {
while (j < v[x].size() && v[x][j] < i) j++;
if (j == v[x].size() || i != v[x][j]) dfs(i, cnt);
}
}
int main() {
ios_base::sync_with_stdio(false);
int m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i <= n; i++) sort(v[i].begin(), v[i].end());
for (int i = 1; i <= n + 1; i++) _next[i] = i;
int cnt = 0;
for (int i = find_next(1); i <= n; i = find_next(i))
if (mark[i] == false) dfs(i, cnt), cnt++;
cout << cnt << endl;
for (int i = 0; i < cnt; i++) {
cout << group[i].size() << " ";
for (int j = 0; j < group[i].size(); j++) cout << group[i][j] << " ";
cout << endl;
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
unordered_set<int> adj[maxn];
unordered_set<int> unvis;
vector<int> c[maxn];
int ctr = 0;
void dfs(int u) {
c[ctr].push_back(u);
vector<int> tovisit;
for (int x : unvis) {
if (adj[u].count(x) == 0) {
tovisit.push_back(x);
}
}
for (int x : tovisit) {
unvis.erase(x);
}
for (int x : tovisit) {
dfs(x);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].insert(v);
adj[v].insert(u);
}
for (int i = 1; i <= n; i++) {
unvis.insert(i);
}
while (unvis.size()) {
int cur = *unvis.begin();
unvis.erase(unvis.begin());
dfs(cur);
ctr++;
}
cout << ctr << '\n';
for (int i = 0; i < ctr; i++) {
cout << c[i].size() << " ";
for (int x : c[i]) cout << x << " ";
cout << '\n';
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, M = 1e6 + 10;
int n, m, i, j, cnt;
bool mark[N];
vector<int> adj[N], comp[N];
queue<int> q;
unordered_set<int> unmarked;
void bfs() {
while (!q.empty()) {
int u = q.front();
comp[cnt].push_back(u);
q.pop();
for (auto v : adj[u]) {
if (!mark[v]) unmarked.erase(v);
}
while (!unmarked.empty()) {
mark[*unmarked.begin()] = 1;
q.push(*unmarked.begin());
unmarked.erase(unmarked.begin());
}
for (auto v : adj[u]) {
if (!mark[v]) unmarked.insert(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) unmarked.insert(i);
for (i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (i = 0; i < n; i++) {
if (!mark[i]) {
unmarked.erase(i);
mark[i] = 1;
q.push(i);
bfs();
cnt++;
}
}
printf("%d\n", cnt);
for (i = 0; i < cnt; i++) {
printf("%d ", comp[i].size());
for (auto v : comp[i]) printf("%d ", v + 1);
printf("\n");
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool debug;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;
vector<int> w[nax];
set<int> pozostale;
vector<vector<int> > res;
int first[nax];
void zacznij(int pierwszy) {
vector<int> kol;
kol.push_back(pierwszy);
for (int i = 0; i <= ((int)kol.size()) - 1; ++i) {
pozostale.erase(kol[i]);
for (auto b : w[kol[i]]) ++first[b];
if (i == (int)kol.size() - 1) {
int memo_size = (int)kol.size();
for (auto b : pozostale)
if (first[b] != memo_size) kol.push_back(b);
}
}
res.push_back(kol);
for (auto a : kol)
for (auto b : w[a]) first[b] = 0;
}
int main(int argc, char *argv[]) {
debug = argc > 1;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) pozostale.insert(i);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
w[a].push_back(b);
w[b].push_back(a);
}
for (int a = 1; a <= n; ++a)
if (pozostale.find(a) != pozostale.end()) zacznij(a);
printf("%d\n", (int)res.size());
for (auto &w : res) {
printf("%d ", (int)w.size());
for (auto a : w) printf("%d ", a);
puts("");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | // package cf190;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
public class CFE {
private static final long MOD = 1_000_000_007;
private static final String INPUT = "4 4\n" +
"1 2\n" +
"1 3\n" +
"4 2\n" +
"4 3\n";
private PrintWriter out;
private FastScanner sc;
public static void main(String[] args) {
new CFE().run();
}
public void run() {
sc = new FastScanner(oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
try {
solve();
} catch (ExitException ignored) {
}
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
static class IntArray {
int[] array;
int size;
IntArray() {
this(20);
}
IntArray(int capacity) {
this.array = new int[capacity];
}
void add(int n) {
ensureCapacity(size + 1);
array[size++] = n;
}
void sort() {
shuffle();
Arrays.sort(array, 0, size);
}
void shuffle() {
for (int i = 0; i < size; i++) {
int r = i + (int) (Math.random() * (size - i));
{ int tmp = array[i]; array[i] = array[r]; array[r] = tmp; }
}
}
boolean contains(int n) {
int lo = -1;
int hi = size;
while (hi-lo > 1) {
int med = (hi+lo)>>1;
int mValue = array[med];
if (mValue == n) return true;
else if (mValue < n) lo = med;
else hi = med;
}
return false;
}
private void ensureCapacity(int cap) {
if (cap > array.length) grow(cap);
}
private void grow(int cap) {
int oldCapacity = array.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
array = Arrays.copyOf(array, newCapacity);
}
}
int n;
IntArray cities;
IntArray[] ways;
public void solve() {
n = sc.nextInt();
int m = sc.nextInt();
cities = new IntArray(n);
for (int i = 1; i <= n; i++) cities.add(i);
ways = new IntArray[n+1];
for (int i = 1; i <= n; i++) ways[i] = new IntArray();
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
ways[a].add(b);
ways[b].add(a);
}
for (int i = 1; i <= n; i++) ways[i].sort();
List<IntArray> ans = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (cities.contains(i)) {
IntArray cans = new IntArray(10);
dfs(i, cans);
ans.add(cans);
}
}
out.println(ans.size());
for (IntArray an : ans) {
out.print(an.size + " ");
int[] array = an.array;
for (int i = 0; i < an.size; i++) {
out.print(array[i] + " ");
}
out.println();
}
}
private void dfs(int ccity, IntArray cans) {
cans.add(ccity);
IntArray newCities = new IntArray(cities.size);
IntArray edges = ways[ccity];
IntArray nexts = new IntArray();
int ie = 0;
int nEdge = ie < edges.size ? edges.array[ie++] : Integer.MAX_VALUE;
int[] array = cities.array;
for (int i = 0, length = cities.size; i < length; i++) {
int city = array[i];
if (city == ccity) continue;
while (city > nEdge) nEdge = ie < edges.size ? edges.array[ie++] : Integer.MAX_VALUE;
if (city < nEdge) nexts.add(city);
else newCities.add(city);
}
cities = newCities;
for (int i = 0; i < nexts.size; i++) {
dfs(nexts.array[i], cans);
}
}
//********************************************************************************************
//********************************************************************************************
//********************************************************************************************
private void times(int n, IntConsumer consumer) {
for (int i = 0; i < n; i++) {
try {
consumer.accept(i);
} catch (ExitException ignored) {
}
}
}
private static class ExitException extends RuntimeException {
}
private void answer(Object ans) {
out.println(ans);
throw new ExitException();
}
private static int lowerBound(long[] arr, long key) {
int lo = 0;
int hi = arr.length - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (key <= arr[mid]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return arr[lo] < key ? lo + 1 : lo;
}
private static int upperBound(long[] arr, long key) {
int lo = 0;
int hi = arr.length - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (key >= arr[mid]) {
lo = mid + 1;
} else {
hi = mid;
}
}
return arr[lo] <= key ? lo + 1 : lo;
}
private static int ceil(double d) {
int ret = (int) d;
return ret == d ? ret : ret + 1;
}
private static int round(double d) {
return (int) (d + 0.5);
}
private static int gcd(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
private static long gcd(long a, long b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
private int[] readIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = sc.nextInt();
}
return res;
}
private long[] readLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = sc.nextLong();
}
return res;
}
@SuppressWarnings("unused")
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
} | JAVA |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 100 * 1000 + 14;
vector<int> comp[MAXN], adj[MAXN];
set<int> res;
int ans = 0;
void dfs(int v) {
comp[ans].push_back(v);
auto it = res.begin();
while (it != res.end()) {
if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) {
int u = *it;
res.erase(u);
dfs(u);
it = res.upper_bound(u);
} else
it++;
}
return;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= n; i++) res.insert(i);
while (!res.empty()) {
int v = *res.begin();
res.erase(v);
ans++;
dfs(v);
}
cout << ans << '\n';
for (int i = 1; i <= ans; i++) {
cout << comp[i].size() << ' ';
for (auto u : comp[i]) cout << u << ' ';
cout << '\n';
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
unordered_set<long long> second[500001];
unordered_set<long long> unvisited;
vector<vector<long long>> ans;
vector<long long> temp;
void dfs(long long node) {
temp.push_back(node);
unvisited.erase(node);
vector<long long> t;
for (auto c : unvisited) {
if (!second[node].count(c)) t.push_back(c);
}
for (auto c : t) unvisited.erase(c);
for (auto &c : t) dfs(c);
}
void solve() {
long long n, m;
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
--a;
--b;
second[a].insert(b);
second[b].insert(a);
}
for (long long i = 0; i < n; i++) unvisited.insert(i);
for (long long i = 0; i < n; i++) {
if (unvisited.count(i)) {
temp.clear();
dfs(i);
ans.push_back(temp);
}
}
cout << ans.size() << "\n";
for (auto &c : ans) {
cout << c.size() << " ";
for (auto d : c) cout << d + 1 << " ";
cout << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 5e5 + 30, mod = 1e9 + 7, inf = 1e12;
const long double eps = 0.0001;
int n, m;
bool visited[N];
set<pair<int, int> > st;
set<int> ve;
vector<int> g[N];
void bfs(int v, int t) {
g[t].push_back(v);
queue<int> q;
q.push(v);
visited[v] = 1;
ve.erase(v);
while (!q.empty()) {
int u = q.front();
q.pop();
bool f = 0;
if (ve.empty()) break;
for (auto it = ve.begin(); it != ve.end(); it) {
if (ve.empty()) return;
if (f) it = ve.begin();
f = 0;
int i = *it;
if (!visited[i] and st.find({min(i, u), max(i, u)}) == st.end()) {
q.push(i);
visited[i] = 1;
g[t].push_back(i);
it = ve.erase(it);
} else
it++;
if (ve.empty()) return;
}
}
}
int main() {
ios ::sync_with_stdio(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
st.insert({x, y});
}
if (m < n - 1) {
cout << 1 << '\n' << n << ' ';
for (int i = 1; i < n + 1; i++) cout << i << ' ';
return 0;
}
for (int i = 1; i < n + 1; i++) ve.insert(i);
int t = 0;
for (int i = 1; i < n + 1; i++) {
if (!visited[i]) {
t++;
bfs(i, t);
}
}
cout << t << '\n';
for (int i = 1; i < t + 1; i++) {
cout << g[i].size() << ' ';
for (int u : g[i]) cout << u << ' ';
cout << '\n';
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 77, Mod = 1e9 + 7;
int n, m, p[N], t[N], A;
vector<int> a[N], sz[N];
vector<int> s;
int find(int v) { return p[v] < 0 ? v : (p[v] = find(p[v])); }
void merge(int v, int u) {
v = find(v);
u = find(u);
if (u == v) {
return;
}
p[v] += p[u];
p[u] = v;
}
int main() {
fill(p, p + N, -1);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int v, u;
scanf("%d %d", &v, &u);
a[v].push_back(u);
a[u].push_back(v);
}
for (int v = 1; v <= n; v++) {
for (auto u : a[v]) {
t[find(u)]++;
}
vector<int> r;
for (auto x : s) {
if (t[x] == -p[x]) {
r.push_back(x);
continue;
}
merge(v, x);
}
r.push_back(v);
s.clear();
for (auto x : r) s.push_back(x);
for (auto u : a[v]) {
t[find(u)] = 0;
}
}
for (int i = 1; i <= n; i++) {
sz[find(i)].push_back(i);
A += p[i] < 0;
}
printf("%d\n", A);
for (int i = 1; i <= n; i++) {
if (sz[i].size()) {
printf("%d ", sz[i].size());
for (auto x : sz[i]) printf("%d ", x);
printf("\n");
}
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.io.InputStream;
/**
* TEST
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE_Counter solver = new TaskE_Counter();
solver.solve(1, in, out);
out.close();
}
}
class TaskE_Counter {
Graph g;
Queue<Integer> queue = new ArrayDeque<>();
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
g = new Graph();
g.initGraph(n, m);
for (int i = 0; i < m; ++i) {
g.addEdge(in.nextInt() - 1, in.nextInt() - 1);
}
Set<Integer> contains = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
contains.add(i);
}
int size, iv, i, v, j, to;
List<Integer> color;
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (iv = 0; iv < n; ++iv) {
if (contains.contains(iv)) {
queue.add(iv);
color = new ArrayList<Integer>();
contains.remove(iv);
while (!queue.isEmpty()) {
int element = queue.remove();
color.add(element + 1);
Set<Integer> toRemove = new HashSet<>();
for (j = g.first[element]; j != -1; j = g.next[j]) {
to = g.to[j];
if (contains.contains(to)) {
toRemove.add(to);
contains.remove(to);
}
}
for (int x : contains) {
queue.add(x);
}
if (contains.size() > 0) {
contains.clear();
}
contains.addAll(toRemove);
}
result.add(color);
}
}
out.println(result.size());
for (List<Integer> list : result) {
out.print(list.size());
for (int vv : list) {
out.print(" " + vv);
}
out.println();
}
}
}
class Graph {
public int[] from;
public int[] to;
public int[] first;
public int[] next;
public int nVertex;
public int nEdges;
public int curEdge;
public Graph() {}
public void initGraph(int n, int m) {
curEdge = 0;
nVertex = n;
nEdges = m;
from = new int[m * 2];
to = new int[m * 2];
first = new int[n];
next = new int[m * 2];
Arrays.fill(first, -1);
}
public void addEdge(int a, int b) {
next[curEdge] = first[a];
first[a] = curEdge;
to[curEdge] = b;
from[curEdge] = a;
++curEdge;
next[curEdge] = first[b];
first[b] = curEdge;
to[curEdge] = a;
from[curEdge] = b;
++curEdge;
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine().trim();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
} | JAVA |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
long long nextLong() {
long long x;
scanf("%I64d", &x);
return x;
}
double nextDouble() {
double x;
scanf("%lf", &x);
return x;
}
const int BUFSIZE = 1000000;
char buf[BUFSIZE + 1];
string nextString() {
scanf("%s", buf);
return buf;
}
string nextLine() {
gets(buf);
return buf;
}
int stringToInt(string s) {
stringstream in(s);
int x;
in >> x;
return x;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
Point operator-(Point op) const { return Point(x - op.x, y - op.y); }
Point operator+(Point op) const { return Point(x + op.x, y + op.y); }
Point operator*(double op) const { return Point(x * op, y * op); }
double operator*(Point op) const { return x * op.x + y * op.y; }
double operator%(Point op) const { return x * op.y - y * op.x; }
double length2() { return x * x + y * y; }
double length() { return sqrt(length2()); }
};
Point nextPoint() {
double x = nextDouble();
double y = nextDouble();
return Point(x, y);
}
int main() {
int n = nextInt();
int m = nextInt();
set<pair<int, int> > missedEdges;
for (int i = 0; i < m; ++i) {
int x = nextInt() - 1;
int y = nextInt() - 1;
if (x > y) {
swap(x, y);
}
missedEdges.insert(make_pair(x, y));
}
vector<bool> used(n, false);
vector<int> unused;
for (int i = 0; i < n; ++i) {
unused.push_back(i);
}
int resSize = 0;
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) {
if (!used[i]) {
++resSize;
queue<int> q;
q.push(i);
used[i] = true;
res[i] = resSize;
while (!q.empty()) {
int at = q.front();
q.pop();
for (int j = 0; j < unused.size(); ++j) {
int to = unused[j];
int x = at;
int y = to;
if (x > y) {
swap(x, y);
}
if (missedEdges.find(make_pair(x, y)) == missedEdges.end()) {
used[to] = true;
res[to] = resSize;
q.push(to);
}
}
int newSize = 0;
for (int j = 0; j < unused.size(); ++j) {
int x = unused[j];
if (!used[x]) {
unused[newSize++] = x;
}
}
unused.resize(newSize);
}
}
}
vector<vector<int> > ans(resSize);
for (int i = 0; i < n; ++i) {
ans[res[i] - 1].push_back(i);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) {
printf("%d", ans[i].size());
for (int j = 0; j < ans[i].size(); ++j) {
printf(" %d", ans[i][j] + 1);
}
printf("\n");
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 500000 + 1000;
int n, m, ans, par[MAXN];
vector<int> adj[MAXN], out[MAXN];
bool mark[MAXN];
int find(int x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void dfs(int x) {
mark[x] = 1;
par[x] = x + 1;
for (register int i = 0; i < adj[x].size(); i++) {
int u = adj[x][i] + 1;
while (find(u) < (i == adj[x].size() - 1 ? n : adj[x][i + 1])) {
out[ans].push_back(find(u));
dfs(find(u));
}
}
}
inline void read(register int *k) {
register char c;
*k = 0;
do {
c = getchar();
} while (c < '0' || c > '9');
do {
*k = *k * 10 + c - '0';
c = getchar();
} while (c >= '0' && c <= '9');
}
int main() {
read(&n);
read(&m);
for (register int i = 0; i < m; i++) {
int x, y;
read(&x);
read(&y);
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (register int i = 0; i < n; i++) adj[i].push_back(-1);
for (register int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end());
for (register int i = 0; i < n + 100; i++) par[i] = i;
for (register int i = 0; i < n; i++)
if (mark[i] == 0) {
out[++ans].push_back(i);
dfs(i);
}
cout << ans << endl;
for (register int i = 1; i < ans + 1; i++) {
cout << out[i].size();
for (register int j = 0; j < out[i].size(); j++)
cout << ' ' << out[i][j] + 1;
cout << endl;
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long int div_floor(const long long int &a, const long long int &b) {
return a / b - (((a ^ b) < 0) and a % b);
}
long long int div_ceil(const long long int &a, const long long int &b) {
return a / b + (((a ^ b) >= 0) and a % b);
}
vector<int> parent;
vector<int> cnt;
void make_set(int v) {
parent[v] = v;
cnt[v] = 1;
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (cnt[a] < cnt[b]) swap(a, b);
parent[b] = a;
cnt[a] += cnt[b];
}
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int> > adj(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (auto &e : adj) sort(e.begin(), e.end());
int vertex = 0;
for (int i = 0; i < n; i++)
if ((long long int)adj[i].size() < (long long int)adj[vertex].size())
vertex = i;
vector<int> nodes = adj[vertex];
nodes.push_back(vertex);
sort(nodes.begin(), nodes.end());
int n1 = (long long int)nodes.size();
map<int, int> translate;
for (int i = 0; i < n1; i++) translate[nodes[i]] = i;
parent.resize(n1);
cnt.resize(n1);
for (int i = 0; i < n1; i++) make_set(i);
for (int i = 0; i < n1; i++) {
int cntr = 0;
for (auto &v : adj[nodes[i]])
if (!binary_search(nodes.begin(), nodes.end(), v)) cntr++;
for (auto &v : nodes)
if (!binary_search(adj[nodes[i]].begin(), adj[nodes[i]].end(), v))
union_sets(i, translate[v]);
if (cntr + n1 < n) union_sets(i, translate[vertex]);
}
int ans = 0;
for (int i = 0; i < n1; i++) ans += parent[i] == i;
cout << ans << '\n';
vector<vector<int> > comps(n);
for (int i = 0; i < n1; i++) comps[nodes[find_set(i)]].push_back(nodes[i]);
for (int i = 0; i < n; i++)
if (!binary_search(nodes.begin(), nodes.end(), i)) {
comps[nodes[find_set(translate[vertex])]].push_back(i);
}
for (auto &v : comps) {
if ((long long int)v.size() > 0) {
cout << (long long int)v.size() << " ";
for (auto &e : v) cout << e + 1 << " ";
cout << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) solve();
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const int N = 1e6 + 5;
set<int> se;
vector<int> w[N];
int ind[N];
vector<vector<int> > ans;
void go(int n) {
vector<int> v;
v.emplace_back(n);
for (int i = 0; i < v.size(); i++) {
int num = v[i];
se.erase(num);
for (auto j : w[num]) ind[j]++;
if (i == v.size() - 1) {
int mem = v.size();
for (auto k : se)
if (ind[k] != mem) v.emplace_back(k);
}
}
ans.emplace_back(v);
for (auto i : v)
for (auto j : w[i]) ind[j] = 0;
}
void _main_main() {
long long int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) se.insert(i);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
w[x].emplace_back(y);
w[y].emplace_back(x);
}
for (int i = 1; i <= n; i++)
if (se.find(i) != se.end()) go(i);
cout << ans.size() << "\n";
for (auto i : ans) {
cout << i.size() << " ";
for (auto j : i) cout << j << " ";
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int testCase = 1;
for (int i = 0; i < testCase; i++) {
_main_main();
}
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 100 * 1000 + 14;
vector<int> comp[MAXN], adj[MAXN];
set<int> res;
int ans = 0;
void dfs(int v) {
comp[ans].push_back(v);
auto it = res.begin();
while (it != res.end()) {
if (find(adj[v].begin(), adj[v].end(), *it) == adj[v].end()) {
int u = *it;
res.erase(u);
dfs(u);
it = res.upper_bound(u);
} else
it++;
}
return;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= n; i++) res.insert(i);
while (!res.empty()) {
int v = *res.begin();
res.erase(v);
ans++;
dfs(v);
}
cout << ans << '\n';
for (int i = 1; i <= ans; i++) {
cout << comp[i].size() << ' ';
for (auto u : comp[i]) cout << u << ' ';
cout << '\n';
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int num[500050], bel[500050], wz[500050];
int a[500050], dl[500050];
int n, m, top, now;
int fir[500050], en[2000400], nex[2000400], tot;
int get() {
char t = getchar();
while (t < '0' || t > '9') t = getchar();
int x = 0;
while (t >= '0' && t <= '9') {
x *= 10;
x += t - '0';
t = getchar();
}
return x;
}
void tjb(int x, int y) {
en[++tot] = y;
nex[tot] = fir[x];
fir[x] = tot;
}
bool cmp(int a, int b) {
return ((bel[a] < bel[b]) || ((bel[a] == bel[b]) && (a < b)));
}
int main() {
n = get();
m = get();
for (int i = 1; i <= m; i++) {
int x = get();
int y = get();
tjb(x, y);
tjb(y, x);
}
for (int i = 1; i <= n; i++) dl[i] = wz[i] = i;
for (int i = 1; i <= n; i++) {
if (now < i) {
now = i;
++top;
}
int x = dl[i];
bel[x] = top;
++num[top];
int td = n;
for (int k = fir[x]; k; k = nex[k]) {
int j = en[k];
if (wz[j] <= now) continue;
if (wz[j] > td) continue;
int y = dl[td];
swap(wz[y], wz[j]);
swap(dl[wz[y]], dl[wz[j]]);
--td;
}
now = td;
}
for (int i = 1; i <= n; i++) a[i] = i;
sort(a + 1, a + 1 + n, cmp);
printf("%d\n", top);
now = 0;
for (int i = 1; i <= top; i++) {
printf("%d ", num[i]);
for (int j = 1; j <= num[i] - 1; j++) {
++now;
printf("%d ", a[now]);
}
++now;
printf("%d\n", a[now]);
}
return 0;
}
| CPP |
190_E. Counter Attack | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.
Input
The first line contains two space-separated integers n and m (1 β€ n β€ 5Β·105, 0 β€ m β€ 106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of cities that are connected by the i-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once.
Output
On the first line print number k β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following k lines first print ti (1 β€ ti β€ n) β the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.
Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
2
2 1 4
2 2 3
Input
3 1
1 2
Output
1
3 1 2 3
Note
In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | 2 | 11 | #include <bits/stdc++.h>
const int N = 500005;
int H[N], id;
struct Edge {
int v, nt;
} e[N * 4];
int n, m;
int next[N];
std::vector<int> block[N];
int num, head;
int p[N];
void AddEdge(int u, int v) {
e[id].v = v;
e[id].nt = H[u];
H[u] = id++;
}
int main() {
scanf("%d%d", &n, &m);
id = 1;
num = 0;
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
AddEdge(a, b);
AddEdge(b, a);
}
for (int i = 1; i <= n; i++) next[i] = i + 1;
for (head = 1; head <= n; num++) {
block[num].push_back(head);
head = next[head];
int pre;
for (int i = 0; i < block[num].size(); i++) {
int u = block[num][i];
for (int j = H[u]; j; j = e[j].nt) p[e[j].v] = u;
for (int j = head; j <= n; j = next[j])
if (p[j] != u) {
block[num].push_back(j);
if (j == head)
head = next[head];
else
next[pre] = next[j];
} else
pre = j;
}
}
printf("%d\n", num);
for (int i = 0; i < num; i++) {
printf("%d", block[i].size());
for (int j = 0; j < block[i].size(); j++) printf(" %d", block[i][j]);
puts("");
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e2 + 10;
const int INF = 1e8 + 100;
int dp[MAXN][MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN];
int a[MAXN][MAXN];
int memo(int r, int d, int R) {
if (vis[r][d][R]) return dp[r][d][R];
vis[r][d][R] = true;
int D = r + d - R;
int &res = dp[r][d][R];
res = -INF;
if (r + d == 0) {
res = a[0][0];
return res;
}
int temp = a[r][d] + a[R][D];
if (R == r) temp = a[r][d];
if (r && R) res = max(res, temp + memo(r - 1, d, R - 1));
if (r && D) res = max(res, temp + memo(r - 1, d, R));
if (d && R) res = max(res, temp + memo(r, d - 1, R - 1));
if (d && D) res = max(res, temp + memo(r, d - 1, R));
return res;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> a[i][j];
cout << memo(n - 1, n - 1, n - 1);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mx[2] = {1, 0};
const int MAXN = 310;
const int INF = 1e9;
int N;
int A[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void setmax(int& a, int b) {
if (a < b) {
a = b;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
for (int a = 0; a < N; a++) {
for (int b = 0; b < N; b++) {
for (int c = 0; c < N; c++) {
dp[a][b][c] = -INF;
}
}
}
dp[0][0][0] = A[0][0];
for (int a = 0; a < N; a++) {
for (int b = 0; b < N; b++) {
for (int c = 0; c < N; c++) {
if (dp[a][b][c] == -INF) continue;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int na = a + mx[i], nb = a + b + 1 - na;
int nc = c + mx[j], nd = a + b + 1 - nc;
auto in = [&](int x) -> bool { return 0 <= x && x < N; };
if (in(na) && in(nb) && in(nc) && in(nd)) {
int get = (na == nc ? A[na][nb] : A[na][nb] + A[nc][nd]);
setmax(dp[na][nb][nc], dp[a][b][c] + get);
}
}
}
}
}
}
cout << dp[N - 1][N - 1][N - 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[305 << 1][305][305];
int a[305][305], n;
int way[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int main() {
scanf("%d", &n);
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
}
memset(dp, 0x81, sizeof(dp));
dp[0][1][1] = a[1][1];
int r;
for (i = 1; i <= 2 * n - 2; i++) {
for (j = 1; j <= i + 1 && j <= n; j++) {
for (k = 1; k <= i + 1 && k <= n; k++) {
for (r = 0; r < 4; r++) {
dp[i][j][k] =
max(dp[i][j][k], dp[i - 1][j - way[r][0]][k - way[r][1]]);
}
dp[i][j][k] +=
a[j][i - j + 2] + a[k][i - k + 2] - (j == k ? a[k][i - k + 2] : 0);
}
}
}
printf("%d\n", dp[2 * n - 2][n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int pts[301][301];
int dp[2 * 301][301][301];
inline int _dp(int i, int j, int k) {
if (i < 0 or j < 0 or k < 0) return -100000000;
if (dp[i][j][k] != 1e8) return dp[i][j][k];
long long ans = 0;
ans = max(max(_dp(i - 1, j - 1, k), _dp(i - 1, j - 1, k - 1)),
max(_dp(i - 1, j, k - 1), _dp(i - 1, j, k)));
ans += pts[j][i - j];
if (j != k) ans += pts[k][i - k];
dp[i][j][k] = ans;
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) cin >> pts[i][j];
for (int i = 0; i < 2 * n; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k) dp[i][j][k] = 1e8;
dp[0][0][0] = pts[0][0];
cout << _dp(2 * n - 2, n - 1, n - 1);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
#pragma warning(disable : 4786)
using namespace std;
int MIN(int a, int b) { return a < b ? a : b; }
int MAX(int a, int b) { return a > b ? a : b; }
int GCD(int a, int b) {
while (b) b ^= a ^= b ^= a %= b;
return a;
}
int LCM(int a, int b) { return a * (b / GCD(a, b)); }
void SWAP(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
const double PI = acos(-1);
const double EPS = 1e-11;
int arr[310][310], memo[310][310][310], N, M;
int dp(int i, int j, int k) {
int l = i + j - k;
if (i == N - 1 && j == M - 1) {
return arr[i][j];
}
if (i == N || j == M || k >= N || l >= N) {
return -(1 << 29);
;
}
int &ret = memo[i][j][k];
if (ret != -1) {
return ret;
}
int res, r1, val;
res = -(1 << 29);
;
if (i == k && j == l) {
val = arr[i][j];
} else {
val = arr[i][j] + arr[k][l];
}
r1 = dp(i + 1, j, k + 1) + val;
res = MAX(res, r1);
r1 = dp(i + 1, j, k) + val;
res = MAX(res, r1);
r1 = dp(i, j + 1, k + 1) + val;
res = MAX(res, r1);
r1 = dp(i, j + 1, k) + val;
res = MAX(res, r1);
return ret = res;
}
int main() {
int T, res, n, i, ind, m, j;
while (scanf("%d", &n) != EOF) {
int a = -(1 << 29);
;
memset(memo, -1, sizeof(memo));
memset(arr, 0, sizeof(arr));
m = n;
for (i = 0; i < (n); i++) {
for (j = 0; j < (m); j++) {
scanf("%d", &arr[i][j]);
}
}
N = n;
M = m;
res = dp(0, 0, 0);
printf("%d\n", res);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[305][305];
bool done[305][305][305];
int memo[305][305][305];
int dr[] = {1, 0}, dc[] = {0, 1};
int solve(int r1, int c1, int r2, int c2) {
if (r1 == n - 1 && c1 == n - 1) return 0;
int &ret = memo[r1][c1][r2];
if (!done[r1][c1][r2]) {
done[r1][c1][r2] = true;
ret = -90000000;
for (int i = 0; i < 2; ++i) {
int x1 = r1 + dr[i], y1 = c1 + dc[i];
if (x1 < n && y1 < n) {
for (int j = 0; j < 2; ++j) {
int x2 = r2 + dr[j], y2 = c2 + dc[j];
if (x2 < n && y2 < n) {
int aux = solve(x1, y1, x2, y2);
if (x1 == x2 && y1 == y2)
aux += a[x1][y1];
else
aux += a[x1][y1] + a[x2][y2];
ret = max(ret, aux);
}
}
}
}
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]);
printf("%d\n", solve(0, 0, 0, 0) + a[0][0]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 600; i++)
for (int j = 0; j <= 600; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = k % 2, y = 1 - x;
if (i >= k || j >= k) continue;
if (k - i > n || k - j > n) {
f[x][i][j] = -inf;
continue;
}
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[606][303][303];
int solve(int n, vector<vector<int>> &vec, int d, int x1, int x2) {
if (d > n + n - 2) {
return 0;
}
if (x1 >= n || x2 >= n) {
return -0x3f3f3f3f;
}
int y1 = d - x1, y2 = d - x2;
if (y1 >= n || y2 >= n) {
return -0x3f3f3f3f;
}
if (dp[d][x1][x2] != -1) {
return dp[d][x1][x2];
}
int temp = 0;
temp =
max(max(solve(n, vec, d + 1, x1 + 1, x2 + 1),
solve(n, vec, d + 1, x1 + 1, x2)),
max(solve(n, vec, d + 1, x1, x2 + 1), solve(n, vec, d + 1, x1, x2)));
if (x1 == x2 && y1 == y2) {
temp += vec[x1][y1];
} else {
temp += vec[x1][y1] + vec[x2][y2];
}
dp[d][x1][x2] = temp;
return temp;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> vec(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> vec[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << solve(n, vec, 0, 0, 0);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int mod = 1e9 + 7;
int dp[602][302][302], n, a[303][303];
int solve(int dg, int i1, int i2) {
if (i1 > n || i2 > n) return -inf;
int j1 = dg - i1;
int j2 = dg - i2;
if (j1 > n || j2 > n) return -inf;
if (dg == (2 * n)) return a[i1][j1];
int &ret = dp[dg][i1][i2];
if (ret != (-inf)) return ret;
if (i1 == i2 && j1 == j2) {
ret = a[i1][j1];
} else
ret = a[i1][j1] + a[i2][j2];
int tmp = -inf;
for (int x = 0; x <= 1; x++) {
for (int y = 0; y <= 1; y++) {
tmp = max(tmp, solve(dg + 1, i1 + x, i2 + y));
}
}
ret += tmp;
return ret;
}
void init() {
for (int i = 1; i <= 2 * n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) dp[i][j][k] = -inf;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
init();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
int ses = solve(2, 1, 1);
cout << ses << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 310;
const int mod = 1e9 + 7;
const int inf = -1e9;
int a[N][N];
int cache[2 * N][N][N];
int n;
int dp(int diag, int i1, int i2) {
if (i1 > n || i2 > n) return -1e9;
int j1 = diag - i1;
int j2 = diag - i2;
if (j1 > n || j2 > n) return -1e9;
if (diag == 2 * n) return a[i1][j1];
int &ans = cache[diag][i1][i2];
if (ans != inf) return ans;
if (i1 == i2)
ans = a[i1][j1];
else
ans = a[i1][j1] + a[i2][j2];
int mx = -1e9;
mx = max(mx, dp(diag + 1, i1, i2 + 1));
mx = max(mx, dp(diag + 1, i1 + 1, i2));
mx = max(mx, dp(diag + 1, i1 + 1, i2 + 1));
mx = max(mx, dp(diag + 1, i1, i2));
ans += mx;
return ans;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> a[i][j];
}
}
for (int i = 1; i <= 2 * n; ++i)
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= n; ++k) cache[i][j][k] = inf;
cout << dp(2, 1, 1) << '\n';
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300 + 10;
const int INF = 1e9 + 2;
int n;
int cc[MAXN][MAXN];
int d[2 * MAXN][MAXN][MAXN];
bool fit(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; }
int get(int a, int b, int c) {
if (d[a][b][c] != INF) return d[a][b][c];
if (a == 2 * n - 2) return d[a][b][c] = 0;
int r1, r2, col1, col2;
d[a][b][c] = -INF;
if (a < n - 1) {
r1 = b, r2 = c;
col1 = a - b, col2 = a - c;
if (fit(r1 + 1, col1) && fit(r2 + 1, col2))
d[a][b][c] =
max(d[a][b][c], get(a + 1, b + 1, c + 1) + cc[r1 + 1][col1] +
cc[r2 + 1][col2] -
int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]);
if (fit(r1 + 1, col1) && fit(r2, col2 + 1))
d[a][b][c] =
max(d[a][b][c],
get(a + 1, b + 1, c) + cc[r1 + 1][col1] + cc[r2][col2 + 1] -
int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]);
if (fit(r1, col1 + 1) && fit(r2 + 1, col2))
d[a][b][c] =
max(d[a][b][c],
get(a + 1, b, c + 1) + cc[r1][col1 + 1] + cc[r2 + 1][col2] -
int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]);
if (fit(r1, col1 + 1) && fit(r2, col2 + 1))
d[a][b][c] = max(d[a][b][c],
get(a + 1, b, c) + cc[r1][col1 + 1] + cc[r2][col2 + 1] -
int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]);
} else {
int t = a - n;
r1 = t + b + 1, r2 = t + 1 + c;
col1 = a - r1, col2 = a - r2;
if (fit(r1 + 1, col1) && fit(r2 + 1, col2))
d[a][b][c] = max(d[a][b][c],
get(a + 1, b, c) + cc[r1 + 1][col1] + cc[r2 + 1][col2] -
int(r2 == r1 && col1 == col2) * cc[r1 + 1][col1]);
if (fit(r1 + 1, col1) && fit(r2, col2 + 1))
d[a][b][c] =
max(d[a][b][c],
get(a + 1, b, c - 1) + cc[r1 + 1][col1] + cc[r2][col2 + 1] -
int(r2 == r1 + 1 && col2 + 1 == col1) * cc[r2][col2 + 1]);
if (fit(r1, col1 + 1) && fit(r2 + 1, col2))
d[a][b][c] =
max(d[a][b][c],
get(a + 1, b - 1, c) + cc[r1][col1 + 1] + cc[r2 + 1][col2] -
int(r2 + 1 == r1 && col1 + 1 == col2) * cc[r2 + 1][col2]);
if (fit(r1, col1 + 1) && fit(r2, col2 + 1))
d[a][b][c] =
max(d[a][b][c], get(a + 1, b - 1, c - 1) + cc[r1][col1 + 1] +
cc[r2][col2 + 1] -
int(r1 == r2 && col1 == col2) * cc[r2][col2 + 1]);
}
return d[a][b][c];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> cc[i][j];
for (int i = 0; i < 2 * MAXN; i++)
for (int j = 0; j < MAXN; j++)
for (int w = 0; w < MAXN; w++) d[i][j][w] = INF;
cout << get(0, 0, 0) + cc[0][0] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class RelayRace {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int N = sc.nextInt();
int[][] a = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i][j] = sc.nextInt();
}
}
int diagLen = 2 * N - 1;
// int[][][] dp = new int[diagLen][N][N];
// dp[0][0][0] = a[0][0];
int[][] prev = new int[1][1];
prev[0][0] = a[0][0];
for (int d = 1; d < diagLen; d++) {
int len = (d < N) ? d : (diagLen - d - 1);
int[][] next = new int[len + 1][len + 1];
// System.out.println("len = " + len);
for (int x = 0; x <= len; x++) {
// System.out.println("px:");
Location pX = diagToRect(d, x, N);
int x0 = (d >= N) ? x : (x - 1);
for (int y = x; y <= len; y++) {
// System.out.println("py:");
int y0 = (d >= N) ? y : (y - 1);
Location pY = diagToRect(d, y, N);
if (x == y) {
int poss1 = getDP(prev, d - 1, x0, y0, N);
int poss2 = getDP(prev, d - 1, x0, y0 + 1, N);
int poss3 = getDP(prev, d - 1, x0 + 1, y0 + 1, N);
int best = Math.max(poss1, Math.max(poss2, poss3));
// System.out.format("X == Y: (%d, %d, %d): (%d, %d), p1 = %d, p2 = %d, p3 = %d\n", d, x, y, x0, y0, poss1, poss2, poss3);
// dp[d][x][y] = best + a[pX.X][pX.Y];
next[x][y] = best + a[pX.R][pX.C];
} else {
int poss1 = getDP(prev, d - 1, x0, y0, N);
int poss2 = getDP(prev, d - 1, x0, y0 + 1, N);
int poss3 = getDP(prev, d - 1, x0 + 1, y0, N);
int poss4 = getDP(prev, d - 1, x0 + 1, y0 + 1, N);
int best = Math.max(poss1, Math.max(poss2, Math.max(poss3, poss4)));
// System.out.format("X != Y: (%d, %d, %d): (%d, %d), p1 = %d, p2 = %d, p3 = %d, p4 = %d\n", d, x, y, x0, y0, poss1, poss2, poss3, poss4);
// dp[d][x][y] = best + a[pX.X][pX.Y]+ a[pY.X][pY.Y];
next[x][y] = best + a[pX.R][pX.C]+ a[pY.R][pY.C];
}
// System.out.format("dp[%d][%d][%d] = %d\n", d, x, y, next[x][y]);
}
}
prev = next;
}
System.out.println(prev[0][0]);
}
public static int getDP(int[][][] dp, int d, int x, int y, int N) {
int diagLen = 2 * N - 1;
int len = (d < N) ? d : (diagLen - d - 1);
if (x < 0 || x > len || y < 0 || y > len) {
return -1000000;
} else {
return dp[d][x][y];
}
}
public static int getDP(int[][] dp, int d, int x, int y, int N) {
int diagLen = 2 * N - 1;
int len = (d < N) ? d : (diagLen - d - 1);
if (x < 0 || x > len || y < 0 || y > len) {
return -1000000;
} else {
// System.out.println(d + " " + diagLen + " " + len + " " + dp.length);
return dp[x][y];
}
}
public static Location diagToRect(int d, int x, int N) {
int r0 = (d < N) ? d : (N - 1);
int c0 = (d < N) ? 0 : (d - N + 1);
int r = r0 - x;
int c = c0 + x;
// System.out.format("(%d, %d) => (%d, %d)\n", d, x, r, c);
return new Location(r, c);
}
public static boolean valid(Location p, int N) {
return (p.C >= 0 && p.C < N && p.R >= 0 && p.R < N);
}
public static class Location {
public int R, C;
public Location(int r, int c) {
this.R = r;
this.C = c;
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e14;
const double eps = 1e-10;
const double pi = acos(-1.0);
int a[305][610];
int dp[305][305][3];
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j + i]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= 1; k++) {
dp[i][j][k] = -1e9;
}
}
}
dp[1][1][2 & 1] = a[1][2];
for (int d = 2; d <= n + n - 1; d++) {
for (int i = 1; i <= min(d + 1, n); i++) {
for (int j = 1; j <= min(d + 1, n); j++) {
int tx = i + 1;
int ty = j;
int& res = dp[tx][ty][(d + 1) & 1];
if (tx == ty)
res = max(res, dp[i][j][(d & 1)] + a[tx][d + 1]);
else
res = max(res, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]);
tx = i + 1;
ty = j + 1;
int& res1 = dp[tx][ty][(d + 1) & 1];
if (tx == ty)
res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1]);
else
res1 = max(res1, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]);
tx = i;
ty = j;
int& res2 = dp[tx][ty][(d + 1) & 1];
if (tx == ty)
res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1]);
else
res2 = max(res2, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]);
tx = i;
ty = j + 1;
int& res3 = dp[tx][ty][(d + 1) & 1];
if (tx == ty)
res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1]);
else
res3 = max(res3, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]);
dp[i][j][d & 1] = -1e9;
}
}
}
cout << dp[n][n][(n + n) & 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class E {
static int[][] a;
static int[][][] dp;
static int n;
public static void main(String[] args) throws IOException {
InputReader in = new InputReader();
n = in.nextInt();
a = new int[n][n];
dp = new int[n + 5][n + 5][n + 5];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = in.nextInt();
for (int i = 0; i < n + 5; i++)
for (int j = 0; j < n + 5; j++)
for (int j2 = 0; j2 < n + 5; j2++)
dp[i][j][j2] = -1;
System.out.println(go(0, 0, 0, 0));
}
public static int go(int i, int j, int i1, int j1) {
if (i < 0 || j < 0 || i >= n || j >= n || i1 >= n || i1 < 0 || j1 >= n
|| j1 < 0)
return -10000000;
if (i == n - 1 && j == n - 1 && i == j1 && i == i1)
return a[i][j];
if (dp[i][j][i1] != -1)
return dp[i][j][i1];
int res = -100000000;
int a1, a2, a3, a4;
res = Math.max(res, a1 = go(i + 1, j, i1 + 1, j1));
res = Math.max(res, a2 = go(i, j + 1, i1, j1 + 1));
res = Math.max(res, a3 = go(i + 1, j, i1, j1 + 1));
res = Math.max(res, a4 = go(i, j + 1, i1 + 1, j1));
if (i != i1 || j != j1)
res += a[i1][j1];
return dp[i][j][i1] = res + a[i][j];
}
static class InputReader {
BufferedReader in;
StringTokenizer st;
String cur;
public InputReader() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(cur = in.readLine());
}
public boolean hasNext() throws IOException {
if (!st.hasMoreElements()) {
cur = in.readLine();
if (cur == null)
return false;
st = new StringTokenizer(cur);
}
return true;
}
public String next() throws IOException {
while (!st.hasMoreElements())
st = new StringTokenizer(cur = in.readLine());
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
}
} | JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int maxint = 999999999;
int dp[2][maxn][maxn];
int a[maxn][maxn];
int n;
bool judge(int x, int y) { return (x >= 1 && y <= n); }
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
dp[1][1][1] = a[1][1];
for (int i = 2; i <= 2 * n - 1; i++) {
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) dp[i % 2][j][k] = -maxint;
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k;
if (judge(x1, y1) == false || judge(x2, y2) == false) continue;
int value;
if (j == k)
value = a[x1][y1];
else
value = a[x1][y1] + a[x2][y2];
if (j - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value);
}
if (j - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value);
}
if (x1 - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value);
}
if (x1 - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value);
}
}
}
}
printf("%d\n", dp[(2 * n - 1) % 2][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 301;
int a[N][N];
int f[N * 2 + 2][N][N];
int i, j, n, x, y, t;
int max(int b, int c, int d, int e) { return max(max(b, c), max(d, e)); }
void Main() {
memset(f, -10, sizeof(f));
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[1][1][1] = a[1][1];
for (int i = 2; i <= n + n - 1; i++)
for (int x = 1; x <= n; x++)
for (int y = 1; y <= n; y++)
if (i + 1 - x > 0 && i + 1 - y > 0) {
f[i][x][y] = max(f[i - 1][x - 1][y], f[i - 1][x - 1][y - 1],
f[i - 1][x][y - 1], f[i - 1][x][y]);
if (x != y)
f[i][x][y] = f[i][x][y] + a[x][i + 1 - x] + a[y][i + 1 - y];
if (x == y) f[i][x][y] = f[i][x][y] + a[x][i + 1 - x];
}
cout << f[n + n - 1][n][n] << endl;
}
int main() {
while (cin >> n && n) {
Main();
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 301, inf = -(1e9 + 1);
int n;
int a[N][N];
int cache[2 * N][N][N];
int dp(int idx, int i1, int i2) {
if (i1 > n || i2 > n) return -1e9;
int j1 = idx - i1;
int j2 = idx - i2;
if (j1 > n || j2 > n) return -1e9;
if (idx == 2 * n) return a[i1][j1];
int &ans = cache[idx][i1][i2];
if (ans != inf) return ans;
if (i1 == i2)
ans = a[i1][j1];
else
ans = a[i1][j1] + a[i2][j2];
int best = -1e9;
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++) best = max(best, dp(idx + 1, i1 + i, i2 + j));
ans += best;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= 2 * n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) cache[i][j][k] = inf;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
int ans = dp(2, 1, 1);
cout << ans << '\n';
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[305][305];
int p[2] = {0, 1};
int memo[305][305][305];
int dfss(int x1, int x2, int tot) {
if (memo[x1][x2][tot] != -1) return memo[x1][x2][tot];
if (x1 == n && x2 == n && tot == 2 * n) return 0;
int hsl = -900000;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int lol1 = x1 + p[i];
int lol2 = x2 + p[j];
if (x1 > n || x2 > n || (tot - x1) > n || (tot - x2) > n) continue;
if (lol1 == lol2)
hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1]);
else
hsl = max(hsl, dfss(lol1, lol2, tot + 1) + a[lol1][tot - lol1 + 1] +
a[lol2][tot - lol2 + 1]);
}
}
return memo[x1][x2][tot] = hsl;
}
int main() {
scanf("%d", &n);
memset(memo, -1, sizeof(memo));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
}
int ans = dfss(1, 1, 2);
printf("%d\n", ans + a[1][1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int F[605][305][305], a[305][305], n;
int main() {
if (0) freopen("input.txt", "r", stdin);
;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
for (int i = 0; i <= 2 * n; ++i)
for (int j = 0; j <= n; ++j)
for (int k = 0; k <= n; ++k) F[i][j][k] = -1000000000;
F[0][1][1] = a[1][1];
for (int i = 1; i <= 2 * n - 2; ++i)
for (int j = max(1, i - (n - 2)); j <= min(n, i + 1); ++j)
for (int k = max(1, i - (n - 2)); k <= min(n, i + 1); ++k) {
F[i][j][k] = max(F[i - 1][j][k],
max(F[i - 1][j - 1][k],
max(F[i - 1][j][k - 1], F[i - 1][j - 1][k - 1])));
if (F[i][j][k] == -1000000000) continue;
F[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2];
if (j == k) F[i][j][k] -= a[j][i - j + 2];
}
printf("%d\n", F[2 * n - 2][n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int mat[300 + 10][300 + 10];
int sol[300 + 10][300 + 10][300 + 10];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) sol[i][j][k] = -100000000;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &mat[j][i]);
for (int w = 0; w < n; w++) {
if (w > 0) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
sol[w][i][j] = sol[w - 1][i][j] + mat[w][i] + mat[w][j];
else
sol[w][i][j] = sol[w - 1][i][j] + mat[w][i];
} else
sol[0][0][0] = mat[0][0];
for (int j = 0; j < n; j++)
for (int i = 1; i < j; i++)
sol[w][i][j] = max(sol[w][i][j], sol[w][i - 1][j] + mat[w][i]);
for (int i = 1; i < n; i++)
sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i]);
for (int i = 1; i < n; i++)
sol[w][i][i] = max(sol[w][i][i], sol[w][i - 1][i - 1] + mat[w][i]);
for (int i = 0; i <= n; i++)
for (int j = i + 1; j < n; j++)
sol[w][i][j] = max(sol[w][i][j], sol[w][i][j - 1] + mat[w][j]);
}
printf("%d", sol[n - 1][n - 1][n - 1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.*;
import java.util.*;
public class relay {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(r.readLine());
int[][][] dp = new int[2][n][n];
int[][] mat = new int[n][n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(r.readLine());
for (int j = 0; j < n; j++) {
mat[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < 2; i++) {
for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) dp[i][k][j] = Integer.MIN_VALUE;
}
}
dp[0][0][0] = mat[0][0];
for (int i = 1; i < 2 * n - 1; i++) {
int lim;
int start = 0;
if (i < n - 1) {
lim = i + 1;
} else if (i == n - 1) {
lim = n;
} else {
lim = 2 * n - 1 - i;
start = (i % n) + 1;
}
for (int k = start; k < start + lim; k++) {
for (int j = start; j < start + lim; j++) {
int c = mat[j][i - j];
if (j != k) {
c += mat[k][i - k];
}
int yk = i - k;
int yj = i - j;
if (k - 1 >= 0) {
if (yj - 1 >= 0) dp[1][k][j] = Math.max(dp[1][k][j], dp[0][k - 1][j] + c);
if (j - 1 >= 0) {
dp[1][k][j] = Math.max(dp[1][k][j], dp[0][k - 1][j - 1] + c);
}
}
if (yk - 1 >= 0 && j - 1 >= 0) dp[1][k][j] = Math.max(dp[1][k][j], dp[0][k][j - 1] + c);
if (yj - 1 >= 0 && yk - 1 >= 0) dp[1][k][j] = Math.max(dp[1][k][j], dp[0][k][j] + c);
}
}
for (int k = 0; k < dp[0].length; k++) {
for (int j = 0; j < dp[0][k].length; j++) {
dp[0][k][j] = dp[1][k][j];
dp[1][k][j] = Integer.MIN_VALUE;
}
}
}
w.write("" + dp[0][n - 1][n - 1] + "\n"); w.close(); System.exit(0);
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[0x12d * 2][0x12d][0x12d];
int v[0x12d][0x12d];
inline int max(int a, int b, int c, int d) {
if (a < b) a = b;
if (a < c) a = c;
if (a < d) a = d;
return a;
}
int main() {
int N;
cin >> N;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) scanf("%d", v[i] + j);
for (int i = 0; i < 2 * N; ++i)
for (int j = 0; j <= N; ++j)
for (int k = 0; k <= N; ++k) dp[i][j][k] = -0x80000000;
dp[1][1][1] = v[1][1];
for (int i = 2; i <= N; ++i)
for (int j = 1; j <= i; ++j) {
dp[i][j][j] =
v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j],
dp[i - 1][j][j - 1], dp[i - 1][j][j]);
for (int k = j + 1; k <= i; ++k)
dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] +
max(dp[i - 1][j][k], dp[i - 1][j - 1][k],
dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]);
}
for (int i = N + 1; i < 2 * N; ++i)
for (int j = i + 1 - N; j <= N; ++j) {
dp[i][j][j] =
v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j],
dp[i - 1][j][j - 1], dp[i - 1][j][j]);
for (int k = j + 1; k <= N; ++k)
dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] +
max(dp[i - 1][j][k], dp[i - 1][j - 1][k],
dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]);
}
cout << dp[2 * N - 1][N][N] << '\n';
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300 + 2;
const int MAXA = 1e3 + 3;
const int INF = 0x3f3f3f3f;
int n;
int a[MAXN][MAXN];
int dp[MAXN * 2][MAXN][MAXN];
bool outOfRange(int x, int y) { return x < 0 || x >= n || y < 0 || y >= n; }
void upmax(int &x, int v) {
if (x < v) x = v;
}
void solve() {
for (int i = 0; i < (int)(2 * n); ++i)
for (int j = 0; j < (int)(n); ++j)
for (int k = 0; k < (int)(n); ++k) dp[i][j][k] = -INF;
dp[0][0][0] = a[0][0];
for (int i = 0; i < (int)(2 * (n - 1)); ++i)
for (int j = 0; j < (int)(n); ++j)
for (int k = 0; k < (int)(n); ++k) {
int dx[2] = {0, 1};
int dy[2] = {1, 0};
for (int d1 = 0; d1 < 2; ++d1) {
int nx1 = j + dx[d1];
int ny1 = (i - j) + dy[d1];
if (outOfRange(nx1, ny1)) continue;
for (int d2 = 0; d2 < 2; ++d2) {
int nx2 = k + dx[d2];
int ny2 = (i - k) + dy[d2];
if (outOfRange(nx2, ny2)) continue;
if (nx1 == nx2 && ny1 == ny2)
upmax(dp[i + 1][nx1][nx2], dp[i][j][k] + a[nx1][ny1]);
else
upmax(dp[i + 1][nx1][nx2],
dp[i][j][k] + a[nx1][ny1] + a[nx2][ny2]);
}
}
}
printf("%d\n", dp[2 * (n - 1)][n - 1][n - 1]);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]);
solve();
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long dp[5][305][305];
long long a[305][305];
long long max(long long a, long long b, long long c, long long d) {
return max(a, max(b, max(c, d)));
}
int main(int argc, char** argv) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lld", &a[i][j]);
}
}
dp[0][1][1] = a[1][1] + 100000000000000;
for (int i = 1; i <= (2 * (n - 1)); i++) {
memset(dp[i % 2], 0, sizeof(dp[i % 2]));
for (int j = 1; j <= i + 1; j++) {
for (int k = 1; k <= j; k++) {
long long aa =
((j == k) ? a[j][i - j + 2] : a[j][i - j + 2] + a[k][i - k + 2]);
dp[(i % 2)][j][k] =
max(dp[(i + 1) % 2][j - 1][k], dp[(i + 1) % 2][j][k],
dp[(i + 1) % 2][j][k - 1], dp[(i + 1) % 2][j - 1][k - 1]) +
aa;
}
}
}
cout << dp[0][n][n] - 100000000000000;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long MAX = 2005;
int k;
int dp[605][305][305];
int arr[305][305];
bool ok(int dist, int a, int c) {
int b = dist - a;
int d = dist - c;
return ((0 <= a) && (0 <= b) && (0 <= c) && (0 <= d) && (a < k) && (b < k) &&
(c < k) && (d < k));
}
int add(int dist, int a, int c) {
int ans = 0;
int b = dist - a;
int d = dist - c;
if (a == c && b == d) {
ans += arr[a][b];
} else {
ans += arr[a][b] + arr[c][d];
}
return ans;
}
int main() {
cin >> k;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
scanf("%d", &arr[i][j]);
}
}
memset(dp, -10000, sizeof dp);
dp[0][0][0] = arr[0][0];
for (int d = 0; d < 2 * k - 1; d++) {
for (int a = 0; a < d + 1; a++) {
for (int b = 0; b < d + 1; b++) {
int x = d - a;
int y = d - b;
if (ok(d + 1, a, b + 1)) {
dp[d + 1][a][b + 1] =
max(dp[d + 1][a][b + 1], dp[d][a][b] + add(d + 1, a, b + 1));
}
if (ok(d + 1, a + 1, b)) {
dp[d + 1][a + 1][b] =
max(dp[d + 1][a + 1][b], dp[d][a][b] + add(d + 1, a + 1, b));
}
if (ok(d + 1, a, b)) {
dp[d + 1][a][b] =
max(dp[d + 1][a][b], dp[d][a][b] + add(d + 1, a, b));
}
if (ok(d + 1, a + 1, b + 1)) {
dp[d + 1][a + 1][b + 1] = max(dp[d + 1][a + 1][b + 1],
dp[d][a][b] + add(d + 1, a + 1, b + 1));
}
}
}
}
int d = 2 * k - 2;
cout << dp[d][d / 2][d / 2] << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 600; i++)
for (int j = 0; j <= 600; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = k % 2, y = 1 - x;
if (k - i > n || k - j > n || k - i < 1 || k - j < 1) {
f[x][i][j] = -inf;
continue;
}
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int Map[305][305], F[305 << 1][305][305];
int X[4] = {-1, -1, 0, 0};
int Y[4] = {-1, 0, -1, 0};
int main() {
int i, j, k, n, t;
memset(F, 0xCC, sizeof(F));
for (i = scanf("%d", &n); i <= n; i++)
for (j = 1; j <= n; j++) scanf("%d", &Map[i][j]);
for (F[0][i = 1][1] = Map[1][1]; i <= 2 * n - 2; i++)
for (j = 1; j <= n && j <= i + 1; j++)
for (k = j; k <= n && k <= i + 1; k++) {
for (t = 0; t < 4; t++)
F[i][j][k] = max(F[i][j][k], F[i - 1][j + X[t]][k + Y[t]]);
F[i][j][k] += Map[j][i - j + 2] + Map[k][i - k + 2] -
(j == k ? Map[k][i - k + 2] : 0);
}
printf("%d\n", F[2 * n - 2][n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int n, a[303][303], dp[303][303][303], xx[] = {1, 0}, yy[] = {0, 1}, ans;
int f(int x, int y, int z) {
if (x == n - 1 && y == n - 1) return 0 + a[x][y];
if (dp[x][y][z] != -1e9) return dp[x][y][z];
int ans = -1e9, k = (x + y) - z;
for (int i = 0; i < 2; i++) {
int tx = x + xx[i], ty = y + yy[i];
for (int j = 0; j < 2; j++) {
int tz = z + xx[j], tk = k + yy[j];
if (tx < n && ty < n && tz < n && tk < n) {
if (x == z && y == k)
ans = max(ans, f(tx, ty, tz) + a[x][y]);
else
ans = max(ans, f(tx, ty, tz) + a[x][y] + a[z][k]);
}
}
}
return dp[x][y][z] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int z = 0; z < n; z++) dp[i][j][z] = -1e9;
cin >> a[i][j];
}
}
cout << f(0, 0, 0);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 310;
const int INF = 100000000;
int n, v[N][N], dp[N * 2][N][N];
bool valid(int x, int y) {
if (x <= 0 || x > n || y <= 0 || y > n) return false;
return true;
}
bool same(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2) return true;
return false;
}
int main() {
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &v[i][j]);
dp[0][1][1] = v[1][1];
for (int i = 1; i <= 2 * n - 2; i++)
for (int x1 = 1; x1 <= n; x1++)
for (int x2 = 1; x2 <= n; x2++) {
int y1 = i + 2 - x1;
int y2 = i + 2 - x2;
int t = 0, ans = -INF;
if (!same(x1, y1, x2, y2))
t = v[x1][y1] + v[x2][y2];
else
t = v[x1][y1];
if (!valid(x1, y1) || !valid(x2, y2)) continue;
if (valid(x1 - 1, y1)) {
if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1 - 1][x2 - 1]);
if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1 - 1][x2]);
}
if (valid(x1, y1 - 1)) {
if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1][x2 - 1]);
if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1][x2]);
}
dp[i][x1][x2] = ans;
}
printf("%d\n", dp[2 * n - 2][n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[305][305], dp[2 * 305][305][305];
int f(int r1, int c1, int r2, int c2) {
if (r1 > n || r2 > n || c1 > n || c2 > n) return -100000000;
if (r1 == n && c1 == n) return a[n][n];
int t = r1 + c1 - 1;
if (dp[t][c1][c2] != -1) return dp[t][c1][c2];
int ans = f(r1 + 1, c1, r2 + 1, c2);
ans = max(ans, f(r1 + 1, c1, r2, c2 + 1));
ans = max(ans, f(r1, c1 + 1, r2 + 1, c2));
ans = max(ans, f(r1, c1 + 1, r2, c2 + 1));
ans += a[r1][c1];
if (r1 != r2 || c1 != c2) ans += a[r2][c2];
return dp[t][c1][c2] = ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
for (int t = 1; t < 2 * n; t++) dp[t][i][j] = -1;
}
printf("%d\n", f(1, 1, 1, 1));
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
const double EPS = 1E-8;
class UnionFind {
public:
vector<long long> par;
vector<long long> siz;
vector<long long> maxv;
UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) {
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
void init(long long sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
long long root(long long x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(long long x, long long y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(long long x, long long y) { return root(x) == root(y); }
long long size(long long x) { return siz[root(x)]; }
};
long long mod_pow(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
bool sieve[5000000 + 10];
void make_sieve() {
for (int i = 0; i < 5000000 + 10; ++i) sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < 5000000 + 10; ++i)
if (sieve[i])
for (int j = 2; i * j < 5000000 + 10; ++j) sieve[i * j] = false;
}
bool isprime(long long n) {
if (n == 0 || n == 1) return false;
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % 1000000007;
inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007;
finv[i] = finv[i - 1] * inv[i] % 1000000007;
}
}
long long COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007;
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long modinv(long long a, long long m) {
long long x, y;
extGCD(a, m, x, y);
return mod(x, m);
}
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
struct LazySegmentTree {
private:
int n;
vector<long long> node, lazy;
public:
LazySegmentTree(vector<long long> v) {
int sz = (int)v.size();
n = 1;
while (n < sz) n *= 2;
node.resize(2 * n - 1);
lazy.resize(2 * n - 1, 0);
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
node[i] = node[i * 2 + 1] + node[i * 2 + 2];
}
void eval(int k, int l, int r) {
if (lazy[k] != 0) {
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k] / 2;
lazy[2 * k + 2] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
void add(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
eval(k, l, r);
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
lazy[k] += (r - l) * x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
node[k] = node[2 * k + 1] + node[2 * k + 2];
}
}
long long getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
eval(k, l, r);
if (b <= l || r <= a) return 0;
if (a <= l && r <= b) return node[k];
long long vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
long long vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return vl + vr;
}
};
using Weight = int;
using Flow = int;
struct Edge {
int src, dst;
Weight weight;
Flow cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;
void add_edge(Graph &g, int a, int b, Weight w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) {
g[a].emplace_back(a, b, w);
}
int n;
int a[310][310];
int dp[2 * 310][310][310];
int dx[2] = {0, -1};
int dy[2] = {-1, 0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
for (int i = 0; i <= 2 * n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) dp[i][j][k] = -(1 << 28);
dp[2][1][1] = a[1][1];
for (int iter = 3; iter <= 2 * n; iter++) {
for (int x1 = 1; x1 <= n && x1 < iter; x1++) {
for (int x2 = 1; x2 <= n && x2 < iter; x2++) {
int y1 = iter - x1, y2 = iter - x2;
for (int k = 0; k < 2; k++) {
int X1 = x1 + dx[k], Y1 = y1 + dy[k];
if (X1 < 1 || X1 > n || Y1 < 1 || Y1 > n) continue;
for (int l = 0; l < 2; l++) {
int X2 = x2 + dx[l], Y2 = y2 + dy[l];
if (X2 < 1 || X2 > n || Y2 < 1 || Y2 > n) continue;
if (x1 == x2) {
dp[iter][x1][x2] =
max(dp[iter][x1][x2], dp[iter - 1][X1][X2] + a[x1][y1]);
} else {
dp[iter][x1][x2] =
max(dp[iter][x1][x2],
dp[iter - 1][X1][X2] + a[x1][y1] + a[x2][y2]);
}
}
}
}
}
}
cout << dp[2 * n][n][n] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-5;
const int inf = (1 << 31) - 1;
const int hinf = 1000000000;
const int mod = 1000000007;
int dat[500][500];
int dp[305][305][305];
int dp2[305][305][305];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &dat[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) dp[i][j][k] = dp2[i][j][k] = -hinf;
dp[1][1][1] = dat[1][1];
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k <= i; k++) {
if (dp[i][j][k] > -hinf) {
for (int p = 0; p < 2; p++) {
for (int q = 0; q < 2; q++) {
if (j + p <= i + 1 && k + q <= i + 1) {
if (j + p == k + q) {
dp[i + 1][j + p][k + q] =
max(dp[i + 1][j + p][k + q],
dp[i][j][k] + dat[j + p][i + 2 - (j + p)]);
} else {
dp[i + 1][j + p][k + q] =
max(dp[i + 1][j + p][k + q],
dp[i][j][k] + dat[j + p][i + 2 - (j + p)] +
dat[k + q][i + 2 - (k + q)]);
}
}
}
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
swap(dat[i][j], dat[n - i + 1][n - j + 1]);
}
}
for (int i = 1; i <= n / 2; i++) swap(dat[i][n + 1 - i], dat[n + 1 - i][i]);
dp2[1][1][1] = dat[1][1];
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k <= i; k++) {
if (dp2[i][j][k] > -hinf) {
for (int p = 0; p < 2; p++) {
for (int q = 0; q < 2; q++) {
if (j + p <= i + 1 && k + q <= i + 1) {
if (j + p == k + q) {
dp2[i + 1][j + p][k + q] =
max(dp2[i + 1][j + p][k + q],
dp2[i][j][k] + dat[j + p][i + 2 - (j + p)]);
} else {
dp2[i + 1][j + p][k + q] =
max(dp2[i + 1][j + p][k + q],
dp2[i][j][k] + dat[j + p][i + 2 - (j + p)] +
dat[k + q][i + 2 - (k + q)]);
}
}
}
}
}
}
}
}
int ans = -hinf;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
if (i == j) {
ans = max(ans, dp[n][i][j] + dp2[n][n + 1 - i][n + 1 - j] -
dat[n + 1 - i][i]);
} else {
ans = max(ans, dp[n][i][j] + dp2[n][n + 1 - i][n + 1 - j] -
dat[n + 1 - i][i] - dat[n + 1 - j][j]);
}
}
}
printf("%d\n", ans);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 600; i++)
for (int j = 0; j <= 600; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n && i < k; i++) {
for (int j = 1; j <= n && j < k; j++) {
int x = k % 2, y = 1 - x;
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[0x12d * 2][0x12d][0x12d];
int v[0x12d][0x12d];
int max(int a, int b, int c, int d) {
if (a < b) a = b;
if (a < c) a = c;
if (a < d) a = d;
return a;
}
int main() {
int N;
cin >> N;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) scanf("%d", v[i] + j);
for (int i = 0; i < 2 * N; ++i)
for (int j = 0; j <= N; ++j)
for (int k = 0; k <= N; ++k) dp[i][j][k] = -0x80000000;
dp[1][1][1] = v[1][1];
for (int i = 2; i <= N; ++i)
for (int j = 1; j <= i; ++j) {
dp[i][j][j] =
v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j],
dp[i - 1][j][j - 1], dp[i - 1][j][j]);
for (int k = j + 1; k <= i; ++k)
dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] +
max(dp[i - 1][j][k], dp[i - 1][j - 1][k],
dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]);
}
for (int i = N + 1; i < 2 * N; ++i)
for (int j = i + 1 - N; j <= N; ++j) {
dp[i][j][j] =
v[i + 1 - j][j] + max(dp[i - 1][j - 1][j - 1], dp[i - 1][j - 1][j],
dp[i - 1][j][j - 1], dp[i - 1][j][j]);
for (int k = j + 1; k <= N; ++k)
dp[i][j][k] = v[i + 1 - j][j] + v[i + 1 - k][k] +
max(dp[i - 1][j][k], dp[i - 1][j - 1][k],
dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]);
}
cout << dp[2 * N - 1][N][N] << '\n';
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class PrE {
public static long time;
public static void main(String[] args) throws Exception {
time = System.currentTimeMillis();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(go(br));
br.close();
System.exit(0);
}
public static int go(BufferedReader br) throws Exception {
int n = Integer.parseInt(br.readLine());
if(n == 1) return Integer.parseInt(br.readLine());
int[][] grid = new int[n][n];
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0; j < n; j++) {
grid[i][j] = Integer.parseInt(st.nextToken());
}
}
int[][] best = new int[n][n];
best[0][0] = grid[0][0];
for(int s = 1; s <= 2 * (n - 1); s++) {
int[][] next = new int[n][n];
for(int a = (s > n - 1 ? s - (n - 1) : 0); a < n && a <= s; a++) {
for(int b = a; b < n && b <= s; b++) {
next[a][b] = Integer.MIN_VALUE;
if(a != 0 && best[a-1][b-1] > next[a][b]) next[a][b] = best[a-1][b-1];
if(s-b != 0 && best[a ][b ] > next[a][b]) next[a][b] = best[a ][b ];
if(a != 0 && s-b != 0 && best[a-1][b ] > next[a][b]) next[a][b] = best[a-1][b ];
if(a != b && best[a ][b-1] > next[a][b]) next[a][b] = best[a ][b-1];
next[a][b] += grid[a][s-a] + (a == b ? 0 : grid[b][s-b]);
}
}
best = next;
}
return best[n-1][n-1];
}
public static void checkTime() {
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
}
} | JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a[307][307];
int f[607][307][307];
int moves[][4] = {{1, 0, 1, 0}, {1, 0, 0, 1}, {0, 1, 1, 0}, {0, 1, 0, 1}};
int main() {
int n;
cin >> n;
for (int(i) = (int)(0); (i) < (int)(n); ++(i))
for (int(j) = (int)(0); (j) < (int)(n); ++(j)) cin >> a[i][j];
fill(&f[0][0][0], &f[0][0][0] + sizeof(f) / sizeof(f[0][0][0]), -987654321);
f[0][0][0] = a[0][0];
for (int d = 0; d < 2 * n - 1; ++d)
for (int i1 = 0; i1 <= min(n - 1, d); ++i1)
for (int i2 = 0; i2 <= min(n - 1, d); ++i2) {
int j1 = d - i1, j2 = d - i2;
for (int k = 0; k < 4; ++k) {
int ii1 = i1 + moves[k][0], jj1 = j1 + moves[k][1];
int ii2 = i2 + moves[k][2], jj2 = j2 + moves[k][3];
if (!(0 <= ii1 && ii1 < n && 0 <= jj1 && jj1 < n && 0 <= ii2 &&
ii2 < n && 0 <= jj2 && jj2 < n))
continue;
f[d + 1][ii1][ii2] =
max(f[d + 1][ii1][ii2],
f[d][i1][i2] + a[ii1][jj1] +
(ii1 != ii2 || jj1 != jj2 ? a[ii2][jj2] : 0));
}
}
cout << f[2 * n - 2][n - 1][n - 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int maxint = 999999999;
int dp[2][maxn][maxn];
int a[maxn][maxn];
int n;
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
dp[1][1][1] = a[1][1];
for (int i = 2; i <= 2 * n - 1; i++) {
for (int j = max(1, i - (n - 1)); j <= min(i, n); j++) {
for (int k = max(1, i - (n - 1)); k <= min(i, n); k++) {
dp[i % 2][j][k] = -maxint;
int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k;
int value;
if (j == k)
value = a[x1][y1];
else
value = a[x1][y1] + a[x2][y2];
if (j - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value);
}
if (j - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value);
}
if (x1 - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value);
}
if (x1 - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value);
}
}
}
}
printf("%d\n", dp[(2 * n - 1) % 2][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int ADD(int a, int b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
return (int)a;
}
inline void ADDTO(int &a, int b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
inline void SUBTO(int &a, int b) {
a -= b;
if (a < 0) a += 1000000007;
}
inline int MUL(int a, int b) { return (int)((long long)a * b % 1000000007); }
const int dx[2] = {1, 0};
const int dy[2] = {0, 1};
int a[310][310], dp[310 + 310][310][310], n;
inline bool inside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < n; }
int main() {
while (scanf("%d", &n) == 1) {
for (int i = 0; i < (n); ++i)
for (int j = 0; j < (n); ++j) scanf("%d", &a[i][j]);
int ans = 0;
if (n == 1)
ans = a[0][0];
else {
for (int i = 0; i < (n + n); ++i)
for (int j = 0; j < (n); ++j)
for (int k = 0; k < (n); ++k) dp[i][j][k] = -(1 << 30);
dp[0][0][0] = a[0][0];
for (int s = (0); s <= (n + n - 3); ++s) {
for (int x1 = (max(0, s - n + 1)); x1 <= (min(n - 1, s)); ++x1) {
int y1 = s - x1;
for (int x2 = (max(x1, s - n + 1)); x2 <= (min(n - 1, s)); ++x2) {
int y2 = s - x2;
for (int d1 = 0; d1 < (2); ++d1)
for (int d2 = 0; d2 < (2); ++d2) {
int nx1 = x1 + dx[d1];
int ny1 = y1 + dy[d1];
int nx2 = x2 + dx[d2];
int ny2 = y2 + dy[d2];
if (inside(nx1, ny1) && inside(nx2, ny2) && nx1 <= nx2) {
int score = dp[s][x1][x2] + a[nx1][ny1];
if (nx1 < nx2) score += a[nx2][ny2];
dp[s + 1][nx1][nx2] = max(dp[s + 1][nx1][nx2], score);
}
}
}
}
}
ans = dp[n + n - 2][n - 1][n - 1];
}
printf("%d\n", ans);
}
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Round131E {
private static final int LOCAL_ENV = 0;
static int n;
static int[][] a;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
if (LOCAL_ENV == 1) {
in = new Scanner(new File("input.txt"));
}
} catch (FileNotFoundException e) {
}
n = in.nextInt();
a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}
if (n == 1) {
System.out.println(a[0][0]);
return;
}
int[][] curr;
int[][] next = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(next[i], Integer.MIN_VALUE);
}
next[0][0] = a[0][0];
for (int tot = 0; tot < (n - 1) * 2; tot++) {
curr = new int[n][];
for (int i = 0; i < n; i++) {
curr[i] = next[i].clone();
}
for (int i = 0; i < n; i++) {
Arrays.fill(next[i], Integer.MIN_VALUE);
}
for (int i = 0; i < n && i <= tot; i++) {
int ci = tot - i;
if (ci < n) {
for (int j = 0; j < n && j <= tot; j++) {
int cj = tot - j;
if (cj < n) {
if (i + 1 < n && j + 1 < n) {
next[i + 1][j + 1] = Math.max(next[i + 1][j + 1],
curr[i][j] + addedValue(i + 1, ci, j + 1, cj));
}
if (i + 1 < n && cj + 1 < n) {
next[i + 1][j] = Math.max(next[i + 1][j],
curr[i][j] + addedValue(i + 1, ci, j, cj + 1));
}
if (ci + 1 < n && j + 1 < n) {
next[i][j + 1] = Math.max(next[i][j + 1],
curr[i][j] + addedValue(i, ci + 1, j + 1, cj));
}
if (ci + 1 < n && cj + 1 < n) {
next[i][j] = Math.max(next[i][j],
curr[i][j] + addedValue(i, ci + 1, j, cj + 1));
}
}
}
}
}
}
System.out.println(next[n - 1][n - 1]);
}
private static int addedValue(int i, int ci, int j, int cj) {
int z = a[i][ci];
if (i != j || ci != cj) {
z += a[j][cj];
}
return z;
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > v, dp;
int main() {
scanf("%d", &n);
v.resize(n + 1, vector<int>(n + 1, 0));
dp.resize(n + 1, vector<int>(n + 1, -1000000007));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) scanf("%d", &v[i][j]);
}
dp[1][1] = v[1][1];
for (int x = 3; x <= 2 * n; ++x) {
for (int i = min(x - 1, n); x - i <= n && i > 0; --i) {
for (int j = min(x - 1, n); j >= i && x - j <= n; --j) {
int c = v[x - i][i], d = v[x - j][j];
if (i == j)
dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1], dp[i - 1][j])) + c;
else
dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1],
max(dp[i][j - 1], dp[i - 1][j]))) +
c + d;
}
}
}
printf("%d", dp[n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class RelayRace {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int N = sc.nextInt();
int[][] a = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i][j] = sc.nextInt();
}
}
int diagLen = 2 * N - 1;
int[][] prev = new int[1][1];
prev[0][0] = a[0][0];
for (int d = 1; d < diagLen; d++) {
int len = (d < N) ? d : (diagLen - d - 1);
int[][] next = new int[len + 1][len + 1];
for (int x = 0; x <= len; x++) {
Location pX = diagToRect(d, x, N);
int x0 = (d >= N) ? x : (x - 1);
for (int y = x; y <= len; y++) {
int y0 = (d >= N) ? y : (y - 1);
Location pY = diagToRect(d, y, N);
if (x == y) {
int poss1 = getDP(prev, d - 1, x0, y0, N);
int poss2 = getDP(prev, d - 1, x0, y0 + 1, N);
int poss3 = getDP(prev, d - 1, x0 + 1, y0 + 1, N);
int best = Math.max(poss1, Math.max(poss2, poss3));
next[x][y] = best + a[pX.R][pX.C];
} else {
int poss1 = getDP(prev, d - 1, x0, y0, N);
int poss2 = getDP(prev, d - 1, x0, y0 + 1, N);
int poss3 = getDP(prev, d - 1, x0 + 1, y0, N);
int poss4 = getDP(prev, d - 1, x0 + 1, y0 + 1, N);
int best = Math.max(poss1, Math.max(poss2, Math.max(poss3, poss4)));
next[x][y] = best + a[pX.R][pX.C]+ a[pY.R][pY.C];
}
}
}
prev = next;
}
System.out.println(prev[0][0]);
}
public static int getDP(int[][][] dp, int d, int x, int y, int N) {
int diagLen = 2 * N - 1;
int len = (d < N) ? d : (diagLen - d - 1);
if (x < 0 || x > len || y < 0 || y > len) {
return -1000000;
} else {
return dp[d][x][y];
}
}
public static int getDP(int[][] dp, int d, int x, int y, int N) {
int diagLen = 2 * N - 1;
int len = (d < N) ? d : (diagLen - d - 1);
if (x < 0 || x > len || y < 0 || y > len) {
return -1000000;
} else {
return dp[x][y];
}
}
public static Location diagToRect(int d, int x, int N) {
int r0 = (d < N) ? d : (N - 1);
int c0 = (d < N) ? 0 : (d - N + 1);
int r = r0 - x;
int c = c0 + x;
return new Location(r, c);
}
public static boolean valid(Location p, int N) {
return (p.C >= 0 && p.C < N && p.R >= 0 && p.R < N);
}
public static class Location {
public int R, C;
public Location(int r, int c) {
this.R = r;
this.C = c;
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, dp[301][301][301], vis[301][301][301], a[301][301];
int valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n); }
int solve(int x1, int y1, int x2) {
int y2 = x1 + y1 - x2;
if (!valid(x1, y1) || !valid(x2, y2)) return -10000000;
if (x1 == n && y1 == n && x2 == n) return a[n][n];
if (vis[x1][y1][x2]) return dp[x1][y1][x2];
int ans = -10000000;
ans = max(ans, solve(x1 + 1, y1, x2));
ans = max(ans, solve(x1 + 1, y1, x2 + 1));
ans = max(ans, solve(x1, y1 + 1, x2));
ans = max(ans, solve(x1, y1 + 1, x2 + 1));
ans += a[x1][y1] + a[x2][y2];
if (x1 == x2 && y1 == y2) ans = ans - a[x1][y1];
vis[x1][y1][x2] = 1;
dp[x1][y1][x2] = ans;
return ans;
}
int main() {
int i, j;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) cin >> a[i][j];
}
cout << solve(1, 1, 1);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.util.*;
public class Relay {
static int[][][] memo;
static int[][] g;
static int N;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int n = reader.nextInt(); N = n;
g = new int[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
g[i][j] = reader.nextInt();
// memo = new int[2*n+1][n+1][n+1];
// for(int[][] me:memo)for(int[] m:me)Arrays.fill(m,-1);
int[][] nmem,pmem;
nmem = new int[n][n];
pmem = new int[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
pmem[i][j] = -(int)1e9;
pmem[n-1][n-1] = g[n-1][n-1];
//go-go space save!
for(int i = 2*n-3; i >= 0; i--){
for(int j = 0; j < n; j++){
for(int k = 0; k < n; k++){
int x1 = i-j;
int x2 = i-k;
if(x1 >= N || x1 < 0 || x2 >= N || x2 < 0)
continue;
int v = j==k?g[x1][j]:(g[x1][j] + g[x2][k]);
int max = -(int)1e9;
for(int x = 0; x < 2; x++){
int nx1 = x1 + dx[x];
int ny1 = j + dy[x];
if(nx1 == N || ny1 == N)
continue;
for(int y = 0; y < 2; y++){
int nx2 = x2 + dx[y];
int ny2 = k + dy[y];
if(nx2 == N || ny2 == N)
continue;
max = Math.max(max, pmem[ny1][ny2]);
}
}
nmem[j][k] = v + max;
}
}
int[][] t = nmem;
nmem = pmem;
pmem = t;
}
// System.out.println(f(0,0,0));
System.out.println(pmem[0][0]);
}
static int[] dx = new int[]{1,0};
static int[] dy = new int[]{0,1};
public static int f(int n, int m, int k){
if(memo[n][m][k] == -1){
if(n == 2*N-2){
memo[n][m][k] = g[N-1][N-1];
}else{
int x1 = n-m;
int x2 = n-k;
int v = m==k?g[x1][m]:(g[x1][m] + g[x2][k]);
int max = -(int)1e9;
for(int i = 0; i < 2; i++){
int nx1 = x1 + dx[i];
int ny1 = m + dy[i];
if(nx1 == N || ny1 == N)
continue;
for(int j = 0; j < 2; j++){
int nx2 = x2 + dx[j];
int ny2 = k + dy[j];
if(nx2 == N || ny2 == N)
continue;
max = Math.max(max, f(n+1,ny1,ny2));
}
}
memo[n][m][k] = v + max;
}
}
return memo[n][m][k];
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 305;
int n, a[N][N], dp[N + N][N][N];
int rec(int x1, int y1, int x2, int y2) {
if (x1 > n || y1 > n || x2 > n || y2 > n) return -(int)1e9;
if (x1 == x2 && y1 == y2 && x1 == n && y1 == n) return a[n][n];
if (dp[x1 + y1][x1][x2] != -(int)1e9) return dp[x1 + y1][x1][x2];
int z = -(int)1e9, w = 0;
z = max(z, rec(x1 + 1, y1, x2 + 1, y2));
z = max(z, rec(x1 + 1, y1, x2, y2 + 1));
z = max(z, rec(x1, y1 + 1, x2 + 1, y2));
z = max(z, rec(x1, y1 + 1, x2, y2 + 1));
w = a[x1][y1];
if (x1 != x2 || y1 != y2) w += a[x2][y2];
return dp[x1 + y1][x1][x2] = w + z;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) cin >> a[i][j];
for (int i = 1; i <= n + n; ++i)
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= n; ++k) dp[i][j][k] = -(int)1e9;
cout << rec(1, 1, 1, 1);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e14;
const double eps = 1e-10;
const double pi = acos(-1.0);
int a[305][610];
int dp[305][305][610];
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j + i]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= n + n; k++) {
dp[i][j][k] = -1e9;
}
}
}
dp[1][1][2] = a[1][2];
for (int d = 2; d <= n + n - 1; d++) {
for (int i = 1; i <= min(d + 1, n); i++) {
for (int j = 1; j <= min(d + 1, n); j++) {
int tx = i + 1;
int ty = j;
int& res = dp[tx][ty][d + 1];
if (tx == ty)
res = max(res, dp[i][j][d] + a[tx][d + 1]);
else
res = max(res, dp[i][j][d] + a[tx][d + 1] + a[ty][d + 1]);
tx = i + 1;
ty = j + 1;
int& res1 = dp[tx][ty][d + 1];
if (tx == ty)
res1 = max(res1, dp[i][j][d] + a[tx][d + 1]);
else
res1 = max(res1, dp[i][j][d] + a[tx][d + 1] + a[ty][d + 1]);
tx = i;
ty = j;
int& res2 = dp[tx][ty][d + 1];
if (tx == ty)
res2 = max(res2, dp[i][j][d] + a[tx][d + 1]);
else
res2 = max(res2, dp[i][j][d] + a[tx][d + 1] + a[ty][d + 1]);
tx = i;
ty = j + 1;
int& res3 = dp[tx][ty][d + 1];
if (tx == ty)
res3 = max(res3, dp[i][j][d] + a[tx][d + 1]);
else
res3 = max(res3, dp[i][j][d] + a[tx][d + 1] + a[ty][d + 1]);
}
}
}
cout << dp[n][n][n + n] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
template <typename T>
inline T Abs(T x) {
return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 610;
int n, a[MAXN][MAXN], d[MAXN][MAXN], d1[MAXN][MAXN];
void update(int &x, int y) { x = max(x, y); }
bool valid(int x, int y) { return (0 <= x && x < n) && (0 <= y && y < n); }
pair<int, int> f1(int x, int y) { return make_pair(y, x - y); }
int main() {
cin >> n;
for (int i = 0; i < (int)(n); i++)
for (int j = 0; j < (int)(n); j++) cin >> a[i][j];
memset(d, 225, sizeof d);
d[0][0] = a[0][0];
for (int i = 0; i < (int)(2 * n - 2); i++) {
memset(d1, 225, sizeof d);
int l = +INF, r = -INF;
for (int j = 0; j < (int)(n); j++) {
pair<int, int> cur = f1(i, j);
if (valid(cur.first, cur.second)) {
l = min(l, j);
r = max(r, j);
}
}
for (int j = l; j <= r; j++) {
pair<int, int> v = f1(i, j);
for (int k = l; k <= r; k++) {
pair<int, int> u = f1(i, k);
for (int cj = 0; cj < (int)(2); cj++)
for (int ck = 0; ck < (int)(2); ck++) {
int cur = d[j][k];
pair<int, int> nv = make_pair(v.first + cj, v.second + 1 - cj);
pair<int, int> nu = make_pair(u.first + ck, u.second + 1 - ck);
if (valid(nv.first, nv.second) && valid(nu.first, nu.second)) {
cur += a[nv.first][nv.second];
if (nv != nu) cur += a[nu.first][nu.second];
update(d1[nv.first][nu.first], cur);
}
}
}
}
memcpy(d, d1, sizeof d);
}
cout << d[n - 1][n - 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[2][310][310], tab[310][310];
int n, k;
int main() {
int i, j, tem, mm;
int be, fo;
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) scanf("%d", &tab[i][j]);
memset(dp, -16, sizeof(dp));
dp[0][0][0] = tab[1][1];
be = 0;
for (k = 1; k <= 2 * n - 2; k++) {
fo = be ^ 1;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
mm = dp[be][i][j];
if (i > 0) mm = max(mm, dp[be][i - 1][j]);
if (j > 0) mm = max(mm, dp[be][i][j - 1]);
if (i > 0 && j > 0) mm = max(mm, dp[be][i - 1][j - 1]);
if (i == j)
dp[fo][i][j] = mm + tab[1 + i][1 + k - i];
else
dp[fo][i][j] = mm + tab[1 + i][1 + k - i] + tab[1 + j][1 + k - j];
}
be ^= 1;
}
printf("%d\n", dp[0][n - 1][n - 1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 600; i++)
for (int j = 0; j <= 600; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = k % 2, y = 1 - x;
if (i >= k || j >= k) {
f[x][i][j] = -inf;
continue;
}
if (k - i > n || k - j > n) {
f[x][i][j] = -inf;
continue;
}
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e14;
const double eps = 1e-10;
const double pi = acos(-1.0);
long long a[750][750];
long long dp[302][302][3];
int n;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
a[i][j + i] = x;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= 2; k++) {
dp[i][j][k] = -inf;
}
}
}
dp[1][1][2 & 1] = a[1][2];
for (int d = 2; d <= n + n - 1; d++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int tx = i + x;
int ty = j + y;
long long& res = dp[tx][ty][(d + 1) & 1];
if (tx == ty) {
res = max(res, dp[i][j][d & 1] + a[tx][d + 1]);
} else {
res = max(res, dp[i][j][d & 1] + a[tx][d + 1] + a[ty][d + 1]);
}
}
}
dp[i][j][d & 1] = -inf;
}
}
}
cout << dp[n][n][(n + n) & 1] << "\n";
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double PI = 3.1415926535897932384626433832795;
const long double E = 2.7182818284;
const int INF = 1000000000;
int main(void) {
int n, i, j, g;
cin >> n;
int a[n][n];
for (int i = 0; i < int(n); i++)
for (int j = 0; j < int(n); j++) cin >> a[i][j];
int dp[2][n][n];
for (int i = 0; i < int(2); i++)
for (int j = 0; j < int(n); j++)
for (int g = 0; g < int(n); g++) dp[i][j][g] = -INF;
dp[0][0][0] = a[0][0];
for (i = 1; i < 2 * n - 1; i++) {
for (int j = 0; j < int(min(n, i + 1)); j++) {
for (int g = 0; g < int(min(n, i + 1)); g++) {
int ind1 = i & 1, ind2 = (ind1 + 1) & 1;
if (i - g >= n || i - j >= n) continue;
int k = a[i - g][g] + a[i - j][j];
if (j == g) k -= a[i - g][g];
dp[ind1][j][g] = dp[ind2][j][g] + k;
if (j) {
dp[ind1][j][g] = max(dp[ind1][j][g], dp[ind2][j - 1][g] + k);
}
if (g) {
dp[ind1][j][g] = max(dp[ind1][j][g], dp[ind2][j][g - 1] + k);
}
if (j && g)
dp[ind1][j][g] = max(dp[ind1][j][g], dp[ind2][j - 1][g - 1] + k);
}
}
}
cout << dp[0][n - 1][n - 1];
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) {
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = k % 2, y = 1 - x;
if (k - i > n || k - j > n || k - i < 1 || k - j < 1) {
f[x][i][j] = -inf;
continue;
}
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, Mat[400][400];
int f[610][310][310];
void Init() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) scanf("%d", &Mat[i][j]);
}
void Work() {
for (int i = 0; i <= 2 * N; i++)
for (int j = 0; j <= N; j++)
for (int k = 0; k <= N; k++) f[i][j][k] = -2000000000;
f[0][0][0] = 0;
for (int i = 0; i < 2 * (N - 1); i++)
for (int j = 0; j <= i && j < N; j++)
for (int k = 0; k <= i && k < N; k++)
if (f[i][j][k] >= -200000000) {
int j1 = j + 1, j2 = 1 + i - j, k1 = k + 1, k2 = 1 + i - k,
tmp = Mat[j1][j2] + Mat[k1][k2];
if (j1 == k1 && j2 == k2) tmp /= 2;
if (j1 + 1 <= N && k1 + 1 <= N)
f[i + 1][j + 1][k + 1] =
max(f[i + 1][j + 1][k + 1], f[i][j][k] + tmp);
if (j1 + 1 <= N && k2 + 1 <= N)
f[i + 1][j + 1][k] = max(f[i + 1][j + 1][k], f[i][j][k] + tmp);
if (j2 + 1 <= N && k1 + 1 <= N)
f[i + 1][j][k + 1] = max(f[i + 1][j][k + 1], f[i][j][k] + tmp);
if (j2 + 1 <= N && k2 + 1 <= N)
f[i + 1][j][k] = max(f[i + 1][j][k], f[i][j][k] + tmp);
}
printf("%d\n", f[2 * (N - 1)][N - 1][N - 1] + Mat[N][N]);
}
int main() {
Init();
Work();
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int n;
int d[300][300];
int dp[300][300][300];
bool was[300][300][300];
int dx[] = {1, 0};
int dy[] = {0, 1};
inline bool valid(int i, int j, int k) {
int l = i + j - k;
if (i >= 0 && i < n && j >= 0 && j < n) {
if (k >= 0 && k < n && l >= 0 && l < n) {
return true;
}
}
return false;
}
int solve(int i, int j, int k) {
if (i + j == 2 * (n - 1)) {
return d[n - 1][n - 1];
}
if (was[i][j][k]) {
return dp[i][j][k];
}
was[i][j][k] = true;
dp[i][j][k] = -10000000;
for (int dir1 = 0; dir1 < 2; ++dir1) {
for (int dir2 = 0; dir2 < 2; ++dir2) {
if (valid(i + dx[dir1], j + dy[dir1], k + dx[dir2])) {
if (i == k && j == i + j - k) {
dp[i][j][k] =
max(dp[i][j][k],
d[i][j] + solve(i + dx[dir1], j + dy[dir1], k + dx[dir2]));
} else {
dp[i][j][k] = max(
dp[i][j][k], d[i][j] + d[k][i + j - k] +
solve(i + dx[dir1], j + dy[dir1], k + dx[dir2]));
}
}
}
}
return dp[i][j][k];
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", d[i] + j);
}
}
cout << solve(0, 0, 0) << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int arr[302][302], n, memory[302][302][302];
int MX(int a, int b) { return (a > b) ? a : b; }
int dp(int r1, int c1, int r2) {
if (r1 == n - 1 && c1 == n - 1) {
return arr[r1][c1];
}
if (memory[r1][c1][r2] != -1) {
return memory[r1][c1][r2];
}
int c2, mx = -99999999;
if (r2 >= r1) {
c2 = c1 - (r2 - r1);
} else {
c2 = c1 + (r1 - r2);
}
if (r1 + 1 < n && r2 + 1 < n) {
mx = dp(r1 + 1, c1, r2 + 1);
}
if (r1 + 1 < n && c2 + 1 < n) {
mx = MX(mx, dp(r1 + 1, c1, r2));
}
if (c1 + 1 < n && r2 + 1 < n) {
mx = MX(mx, dp(r1, c1 + 1, r2 + 1));
}
if (c1 + 1 < n && c2 + 1 < n) {
mx = MX(mx, dp(r1, c1 + 1, r2));
}
if (r1 != r2 || c1 != c2) {
mx += arr[r1][c1] + arr[r2][c2];
} else {
mx += arr[r1][c1];
}
return memory[r1][c1][r2] = mx;
}
int main() {
int i, j, k, ans;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
memory[i][j][k] = -1;
}
}
}
ans = dp(0, 0, 0);
printf("%d\n", ans);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.util.*;
import java.io.*;
public class e
{
public static void main(String[] arg)
{
new e();
}
int[][][] memo;
int[][] g;
int n;
int oo = -123_456_789;
public e()
{
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = in.nextInt();
g = new int[n][n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
g[i][j] = in.nextInt();
}
}
memo = new int[n][n][n];
for(int[][] a : memo) for(int[] b : a) Arrays.fill(b, oo);
int total = solve(0, 0, 0);
System.out.println(total);
in.close(); out.close();
}
int solve(int r, int c, int r2)
{
int c2 = r+c-r2;
if(r == n-1 && c == n-1) return g[r][c];
int ret = memo[r][c][r2];
if(ret != oo) return ret;
ret = Integer.MIN_VALUE;
int cost = g[r][c];
if(r2 != r || c2 != c) cost += g[r2][c2];
if(r+1 < n)
{
if(r2+1 < n)
{
ret = Math.max(ret, solve(r+1, c, r2+1)+cost);
}
if(c2+1 < n)
{
ret = Math.max(ret, solve(r+1, c, r2)+cost);
}
}
if(c+1 < n)
{
if(r2+1 < n)
{
ret = Math.max(ret, solve(r, c+1, r2+1)+cost);
}
if(c2+1 < n)
{
ret = Math.max(ret, solve(r, c+1, r2)+cost);
}
}
return memo[r][c][r2] = ret;
}
}
/*
3
9 9 9
-10 -10 -20
-10 -10 0
4
5 3 3 0
5 5 5 0
5 0 5 5
5 5 4 5
*/
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int ara[303][303], mem[303][303][303], n;
bool vis[303][303][303];
int dx[2] = {0, 1};
int dy[2] = {1, 0};
int dp(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2 && x1 == y1 && x1 == n - 1) return ara[n - 1][n - 1];
if (x2 > x1 || y2 < y1 || max({x1, x2, y1, y2}) > n - 1) return -1000000009;
if (vis[x1][y1][x2]) return mem[x1][y1][x2];
vis[x1][y1][x2] = 1;
mem[x1][y1][x2] = -1000000009;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
mem[x1][y1][x2] = max(mem[x1][y1][x2],
dp(x1 + dx[i], y1 + dy[i], x2 + dx[j], y2 + dy[j]));
return mem[x1][y1][x2] +=
ara[x1][y1] + ara[x2][y2] - (x1 == x2 && y1 == y2) * ara[x2][y2];
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &ara[i][j]);
printf("%d\n", dp(0, 0, 0, 0));
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
const int inf = 1000 * 1000 * 1000;
int dp[599][300][300], a[300][300],
shift[4][2] = {{0, 0}, {-1, 0}, {0, -1}, {-1, -1}};
int main() {
int n;
scanf("%d", &n);
int maxt = 2 * n - 1;
for (int i = 0; i < maxt; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k) dp[i][j][k] = -inf;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) scanf("%d", &a[i][j]);
dp[0][0][0] = a[0][0];
for (int t = 1; t < maxt; ++t)
for (int x1 = 0; x1 < n; ++x1)
for (int x2 = 0; x2 < n; ++x2)
for (int i = 0; i < 4; ++i) {
int nx1 = x1 + shift[i][0], nx2 = x2 + shift[i][1], y1 = t - x1,
y2 = t - x2;
if (nx1 >= 0 && nx1 < n && nx2 >= 0 && nx2 < n && y1 >= 0 && y1 < n &&
y2 >= 0 && y2 < n) {
int cur = dp[t - 1][nx1][nx2] + a[x1][y1] + a[x2][y2];
if (x1 == x2) cur -= a[x1][y1];
if (cur > dp[t][x1][x2]) dp[t][x1][x2] = cur;
}
}
printf("%d\n", dp[maxt - 1][n - 1][n - 1]);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N;
int A[300][300];
int M[600][300][300];
int dx[] = {1, 0}, dy[] = {0, 1};
int main() {
cin >> N;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) cin >> A[i][j];
memset(M, 128, sizeof(M));
M[0][0][0] = A[0][0];
for (int i = 0; i < 2 * N - 2; i++) {
for (int j = 0; j < N && j <= i; j++) {
for (int k = 0; k < N && k <= i; k++) {
for (int dj = 0; dj < 2; dj++) {
int njx = j + dx[dj], njy = i - j + dy[dj];
if (njx == N || njy == N) continue;
for (int dk = 0; dk < 2; dk++) {
int nkx = k + dx[dk], nky = i - k + dy[dk];
if (nkx == N || nky == N) continue;
int total = A[njx][njy] + A[nkx][nky];
if (njx == nkx) total /= 2;
M[i + 1][njx][nkx] = max(M[i + 1][njx][nkx], M[i][j][k] + total);
}
}
}
}
}
cout << M[2 * N - 2][N - 1][N - 1] << "\n";
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int infl = 1e8 + 5;
int n, m, k, q, x, y, f, val, t = 1, i, j;
int ind = -1, cnt, sz, sm, ans, mx = -1, mn = infl;
int a[304][304], dp[304 + 304][304][304];
int rec(int d, int x1, int x2) {
if (d > n + n - 2) return 0;
if (x1 >= n || x2 >= n) return -infl;
int y1 = d - x1, y2 = d - x2, re = 0;
if (y1 >= n || y2 >= n) return -infl;
if (dp[d][x1][x2] != -1) return dp[d][x1][x2];
re = max({rec(d + 1, x1, x2 + 1), rec(d + 1, x1 + 1, x2 + 1),
rec(d + 1, x1, x2), rec(d + 1, x1 + 1, x2)});
if (x1 == x2 && y1 == y2)
re += a[x1][y1];
else
re += a[x1][y1] + a[x2][y2];
dp[d][x1][x2] = re;
return re;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("inp.txt", "r")) {
freopen("myfile.txt", "w", stdout);
freopen("inp.txt", "r", stdin);
}
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> a[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << rec(0, 0, 0);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.util.*;
import static java.lang.System.*;
public class E214 {
Scanner sc = new Scanner(in);
public void run() {
int n=sc.nextInt();
int[][] map=new int[n][n];
for(int y=0;y<n;y++)for(int x=0;x<n;x++)
map[x][y]=sc.nextInt();
int[][] dp=new int[n][n];
dp[0][0]=map[0][0];
for(int i=1;i<2*n-1;i++){
int[][] next=new int[n][n];
int am=i>n-1?i-(n-1):0;
for(int a=am;a<n && a<=i;a++)for(int b=a;b<n && b<=i;b++){
next[a][b]=Integer.MIN_VALUE;
if(a!=0 && dp[a-1][b-1]>next[a][b])next[a][b]=dp[a-1][b-1];
if(b!=i && dp[a][b]>next[a][b])next[a][b]=dp[a][b];
if(a!=0 && b!=i && dp[a-1][b]>next[a][b])next[a][b]=dp[a-1][b];
if(a!=b && dp[a][b-1]>next[a][b])next[a][b]=dp[a][b-1];
next[a][b]+=map[a][i-a]+(a!=b?map[b][i-b]:0);
}
dp=next;
}
ln(dp[n-1][n-1]);
}
public static void main(String[] _) {
new E214().run();
}
public static void pr(Object o) {
out.print(o);
}
public static void ln(Object o) {
out.println(o);
}
public static void ln() {
out.println();
}
} | JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a[305][305], dp[610][305][305];
int n;
void chu() {
for (int i = 1; i <= n + n - 1; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) dp[i][j][k] = -1000000;
}
int main() {
while (scanf("%d", &n) != -1) {
chu();
for (int i = 0; i <= n; i++) a[0][i] = -10000;
for (int i = 1; i <= n; i++) {
a[i][0] = -10000;
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
}
if (n == 1) {
printf("%d\n", a[1][1]);
continue;
}
int i, j, k;
dp[1][1][1] = a[1][1];
for (i = 2; i <= n + n - 1; i++) {
for (j = 1; j <= n; j++) {
if ((i - j + 1 >= 1) && (i - j + 1 <= n))
for (k = 1; k <= n; k++) {
if ((i - k + 1 >= 1) && (i - k + 1 <= n)) {
if (j != k)
dp[i][j][k] =
max(max(dp[i - 1][j][k], dp[i - 1][j - 1][k]),
max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1])) +
a[j][i - j + 1] + a[k][i - k + 1];
else
dp[i][j][k] =
max(max(dp[i - 1][j][k], dp[i - 1][j - 1][k]),
max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1])) +
a[j][i - j + 1];
}
}
}
}
printf("%d\n", dp[n + n - 1][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, C[300][300];
int A[2][300][300], now = 0, ans = 0;
int get(int now, int i1, int i2) {
if (i1 < 0 || i2 < 0) return -(int)HUGE_VAL;
return A[now][i1][i2];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) cin >> C[i][j];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) A[now][i][j] = -(int)HUGE_VAL;
A[now][0][0] = C[0][0];
for (int s = 1; s <= 2 * n - 2; ++s) {
now = !now;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) A[now][i][j] = -(int)HUGE_VAL;
for (int i1 = max(0, s - n + 1); i1 <= min(s, n - 1); ++i1)
for (int i2 = max(0, s - n + 1); i2 <= min(s, n - 1); ++i2) {
A[now][i1][i2] =
max(max(get(!now, i1, i2), get(!now, i1 - 1, i2)),
max(get(!now, i1, i2 - 1), get(!now, i1 - 1, i2 - 1)));
if (i1 == i2)
A[now][i1][i2] += C[i1][s - i1];
else
A[now][i1][i2] += C[i1][s - i1] + C[i2][s - i2];
}
}
cout << A[now][n - 1][n - 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int N = 3e2 + 5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int dr4[] = {1, -1, 0, 0};
const int dc4[] = {0, 0, 1, -1};
const int dr8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n;
int a[N][N];
int dp[N][N][N];
int f(int r1, int r2, int diag) {
if (diag >= n * 2) return 0;
int c1 = diag + 1 - r1;
int c2 = diag + 1 - r2;
if (r1 > n || c1 > n || r2 > n || c2 > n) return -INF;
int &sol = dp[r1][r2][diag];
if (sol != -1) return sol;
int val = a[r1][c1] + (r1 == r2 ? 0 : a[r2][c2]);
return sol =
val + max({f(r1, r2, diag + 1), f(r1 + 1, r2, diag + 1),
f(r1, r2 + 1, diag + 1), f(r1 + 1, r2 + 1, diag + 1)});
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << f(1, 1, 1) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
for (int tc = 1; tc <= T; tc++) {
solve();
}
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, result = 0, f[605][305][305], a[1000][1000];
void readdata() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
}
void solve() {
for (int d = 0; d <= 2 * n + 1; d++) {
for (int c1 = 0; c1 <= d; c1++) {
for (int c2 = 0; c2 <= d; c2++) {
f[d][c1][c2] = -1e9;
}
}
}
f[1][0][0] = 0;
for (int d = 2; d <= 2 * n; d++) {
for (int c1 = 1; c1 <= d - 1; c1++) {
int x1 = c1;
int y1 = d - c1;
if ((x1 <= n) && (y1 <= n)) {
for (int c2 = 1; c2 <= d - 1; c2++) {
int x2 = c2;
int y2 = d - c2;
if ((x2 <= n) && (y2 <= n)) {
f[d][c1][c2] =
max(max(max(f[d - 1][c1][c2 - 1], f[d - 1][c1 - 1][c2]),
f[d - 1][c1][c2]),
f[d - 1][c1 - 1][c2 - 1]);
f[d][c1][c2] += a[x1][y1];
if ((x1 != x2) || (y1 != y2)) {
f[d][c1][c2] += a[x2][y2];
}
}
}
}
}
}
cout << f[2 * n][n][n];
}
int main() {
readdata();
solve();
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 310; i++)
for (int j = 0; j <= 310; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n && i <= k; i++) {
for (int j = 1; j <= n && j <= k; j++) {
int x = k % 2, y = 1 - x;
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
f[x][i][j] += a[i][k - i];
if (i != j) f[x][i][j] += a[j][k - j];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[1005][1005];
int dp[605][303][303];
int calc(int t, int c, int cc) {
if (t == 2 * (n - 1)) {
if (c == n && cc == n)
return a[n][n];
else
return -(1e8);
}
if (dp[t][c][cc] != -1) return dp[t][c][cc];
int r = 2 + t - c;
int rr = 2 + t - cc;
int ans = -(1e9);
int x = 0;
if (c == cc)
x = a[c][r];
else
x = a[c][r] + a[cc][rr];
if (r < n && cc < n) ans = max(ans, x + calc(t + 1, c, cc + 1));
if (rr < n && c < n) ans = max(ans, x + calc(t + 1, c + 1, cc));
if (c < n && cc < n) ans = max(ans, x + calc(t + 1, c + 1, cc + 1));
if (r < n && rr < n) ans = max(ans, x + calc(t + 1, c, cc));
return dp[t][c][cc] = ans;
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
printf("%d\n", calc(0, 1, 1));
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool valid(long long i, long long j, long long n, long long m) {
if (i >= 0 && i < n && j >= 0 && j < m) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
vector<vector<long long> > a(n, vector<long long>(n, 0));
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) cin >> a[i][j];
vector<vector<long long> > curr(n, vector<long long>(n, -9999999999999999ll));
vector<vector<long long> > prev(n, vector<long long>(n, -9999999999999999ll));
curr[0][0] = a[0][0];
for (long long len = 0; len <= 2 * n - 2; len++) {
for (long long i = 0; i <= len; i++) {
for (long long j = 0; j <= len; j++) {
long long x1 = i;
long long y1 = len - x1;
long long x2 = j;
long long y2 = len - x2;
if (!valid(x1, y1, n, n) || !valid(x2, y2, n, n)) continue;
string dir = "UL";
for (long long i1 = 0; i1 < 2; i1++) {
for (long long j1 = 0; j1 < 2; j1++) {
long long px1 = x1, px2 = x2, py1 = y1, py2 = y2;
if (dir[i1] == 'U') {
px1--;
} else {
py1--;
}
if (dir[j1] == 'U')
px2--;
else
py2--;
if (valid(px1, py1, n, n) && valid(px2, py2, n, n)) {
if (x1 == x2) {
curr[i][j] = max(curr[i][j], prev[px1][px2] + a[x1][y1]);
} else {
curr[i][j] =
max(curr[i][j], prev[px1][px2] + a[x1][y1] + a[x2][y2]);
}
}
}
}
}
}
prev = curr;
curr = vector<vector<long long> >(
n, vector<long long>(n, -9999999999999999ll));
}
cout << prev[n - 1][n - 1] << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int maxint = 999999999;
int dp[2][maxn][maxn];
int a[maxn][maxn];
int n;
bool judge(int x, int y) { return (x >= 1 && y <= n); }
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
dp[1][1][1] = a[1][1];
for (int i = 2; i <= 2 * n - 1; i++) {
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) dp[i % 2][j][k] = -maxint;
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k;
if (judge(x1, y1) == false || judge(x2, y2) == false) continue;
int value;
if (j == k)
value = a[x1][y1];
else
value = a[x1][y1] + a[x2][y2];
if (j - 1 >= 1 && k - 1 >= 1 &&
dp[(i - 1) % 2][j - 1][k - 1] != -maxint) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value);
}
if (j - 1 >= 1 && x2 - 1 >= 1 &&
dp[(i - 1) % 2][j - 1][k] != -maxint) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value);
}
if (x1 - 1 >= 1 && k - 1 >= 1 &&
dp[(i - 1) % 2][j][k - 1] != -maxint) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value);
}
if (x1 - 1 >= 1 && x2 - 1 >= 1 && dp[(i - 1) % 2][j][k] != -maxint) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value);
}
}
}
}
printf("%d\n", dp[(2 * n - 1) % 2][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int m[300][300], dp[2 * 300][300][300];
int main() {
int n, i, j, k, val;
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cin >> m[i][j];
}
}
dp[0][0][0] = m[0][0];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = max(i, j); k < min(i, j) + n; k++) {
if (i || j || k) dp[k][i][j] = -0x3f3f3f3f;
val = m[k - i][i];
if (i != j) val += m[k - j][j];
if (i) {
if (j) dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i - 1][j - 1] + val);
if (k - j) dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i - 1][j] + val);
}
if (k - i) {
if (j) dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i][j - 1] + val);
if (k - j) dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i][j] + val);
}
}
}
}
cout << dp[(2 * n) - 2][n - 1][n - 1] << "\n";
return (0);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number β the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 310; i++)
for (int j = 0; j <= 310; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n && i <= k; i++) {
for (int j = 1; j <= n && j <= k; j++) {
int x = k % 2, y = 1 - x;
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.