-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1986D Mathematical Problem.cpp
67 lines (62 loc) · 1.7 KB
/
1986D Mathematical Problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Author : MishkatIT
Created : Wednesday 26-06-2024 18:14:08
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
string str;
cin >> str;
if (n == 2) {
cout << stoi(str) << '\n';
} else if (n == 3) {
int a = str[0] - '0', b = str[1] - '0', c = str[2] - '0';
int ans = inf;
ans = min(ans, a * (b * 10 + c));
ans = min(ans, a + (b * 10 + c));
ans = min(ans, (a * 10 + b) * c);
ans = min(ans, (a * 10 + b) + c);
cout << ans << '\n';
} else {
if (find(str.begin(), str.end(), '0') != str.end()) {
cout << 0 << '\n';
} else {
int sum = 0;
for (auto& i : str) {
if (i != '1') sum += i - '0';
}
int ans = inf;
for (int i = 0; i + 1 < n; i++) {
int a = str[i] - '0', b = str[i + 1] - '0';
if (a != 1) sum -= a;
if (b != 1) sum -= b;
sum += (a * 10 + b);
ans = min(ans, sum);
sum -= (a * 10 + b);
if (a != 1) sum += a;
if (b != 1) sum += b;
}
cout << ans << '\n';
}
}
}
return 0;
}