text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
char s[110][10], f[10];
int main() {
int i;
cin >> f >> n;
for (i = 1; i <= n; i++) {
cin >> s[i];
if (s[i][0] == f[0] && s[i][1] == f[1]) a = b = 1;
if (s[i][1] == f[0]) a = 1;
if (s[i][0] == f[1]) b = 1;
}
if (a && b)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long int freq[10000001];
int main() {
string s1;
cin >> s1;
unsigned long long int n;
cin >> n;
unsigned long long int flag1, flag2;
flag1 = flag2 = 0;
unsigned long long int flag = 0;
while (n--) {
string s2;
cin >> s2;
if (s2 == s1) flag = 1;
if (s2[1] == s1[0]) flag1 = 1;
if (s2[0] == s1[1]) flag2 = 1;
}
if (flag || (flag1 && flag2))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
int n;
cin >> n;
set<char> X[2];
while (n--) {
string t;
cin >> t;
if (t == s) {
cout << "YES\n";
return 0;
}
X[0].insert(t[0]);
X[1].insert(t[1]);
}
if (X[0].count(s[1]) && X[1].count(s[0])) {
cout << "YES\n";
} else
cout << "NO\n";
}
|
#include <bits/stdc++.h>
using namespace std;
char pwd[3];
int n;
char s1[3];
int main() {
scanf("%s", pwd);
scanf("%d", &n);
bool flag, flag1, flag2 = flag1 = flag = 0;
for (int i = 1; i <= n; ++i) {
scanf("%s", s1);
if ((string)s1 == (string)pwd) flag = 1;
if (s1[1] == pwd[0]) flag1 = 1;
if (s1[0] == pwd[1]) flag2 = 1;
}
printf("%s\n", (flag || (flag1 && flag2)) ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[3];
int c[2] = {0}, d[2] = {0};
cin >> a;
int n;
cin >> n;
char** b = new char*[n + 1];
for (int i = 0; i < n; i++) {
b[i] = new char[3];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++) cin >> b[i][j];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 2; k++) {
if (b[j][k] == a[i]) {
if (i == 0) {
c[k]++;
} else {
d[k]++;
}
}
}
if (b[j][0] == a[0] && b[j][1] == a[1]) {
cout << "YES";
return 0;
}
}
}
if (c[1] && d[0])
cout << "YES";
else
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n;
cin >> n;
bool f = 0;
vector<string> h(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
if (h[i] == s) f = 1;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (h[i][1] == s[0] && h[j][0] == s[1]) f = 1;
}
}
if (f)
cout << "YES";
else
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
char s[10], a, b;
bool ma[300]{0}, mb[300]{0};
int n;
cin >> s >> n;
while (n--) {
cin >> a >> b;
if (s[0] == a && s[1] == b) {
cout << "YES";
return 0;
}
ma[a] = true;
mb[b] = true;
}
if (ma[s[1]] && mb[s[0]])
cout << "YES";
else
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
char s[5], a[105][5];
int n;
int main() {
scanf("%s", s);
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", a[i]);
for (int i = 1; i <= n; i++) {
if (a[i][0] == s[0] && s[1] == a[i][1]) return printf("YES"), 0;
for (int j = 1; j <= n; j++)
if (s[0] == a[i][1] && s[1] == a[j][0]) return printf("YES"), 0;
}
return printf("NO"), 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[105][4], x[5];
int n;
int main() {
int i, j, k;
cin >> x >> n;
for (i = 1; i <= n; i++) {
cin >> s[i];
if (s[i][0] == x[0] && s[i][1] == x[1]) break;
}
if (i <= n) {
puts("YES");
return 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
if (s[i][1] == x[0] && s[j][0] == x[1]) break;
if (j <= n) {
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, a[105];
cin >> s;
int n, p = 0, q = 0, i;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == s) {
cout << "YES" << endl;
return 0;
}
if (a[i][0] == s[1] && a[i][1] == s[0]) {
cout << "YES" << endl;
return 0;
}
if (a[i][0] == s[1]) p = 1;
}
for (i = 1; i <= n; i++) {
if (p == 1 && a[i][1] == s[0]) q = 1;
}
cout << ((p == 1 && q == 1) ? "YES" : "NO") << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<char> s[26];
vector<char> foundfirst(26, false), foundsecond(26, false);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s1;
string s2;
getline(cin, s1);
long long int t;
cin >> t;
cin.ignore();
while (t--) {
getline(cin, s2);
s[s2[0] - 'a'].insert(s2[1]);
foundfirst[s2[0] - 'a'] = true;
foundsecond[s2[1] - 'a'] = true;
}
if (foundfirst[s1[1] - 'a'] && foundsecond[s1[0] - 'a']) {
cout << "YES";
return 0;
}
if (s[s1[0] - 'a'].find(s1[1]) != s[s1[0] - 'a'].end()) {
cout << "YES";
return 0;
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long ceili(long long x, long long y) { return (x + y - 1) / y; }
void solve() {
string pass;
cin >> pass;
long long n;
cin >> n;
vector<string> word(n);
for (long long i = 0; i < n; ++i) {
cin >> word[i];
if (word[i] == pass) {
cout << "YES"
<< "\n";
return;
}
}
bool first, second;
first = second = false;
for (long long i = 0; i < n; ++i) {
if (pass[0] == word[i][1]) {
first = true;
break;
}
}
for (long long i = 0; i < n; ++i) {
if (pass[1] == word[i][0]) {
second = true;
break;
}
}
if (first && second)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n;
cin >> n;
vector<string> v;
while (n--) {
string x;
cin >> x;
if (s[0] == x[0] || s[0] == x[1] || s[1] == x[0] || s[1] == x[1])
v.push_back(x);
}
bool ok = false;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size(); j++) {
string x = v[i] + v[j];
string y = v[j] + v[i];
if (x.find(s) != string::npos || y.find(s) != string::npos) {
ok = true;
break;
}
}
}
if (ok)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test = 1;
while (test--) {
string s;
int p = 0;
cin >> s;
int t, i, j;
cin >> t;
vector<string> vec;
string s0;
for (i = 0; i < t; i++) {
cin >> s0;
vec.push_back(s0);
}
int flag = 0;
for (i = 0; i < t; i++) {
string s2 = vec[i];
if (s2 == s || (s2[0] == s[1] && s2[1] == s[0])) {
flag = 1;
break;
}
}
if (flag == 1)
cout << "YES";
else {
for (i = 0; i < t; i++) {
string a = vec[i];
for (j = 0; j < i; j++) {
string b = vec[j];
if ((s[0] == a[1] && s[1] == b[0]) ||
(s[0] == b[1] && s[1] == a[0])) {
p = 1;
break;
}
}
if (p == 1) break;
}
if (p == 1)
cout << "YES";
else
cout << "NO";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[10000];
cin >> s;
int n;
cin >> n;
string ss[10000];
for (int i = 0; i < n; i++) cin >> ss[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
string s1 = ss[i] + ss[j];
if (s1.find(s) != std::string::npos) {
std::cout << "YES" << '\n';
return 0;
}
}
}
cout << "NO\n";
}
|
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> fib;
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
template <typename T>
inline T ItoS(T n) {
stringstream a;
a << n;
return a.str();
}
template <typename T>
inline T TrailingZeros(T n) {
long long cnt = 0;
for (long long i = 5; n / i >= 1; i *= 5) cnt += n / i;
return cnt;
}
template <typename T>
inline T StoI(T s) {
long long sum = 0;
for (long long i = 0; i < s.size(); i++) {
s[i] -= '0';
sum = sum * 10 + s[i];
}
return sum;
}
template <typename T>
inline T binary_decimal(T n) {
long long decimal = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
decimal += rem * pow(2, i);
++i;
}
return decimal;
}
template <typename T>
inline T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
inline T maxi(T x, T y) {
if (x > y)
return x;
else
return y;
}
template <typename T>
inline T maxi(T x, T y, T z) {
return maxi(maxi(x, y), z);
}
template <typename T>
inline T mini(T x, T y) {
if (x < y)
return x;
else
return y;
}
template <typename T>
inline T mini(T x, T y, T z) {
return mini(mini(x, y), z);
}
template <typename T>
inline T GCD(T a, T b) {
long long t;
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
}
template <typename T>
inline T Pow(T base, T exp) {
long long result = 1;
while (exp) {
if (exp & 1) result *= base;
exp >>= 1;
base *= base;
}
return result;
}
template <typename T>
inline T Mulmod(T a, T b, T c) {
unsigned long long x = 0, y = a % c;
while (b > 0) {
if (b & 1) x = (x + y) % c;
y = (y << 1) % c;
b >>= 1;
}
return x;
}
template <typename T>
inline T Pow(T base, T exp, T mod) {
unsigned long long x = 1, y = base;
while (exp > 0) {
if (exp & 1) x = Mulmod(x, y, mod);
y = Mulmod(y, y, mod);
exp >>= 1;
}
return x;
}
template <typename T>
inline T modulo(T a, T b, T c) {
unsigned long long x = 1, y = a % c;
while (b > 0) {
if (b % 2) x = Mulmod(x, y, c);
y = Mulmod(y, y, c);
b /= 2;
}
return x % c;
}
template <typename T>
inline T BigMod(T a, T b, T m) {
if (!b) return 1;
long long x = BigMod(a, b / 2, m);
x = (x * x) % m;
if (b % 2) x = (x * a) % m;
return x;
}
template <typename T>
inline T EGCD(T a, T b, T &x, T &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
T x1, y1;
T d = EGCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
template <typename T>
inline T NumOfDivisor(T n) {
long long cnt = 0;
int square_root = (int)sqrt(n) + 1;
for (int i = 1; i < square_root; i++) {
if (n % i == 0 && i * i != n) cnt += 2;
if (n % i == 0 && i * i == n) cnt++;
}
return cnt;
}
template <typename T>
string to_string(T const &value) {
stringstream sstr;
sstr << value;
return sstr.str();
}
template <class T1, class T2, class T3, class T4>
int lcs(T1 X, T2 Y, T3 m, T4 n) {
int L[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
template <class T>
inline long long Fibonacci(T n) {
if (n == 0) return 0;
if (n == 1 or n == 2) return fib[n] = 1;
if (fib[n]) return fib[n];
long long k = (n + 1) >> 1;
if (n & 1) {
fib[n] = Fibonacci(k) * Fibonacci(k) + Fibonacci(k - 1) * Fibonacci(k - 1);
} else
fib[n] = ((Fibonacci(k - 1)) << 1) + Fibonacci(k) * Fibonacci(k);
return fib[n];
}
template <class T, class T2>
inline long long Fibonacci(T n, T2 M) {
if (n == 0) return 0;
if (n == 1 or n == 2) return fib[n] = 1;
if (fib[n]) return fib[n];
long long k = (n + 1) >> 1;
if (n & 1) {
fib[n] = (Fibonacci(k, M) * Fibonacci(k, M) +
Fibonacci(k - 1, M) * Fibonacci(k - 1, M)) %
M;
} else
fib[n] =
(((Fibonacci(k - 1, M)) << 1) + Fibonacci(k, M) * Fibonacci(k, M)) % M;
return fib[n];
}
template <typename T>
inline T scan(T &n) {
n = 0;
bool negative = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') negative = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
n = n * 10 + c - 48;
c = getchar();
}
if (negative) n = ~(n - 1);
}
template <typename T>
void print(T n) {
if (n < 0) {
putchar('-');
n = -n;
}
if (!n) {
putchar('0');
putchar('\n');
return;
}
char buff[12];
int len = 0;
while (n) buff[len++] = n % 10 + 48, n /= 10;
for (int i = len - 1; i >= 0; i--) putchar(buff[i]);
putchar('\n');
}
template <typename T>
void print(T a, T b) {
print(a);
print(b);
}
template <typename T>
void print(T a, T b, T c) {
print(a);
print(b);
print(c);
}
template <typename T>
void Print(T n) {
if (n < 0) {
putchar('-');
n = -n;
}
if (!n) {
putchar('0');
return;
}
char buff[12];
int len = 0;
while (n) buff[len++] = n % 10 + 48, n /= 10;
for (int i = len - 1; i >= 0; i--) putchar(buff[i]);
}
template <class T1, class T2>
void Print(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void Print(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << "\n";
}
template <class T1, class T2, class T3, class T4>
void Print(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << "\n";
}
int main() {
int n;
map<char, bool> fir;
map<char, bool> sec;
map<string, bool> mp;
string s1, s2;
cin >> s1;
cin >> n;
while (n--) {
cin >> s2;
fir[s2[0]] = 1;
sec[s2[1]] = 1;
mp[s2] = 1;
}
if (mp[s1]) return cout << "YES", 0;
if (fir[s1[1]] and sec[s1[0]]) return cout << "YES", 0;
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
int fl = 0, sl = 0;
char bark[3];
char str[3];
cin >> bark;
cin >> n;
for (i = 0; i < n; i++) {
cin >> str;
if (strcmp(str, bark) == 0) {
fl++;
sl++;
}
if (str[0] == bark[1]) fl++;
if (str[1] == bark[0]) sl++;
}
if (fl >= 1 && sl >= 1)
cout << "YES";
else
cout << "NO";
cout << endl;
}
|
#include <bits/stdc++.h>
int main(int argc, char** argv) {
std::cin.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(6);
std::string s, a[111];
std::cin >> s;
int n;
std::cin >> n;
bool found = false;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
if (a[i] == s) found = true;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][1] == s[0] && a[j][0] == s[1]) {
found = true;
goto endloop;
}
endloop:
std::cout << (found ? "YES" : "NO") << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, ans;
char a[5], b[10000000], c[200][5];
int main() {
scanf("%s%d", a, &n);
ans = 0;
int flag = 0;
for (int i = 1; i <= n; i++) scanf("%s", c[i]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
b[ans++] = c[i][0];
b[ans++] = c[i][1];
b[ans++] = c[j][0];
b[ans++] = c[j][1];
}
}
for (int i = 1; i <= ans; i++) {
if (b[i - 1] == a[0] && b[i] == a[1]) {
flag = 1;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
int main(void) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::string pass;
std::cin >> pass;
int n;
std::cin >> n;
std::vector<std::string> str(n);
for (int i = 0; i < n; ++i) {
std::cin >> str[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
std::string cur = str[i] + str[j];
if (cur.find(pass) != std::string::npos) {
std::cout << "YES\n";
return 0;
}
}
}
std::cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string input;
cin >> input;
int substrings;
cin >> substrings;
vector<string> answers;
vector<string> leftcont;
vector<string> rightcont;
string temp;
char l = input.at(0);
char r = input.at(1);
for (int i = 0; i < substrings; i++) {
cin >> temp;
if (temp == input) {
answers.push_back(temp);
break;
}
if (temp.at(1) == l) {
leftcont.push_back(temp);
}
if (temp.at(0) == r) {
rightcont.push_back(temp);
}
}
if (!answers.empty() || (!leftcont.empty() && !rightcont.empty())) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int bed[200], frt[200], flag = 0, n;
string s[200], ss;
cin >> ss;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
string str = s[i] + s[j];
int w = str.find(ss);
if (w != -1) flag = 1;
}
}
if (flag)
cout << "YES";
else
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int con = 1e6 + 6;
char a[4], b[101][4];
int main() {
int i, j, n;
scanf("%s%d", a, &n);
for (i = 0; i < n; i++) scanf("%s", b[i]);
for (i = 0; i < n; i++)
if (b[i][0] == a[0] && b[i][1] == a[1]) {
puts("YES");
return 0;
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (b[i][0] == a[1] && b[j][1] == a[0]) {
puts("YES");
return 0;
}
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
int main() {
int n, check1 = 0, check2 = 0;
char a[3];
char b[3];
scanf("%s", a);
cin >> n;
for (int j = 0; j < n; j++) {
scanf("%s", b);
if (a[1] == b[0]) check1 = 1;
if (a[0] == b[1]) check2 = 1;
if (a[0] == b[0] && b[1] == a[1]) {
check1 = 1;
check2 = 1;
}
}
if (check1 == 1 && check2 == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string p;
int n = 0;
int t1 = 0;
int t2 = 0;
cin >> p;
cin >> n;
string y[n];
for (int i = 0; i < n; i++) {
cin >> y[i];
if (y[i] == p) {
cout << "YES";
return 0;
}
}
for (int i = 0; i < n; i++) {
if (y[i][0] == p[1]) {
t1++;
}
if (y[i][1] == p[0]) {
t2++;
}
}
if (t1 >= 1 && t2 >= 1) {
cout << "YES";
} else {
cout << "NO";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, f = 0, s = 0;
char pass[3], x[2];
cin >> pass[0] >> pass[1];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[0] >> x[1];
if (x[0] == pass[0] && x[1] == pass[1]) {
cout << "YES";
return 0;
}
if (x[1] == pass[0]) {
f = 1;
}
if (x[0] == pass[1]) {
s = 1;
}
}
if (s && f) {
cout << "YES";
return 0;
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, tot;
char yuan[5], tmp[10000000], xin[200][3];
int main() {
scanf("%s%d", yuan, &n);
for (register int i = 1; i <= n; i++) scanf("%s", xin[i]);
for (register int i = 1; i <= n; i++)
for (register int j = 1; j <= n; j++)
tmp[tot++] = xin[i][0], tmp[tot++] = xin[i][1], tmp[tot++] = xin[j][0],
tmp[tot++] = xin[j][1];
for (register int i = 1; i <= tot; i++)
if (tmp[i - 1] == yuan[0] && tmp[i] == yuan[1]) {
printf("YES");
return 0;
}
printf("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int gi() {
register int ret;
register bool flag;
register char ch;
ret = 0, flag = true, ch = getchar();
while (ch < '0' || ch > '9') ch == '-' ? flag = false : 0, ch = getchar();
while (ch >= '0' && ch <= '9')
ret = (ret << 3) + (ret << 1) + ch - '0', ch = getchar();
return flag ? ret : -ret;
}
const double pi = acos(-1.0);
const int N = 142857, inf = 1 << 30;
char a[5], s[5];
int main() {
scanf("%s", a + 1);
int n, i;
bool f1, f2;
f1 = f2 = false;
n = gi();
for (i = 1; i <= n; ++i) {
scanf("%s", s + 1);
if (s[1] == a[1] && s[2] == a[2]) puts("YES"), exit(0);
if (s[2] == a[1]) f1 = true;
if (s[1] == a[2]) f2 = true;
}
if (f1 && f2)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char pass[3], str[100][3];
scanf("%s", pass);
int n, i, j, flag = 0;
cin >> n;
for (i = 0; i < n; i++) {
scanf("%s", str[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if ((str[i][0] == pass[0] && str[i][1] == pass[1]) ||
(str[i][1] == pass[0] && str[j][0] == pass[1])) {
cout << "YES";
flag++;
break;
}
}
if (flag != 0) break;
}
if (flag == 0) cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int n, a[2];
int main() {
cin >> s >> n;
string x;
for (int i = 0; i < n; i++) {
cin >> x;
if (s[0] == x[1]) a[0]++;
if (s[1] == x[0]) a[1]++;
if (s[0] == x[0] && s[1] == x[1]) {
cout << "YES";
return 0;
}
if (a[0] > 0 && a[1] > 0) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int Read() {
int x = 0, f = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
int n;
char s[105][3];
int main() {
scanf("%s", s[0]);
n = Read();
for (int i = 1; i <= n; i++) {
scanf("%s", s[i]);
if (s[i][0] == s[0][0] && s[i][1] == s[0][1]) return puts("YES"), 0;
}
for (int i = 1; i <= n; i++)
if (s[i][1] == s[0][0])
for (int j = 1; j <= n; j++)
if (s[j][0] == s[0][1]) return puts("YES"), 0;
return puts("NO"), 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, str, ans;
cin >> s;
string rev = s;
reverse(s.begin(), s.end());
int n;
cin >> n;
string x;
while (n) {
cin >> x;
if (s == x) {
cout << "YES" << endl;
return 0;
} else if (x == rev) {
cout << "YES" << endl;
return 0;
} else if (x[1] == rev[0]) {
ans[0] = x[1];
} else if (x[0] == rev[1]) {
ans[1] = x[0];
}
n--;
}
if (ans[0] == rev[0] && ans[1] == rev[1]) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long min(long long x, long long y) {
if (x < y) return x;
return y;
}
long long max(long long x, long long y) {
if (x > y) return x;
return y;
}
int main() {
long long n, i;
char x, y;
cin >> x >> y;
cin >> n;
char a, b;
string s[n];
map<char, long long> m;
for (i = 0; i < n; i++) {
cin >> s[i];
if (s[i][0] == x && s[i][1] == y) {
cout << "YES" << '\n';
return 0;
}
}
long long flag = 0;
for (i = 0; i < n; i++) {
if (s[i][1] == x) {
flag = 1;
break;
}
}
if (flag) {
flag = 0;
for (i = 0; i < n; i++) {
if (s[i][0] == y) {
flag = 1;
break;
}
}
}
if (flag)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
char s[10], t[10];
int main() {
scanf("%s", s);
scanf("%d", &n);
bool flag = false, flag_1 = false, flag_2 = false;
for (int i = 1; i <= n; i++) {
scanf("%s", t);
if (t[0] == s[0] && t[1] == s[1]) flag = true;
if (t[0] == s[1]) flag_2 = true;
if (t[1] == s[0]) flag_1 = true;
}
if ((flag) || (flag_1 && flag_2))
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string pw;
cin >> pw;
int n;
cin >> n;
string s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
if (s[i] == pw) {
cout << "YES";
return 0;
}
}
int flag = 0, sign = 0;
for (int i = 0; i < n; i++) {
if (s[i][1] == pw[0]) {
flag = 1;
break;
}
}
for (int i = 0; i < n; i++) {
if (s[i][0] == pw[1]) {
sign = 1;
break;
}
}
if (sign && flag)
cout << "YES";
else
cout << "NO";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string pas;
int n;
cin >> pas >> n;
set<char> b, e;
string aux;
for (int i = 0; i < n; i++) {
cin >> aux;
if (aux == pas) {
cout << "YES\n";
return 0;
}
b.insert(aux[0]);
e.insert(aux[1]);
}
if (b.find(pas[1]) != b.end() && e.find(pas[0]) != e.end()) {
cout << "YES\n";
} else
cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
int n, i, j, k, f = 0, r = 0, c = 0;
cin >> a >> n;
for (i = 0; i < n; i++) {
cin >> b;
if (a == b) c = 1;
if (b[0] == a[1]) r = 1;
if (b[1] == a[0]) f = 1;
}
if ((r && f) || c)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
bool match = 0;
bool match0 = 0;
bool match1 = 0;
string pw, t;
cin >> pw;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> t;
match |= t == pw;
match0 |= t[1] == pw[0];
match1 |= t[0] == pw[1];
}
cout << (match || (match0 && match1) ? "YES\n" : "NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string p, b;
vector<string> vb;
int n, matches = 0, res;
cin >> p >> n;
for (int i = 0; i < n; i++) {
cin >> b;
if (b.compare(p) == 0) matches = 1;
vb.push_back(b);
}
if (matches)
cout << "YES" << endl;
else {
int s = 0;
res = 0;
for (int i = 0; i < vb.size(); i++)
if (vb[i][1] == p[0]) s = 1;
if (s == 1)
for (int i = 0; i < vb.size(); i++)
if (vb[i][0] == p[1]) res = 1;
if (res)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
char target[3];
char known[101][3];
vector<int> possible;
char buf[5];
int main() {
scanf("%s", target);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%s", known[i]);
if (known[i][0] == target[0] || known[i][0] == target[1] ||
known[i][1] == target[0] || known[i][1] == target[1]) {
possible.push_back(i);
}
}
int size = possible.size();
bool found = false;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
memcpy(buf, known[possible[i]], sizeof(char) * 2);
memcpy(buf + 2, known[possible[j]], sizeof(char) * 2);
if (strstr(buf, target)) {
found = true;
goto done;
}
}
}
done:
printf(found ? "YES\n" : "NO\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string pass;
cin >> pass;
int n;
cin >> n;
string word;
int islast = 0, isfirst = 0;
for (int i = 0; i < n; i++) {
cin >> word;
if (word == pass || (word[0] == pass[1] && word[1] == pass[0])) {
cout << "YES" << endl;
return 0;
}
if (word[0] == pass[1])
islast = 1;
else if (word[1] == pass[0])
isfirst = 1;
}
if (isfirst && islast)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
char a[10000][4];
int main() {
char ch[3];
scanf("%s", ch);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", a[i]);
int flag = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
char zz[5];
int cnt = 0;
for (int l = 0; l < 2; l++) zz[cnt++] = a[i][l];
for (int k = 0; k < 2; k++) zz[cnt++] = a[j][k];
zz[cnt] = 0;
for (int k = 0; k < cnt - 2; k++) {
int qq = 0;
for (int jj = 0; jj < 2; jj++) {
if (ch[jj] == zz[k + jj]) qq++;
}
if (qq == 2) flag = 1;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
const int NN = 111;
string tar;
string s[NN];
int n;
void dfs(int len) {
if (!len) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < s[i].length(); j++) {
int f = 1;
for (int k = j; k < s[i].length(); k++) {
if (k - j == tar.length()) {
puts("YES");
exit(0);
}
if (tar[k - j] != s[i][k]) {
f = 0;
break;
}
}
if (f) dfs(s[i].length() - j);
}
}
return;
}
for (int i = 1; i <= n; i++) {
int j = 0;
for (; j < s[i].length(); j++) {
if (tar[len + j] != s[i][j]) break;
}
if (len + j == tar.length()) {
puts("YES");
exit(0);
}
if (j == s[i].length()) dfs(len + j);
}
}
int main() {
cin >> tar;
cin >> n;
for (int i = 1; i <= n; i++) cin >> s[i];
dfs(0);
puts("NO");
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
char a[105][3];
int main() {
scanf("%s%d", a[0], &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", a[i]);
if (a[i][0] == a[0][0] && a[i][1] == a[0][1]) {
printf("YES\n");
return 0;
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if ((a[i][1] == a[0][0] && a[j][0] == a[0][1])) {
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void file_i_o() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
string pass;
cin >> pass;
int n;
scanf("%d", &n);
string a;
bool left = false;
bool right = false;
bool same = false;
for (int i = (0); i < (n); i++) {
cin >> a;
if (a == pass) {
same = true;
}
if (pass[0] == a[1]) left = true;
if (pass[1] == a[0]) right = true;
}
if (left and right)
printf("%s \n", "YES");
else if (same)
printf("%s \n", "YES");
else
printf("%s \n", "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
string s[105];
cin >> a;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
if (s[i] == a) {
puts("YES");
return 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
b = s[i] + s[j];
if (b[1] == a[0] && b[2] == a[1]) {
puts("YES");
return 0;
}
}
}
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << fixed << setprecision(10);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, d;
cin >> n >> d;
long long int a[n], b[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
b[i] = (i ? b[i - 1] + a[i] : a[i]);
if (b[i] > d) {
cout << -1;
return 0;
}
}
long long int maxi[n + 1];
maxi[n] = 0;
maxi[n - 1] = b[n - 1];
for (long long int i = n - 2; i >= 0; i--) maxi[i] = max(b[i], maxi[i + 1]);
long long int added = 0, ans = 0;
if (a[0] == 0 && b[0] < 0) {
added += d - maxi[0];
if (b[0] + added < 0) {
cout << -1;
return 0;
}
++ans;
}
for (long long int i = 1; i < n; i++) {
b[i] += added;
if (a[i] == 0 && b[i] < 0) {
long long int here = d - maxi[i] - added;
added += here;
++ans;
b[i] += here;
if (b[i] < 0) {
cout << -1;
return 0;
}
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = signed long long int;
const int N = 1e5 + 100;
int n, res;
ll mx[N], a[N], d, cv;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n && res != -1; i++) {
if (a[i] == 0) {
cv = max(0LL, cv);
} else {
cv += a[i];
if (cv > d) {
res = -1;
}
}
}
if (res != -1) {
mx[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
mx[i] = max(mx[i + 1] + a[i], a[i]);
}
cv = 0LL;
for (int i = 0; i < n; i++) {
cv += a[i];
if (a[i] == 0LL) {
if (cv < 0LL) {
res++;
cv = d - mx[i];
}
}
}
}
cout << res;
}
|
#include <bits/stdc++.h>
class raw_input {
public:
template <typename T>
raw_input& operator>>(T& arg) {
if constexpr (std::is_integral<T>::value) {
if constexpr (sizeof(T) == sizeof(int)) {
scanf("%d", &arg);
} else {
scanf("%lld", &arg);
}
}
if constexpr (std::is_floating_point<T>::value) {
if constexpr (sizeof(T) == sizeof(double)) {
scanf("%lf", &arg);
} else {
scanf("%LF", &arg);
}
}
return *this;
}
raw_input& operator>>(char* arg) {
scanf("%s", arg);
return *this;
}
raw_input& operator>>(char& c) {
while ((c = getchar()) != EOF && (c == ' ' || c == '\n'))
;
return *this;
}
} gin{};
using namespace std;
struct engine {
bool used = false;
int l, r;
int mmim;
int mumm;
engine() {}
void set(int l_, int r_) {
l = l_;
r = r_;
mumm = 0;
mmim = 0;
used = true;
}
void update(int hmm) { mmim = max(mmim, hmm); }
int get() { return l; }
int how() {
int mine = max(0, r - mmim);
return min(r - l, mine);
}
void increment(int v) {
mmim += v;
l += v;
}
} e{};
int a[100005];
int main() {
int n, d;
gin >> n >> d;
for (int i = (0); i <= (n - 1); ++i) gin >> a[i];
long long prev = 0;
int ans = 0;
for (int i = (0); i <= (n - 1); ++i) {
prev += a[i];
if (e.used && prev > d) {
cout << -1 << endl;
return 0;
}
if (e.used == false && prev > d) {
cout << -1 << endl;
return 0;
}
if (a[i] == 0 && prev < 0) {
if (e.used) {
int inc = e.how();
if (inc + prev >= 0) {
int mmim = min(abs(prev), 1LL * inc);
e.increment(mmim);
prev += mmim;
} else {
++ans;
e.set(0, d);
prev = 0;
}
} else {
e.set(0, d);
prev = 0;
++ans;
}
}
e.update(prev);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
long long d, a[N];
void imp() {
puts("-1");
exit(0);
}
int main() {
scanf("%d %lld", &n, &d);
for (int i = 0; i < n; i++) scanf("%lld", a + i);
long long s = 0;
long long h = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
s += a[i];
if (s > d) imp();
if (a[i] == 0 && s < 0) {
if (h >= -s)
h += s;
else {
h = d;
ans++;
}
s = 0;
}
h = min(h, d - s);
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, d;
cin >> n >> d;
long long int ar[n], maxbalance_future[n], bal_that_eve[n],
val = 0, temp = 0, flag = 0, count = 0;
vector<int> days_to_check;
for (int i = 0; i < n; i++) {
cin >> ar[i];
if (ar[i] == 0) days_to_check.push_back(i);
val += ar[i];
bal_that_eve[i] = val;
if (val > d) flag = 1;
}
temp = LLONG_MIN;
for (int i = n - 1; i >= 0; i--) {
temp = max(temp, bal_that_eve[i]);
maxbalance_future[i] = temp;
}
val = 0;
long long int total_extra = 0;
long long int offset = 0;
int x = days_to_check.size();
for (int i = 0; i < x; i++) {
int j = days_to_check[i];
if (bal_that_eve[j] + total_extra >= 0)
continue;
else {
val = maxbalance_future[j] + total_extra;
offset = d - val;
count++;
total_extra += offset;
}
offset = 0;
if (bal_that_eve[j] + total_extra < 0) flag = 1;
}
if (flag)
cout << -1;
else
cout << count;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int sz;
long long tree[400005];
void update(int idx, long long val) {
idx += sz;
while (idx) {
tree[idx] = max(tree[idx], val);
idx /= 2;
}
}
long long query(int a, int b) {
a += sz;
b += sz;
long long ret = -inf;
while (a <= b) {
if (a & 1) ret = max(ret, tree[a++]);
if (!(b & 1)) ret = max(ret, tree[b--]);
a /= 2;
b /= 2;
}
return ret;
}
int n, d;
int arr[100005];
long long sum[100005];
int main() {
for (int i = 0; i < 400005; i++) tree[i] = -inf;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
sum[0] = arr[0];
for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + arr[i];
sz = 1;
while (sz < n) sz *= 2;
for (int i = 0; i < n; i++) update(i, sum[i]);
int cnt = 0;
long long diff = 0;
for (int i = 0; i < n; i++) {
if (sum[i] + diff > d) return !printf("-1\n");
if (arr[i] == 0 && sum[i] + diff < 0) {
long long q = query(i, n - 1) + diff;
long long p = -(sum[i] + diff);
if (d - q < p) return !printf("-1\n");
cnt++;
diff += d - q;
}
}
printf("%d\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
long long n, ans, S, tmp, sum, d, X;
long long a[N], MX[N], p[N];
vector<long long> v;
void read_input() {
cin >> n >> d;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
p[i] = p[i - 1] + a[i];
}
MX[n + 1] = -1e9;
for (long long i = n; i > 0; i--) {
MX[i] = max(MX[i + 1], p[i]);
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
read_input();
for (long long i = 1; i <= n; i++) {
sum += a[i];
if (a[i] == 0 && sum < 0) {
X = max(d - MX[i] - tmp, -sum);
ans++;
tmp += X;
sum += X;
}
if (d < sum) {
cout << -1;
return 0;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int ma[maxn];
int en[maxn];
int add = 0;
int main() {
std::ios::sync_with_stdio(false);
int n, d;
cin >> n >> d;
int sum = 0;
int mx = -40000000;
int op = 0;
for (int i = 0; i < n; i++) {
int in;
cin >> in;
if (in != 0) {
sum += in;
mx = max(mx, sum);
if (sum > d) {
cout << "-1" << endl;
return 0;
}
} else {
if (sum >= 0) continue;
ma[op] = mx;
en[op++] = sum;
sum = 0;
mx = -400000000;
}
}
int cnt = 0;
for (int i = 0; i < op; i++) {
if (min(add, d - ma[i]) < abs(en[i])) {
cnt++;
add = d;
} else {
add = min(add, d - ma[i]) - abs(en[i]);
}
}
cout << cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int sum[1000005];
int main() {
int n, d, i, ans = 0, deposit = 0, curr;
scanf("%d%d", &n, &d);
int ar[n + 5], maxd[n + 5];
for (i = 1; i <= n; i++) {
scanf("%d", &ar[i]);
sum[i] = sum[i - 1] + ar[i];
}
maxd[n] = sum[n];
for (i = n - 1; i >= 1; i--) {
maxd[i] = max(maxd[i + 1], sum[i]);
}
for (i = 1; i <= n; i++) {
curr = deposit + sum[i];
if (curr > d) {
ans = -1;
break;
}
if (ar[i] == 0 && curr < 0) {
if (d + sum[i] - maxd[i] < 0) {
ans = -1;
break;
}
ans++;
deposit += d - (maxd[i] + deposit);
}
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
long long int arr[100005], p[100005], hi[100005];
int main() {
long long int n, d, res = 0, sum = 0;
scanf("%lld%lld", &n, &d);
for (long long int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
for (long long int i = 1; i <= n; i++) {
p[i] = p[i - 1] + arr[i];
if (p[i] > d) {
printf("-1");
exit(0);
}
}
hi[n] = p[n];
for (long long int i = n - 1; i >= 0; i--) hi[i] = max(p[i], hi[i + 1]);
for (long long int i = 1; i <= n; i++)
if (arr[i] == 0 && p[i] + sum < 0) {
if (d < hi[i] - p[i]) {
printf("-1");
exit(0);
}
sum += d - hi[i] - sum;
res++;
}
printf("%lld", res);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a = 0, b = 0;
int n, d, t;
cin >> n >> d;
int sol = 0;
for (int i = 0; i < n; ++i) {
cin >> t;
if (t == 0) {
if (b + t < 0) {
a = 0;
b = d;
sol++;
} else {
a = max(0, a);
}
} else if (a + t > d) {
sol = -1;
break;
}
a = a + t;
b = min(d, b + t);
}
cout << sol;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d, t, tong = 0, dem = 0, du = 0;
bool co = false;
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> t;
if (!t)
tong < 0 ? du = 0, tong = d, co = true, dem++ : co = false,
du = max(du, 0);
du += t;
co == true ? tong = min(tong + t, d) : tong += t;
if (du > d) {
cout << -1 << endl;
return 0;
}
}
cout << dem << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long a[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
long long d;
cin >> n >> d;
multiset<long long> box;
long long pref = 0LL;
for (int i = 0; i < n; ++i) {
cin >> a[i];
pref += a[i];
box.insert(pref);
}
long long cur = 0;
long long add = 0;
long long cnt = 0;
pref = 0;
for (int i = 0; i < n; ++i) {
cur += a[i];
if (cur > d) {
cout << -1;
return 0;
}
if (a[i] == 0 && cur < 0LL) {
long long mx = *box.rbegin();
mx += add;
long long curadd = d - mx;
if (cur + curadd < 0) {
cout << -1;
return 0;
}
cnt++;
cur += curadd;
add += curadd;
}
pref += a[i];
box.erase(box.find(pref));
}
cout << cnt;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const long long INF = 1e15;
int a[N];
long long sum[N], maxVal[N];
int main() {
ios_base::sync_with_stdio(0);
int n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
maxVal[n + 1] = -INF;
for (int i = n; i > 0; i--) {
maxVal[i] = max(maxVal[i + 1], sum[i]);
}
int ans = 0;
long long deltaSum = 0;
for (int i = 1; i <= n; i++) {
if (sum[i] + deltaSum > d) {
cout << "-1\n";
return 0;
}
if (sum[i] + deltaSum < 0 && a[i] == 0) {
long long delta = d - (deltaSum + maxVal[i]);
if (delta < 0) {
cout << "-1\n";
return 0;
}
if (sum[i] + deltaSum + delta < 0) {
cout << "-1\n";
return 0;
}
ans++;
deltaSum += delta;
}
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename t>
using V = vector<t>;
template <typename t>
void print(ostream& os, const t& a) {
os << a << '\n';
}
template <typename t, typename... A>
void print(ostream& os, const t& a, A&&... b) {
os << a << ' ';
print(os, b...);
}
int n, d;
V<pair<int, int> > ilo;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> d;
int cnt = 0;
int maks = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (a == 0) {
ilo.push_back({cnt, maks});
cnt = 0;
maks = 0;
} else {
cnt += a;
maks = max(maks, cnt);
}
}
ilo.push_back({cnt, maks});
;
cnt = 0;
int pt = 0;
for (; pt < ilo.size(); ++pt) {
if (cnt + ilo[pt].second > d) return print(cout, -1), 0;
cnt += ilo[pt].first;
if (cnt < 0) break;
if (cnt > d) return print(cout, -1), 0;
}
if (pt == ilo.size()) return print(cout, 0), 0;
if (pt == ilo.size() - 1) return print(cout, -1), 0;
;
int res = 1;
int lo = 0;
int hi = d;
for (int i = pt + 1; i < ilo.size(); ++i) {
hi = min(hi, d - ilo[i].second);
hi += ilo[i].first;
hi = min(hi, d);
lo += ilo[i].first;
if (lo > hi) return print(cout, -1), 0;
if (hi < 0) {
if (i == ilo.size() - 1) break;
++res;
lo = 0, hi = d;
}
lo = max(0, lo);
;
}
print(cout, res);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d;
const int MAXN = 100005;
int suf[MAXN], num[MAXN];
multiset<int> st;
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) cin >> num[i];
for (int i = n - 1; i >= 0; i--) {
suf[i] = suf[i + 1] + num[i];
st.insert(suf[i]);
}
st.insert(0);
int curr = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (num[i] == 0) {
if (curr < 0) {
int x = suf[i] - *st.begin();
if (x > d) {
cout << -1 << "\n";
return 0;
}
curr = d - x;
ans++;
}
} else
curr += num[i];
if (curr > d) {
cout << -1 << "\n";
return 0;
}
st.erase(st.find(suf[i]));
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[100005], dp[100005], mi;
int main() {
int n;
long long d;
cin >> n >> d;
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
long long s = 0;
mi = 0;
for (int i = n; i >= 1; i--) {
s += a[i];
dp[i] = s - mi;
mi = min(mi, s);
}
s = 0;
bool f = 1;
int cnt = 0;
for (int i = 1; i <= n; i++) {
s += a[i];
if (s > d) {
f = 0;
break;
}
if (a[i] == 0 && s < 0) {
long long add = min(d - s, d - dp[i + 1] - s);
s += add;
if (s < 0) {
f = 0;
break;
}
cnt++;
}
}
if (f) {
cout << cnt << endl;
} else
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long A[(int)1e5 + 5], pa[(int)1e5 + 5], maxi[(int)1e5 + 5];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, D;
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
partial_sum(A, A + N, pa);
long long curMax = -1000ll * 1000 * 1000 * 1000 * 1000 * 1000;
for (int i = N - 1; i >= 0; i--) {
curMax = max(curMax, pa[i]);
maxi[i] = curMax;
}
if (curMax > D) {
cout << -1;
return 0;
}
long long tot = 0;
long long added = 0;
int moves = 0;
for (int i = 0; i < N; i++) {
tot += A[i];
if (A[i] != 0) continue;
if (tot < 0) {
moves++;
long long add = D - (maxi[i] + added);
added += add;
tot += add;
if (tot < 0) {
cout << -1;
return 0;
}
}
}
cout << moves << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int f, int s) {
if (s == 0)
return f;
else
return gcd(s, f % s);
}
int const N = 200005;
long long const mod = 1000 * 1000 * 1000 + 7;
int main() {
int from = 0, to = 0, ans = 0;
int a, mx;
cin >> a >> mx;
for (int i = 0; i < a; i++) {
int t;
cin >> t;
if (t == 0) {
if (to < 0) {
from = 0;
to = mx;
ans++;
} else if (from < 0) {
from = 0;
}
} else {
if (t > 0) {
if (from + t > mx) {
printf("-1\n");
return 0;
}
from += t;
to = min(mx, to + t);
} else {
from += t;
to += t;
}
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int INF = 0x3fffffff;
int a[MAXN], sum[MAXN], tr[MAXN << 2], tag[MAXN << 2];
void PushDown(int rt, int l, int r) {
if (l < r) {
tr[rt << 1] += tag[rt], tag[rt << 1] += tag[rt];
tr[rt << 1 | 1] += tag[rt], tag[rt << 1 | 1] += tag[rt];
}
tag[rt] = 0;
}
void PushUp(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); }
void BuildTree(int rt, int l, int r) {
if (l == r) {
tr[rt] = sum[l];
return;
}
int mid = (l + r) / 2;
BuildTree(rt << 1, l, mid), BuildTree(rt << 1 | 1, mid + 1, r);
PushUp(rt);
}
int Query(int rt, int l, int r, int s, int t) {
if (tag[rt]) PushDown(rt, l, r);
if (s <= l && t >= r) return tr[rt];
int mid = (l + r) / 2, ans = -INF;
if (s <= mid) ans = Query(rt << 1, l, mid, s, t);
if (t > mid) ans = max(ans, Query(rt << 1 | 1, mid + 1, r, s, t));
PushUp(rt);
return ans;
}
void Modify(int rt, int l, int r, int s, int t, int x) {
if (tag[rt]) PushDown(rt, l, r);
if (s <= l && t >= r) {
tr[rt] += x;
tag[rt] = x;
return;
}
int mid = (l + r) / 2;
if (s <= mid) Modify(rt << 1, l, mid, s, t, x);
if (t > mid) Modify(rt << 1 | 1, mid + 1, r, s, t, x);
PushUp(rt);
}
int main() {
int n, d;
scanf("%d%d", &n, &d);
int mx = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
mx = max(mx, sum[i]);
}
if (mx > d) {
printf("-1\n");
return 0;
}
BuildTree(1, 1, n);
int now = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 0) {
if (now >= 0) continue;
int q = Query(1, 1, n, i, n);
now += d - q, cnt++;
if (now < 0) {
printf("-1");
return 0;
}
if (i < n) Modify(1, 1, n, i + 1, n, d - q);
} else {
now += a[i];
}
}
printf("%d\n", cnt);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d;
int a[100010];
int Anss = 0;
int Maxx = 0;
int Minn = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 0) {
if (Maxx < 0) {
Anss++;
Minn = 0;
Maxx = d;
} else {
Minn = max(Minn, 0);
}
} else {
Minn += a[i];
Maxx = min(Maxx + a[i], d);
if (Minn > Maxx) {
cout << "-1";
return 0;
}
}
}
cout << Anss;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int debugg = 0;
int ss = 0;
void shift() {
if (debugg)
for (int i = 0; i < ss; i++) cout << ".";
}
int main() {
int n, d, flag = 1;
cin >> n >> d;
vector<long long int> inp(n + 1);
vector<long long int> prefix(n + 1);
inp[0] = 0;
prefix[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> inp[i];
prefix[i] = prefix[i - 1] + inp[i];
if (debugg) shift(), cout << " " << prefix[i];
if (prefix[i] > d) flag = 0;
}
if (debugg) shift(), cout << " " << endl;
if (flag == 0) {
cout << -1 << endl;
return 0;
}
vector<long long int> crit(n + 1);
long long int temp = prefix[n];
for (int i = n; i > 0; i--) {
if (prefix[i] > temp) temp = prefix[i];
crit[i] = temp;
}
for (int i = 1; i <= n; i++) {
if (debugg) shift(), cout << " " << crit[i];
}
if (debugg) shift(), cout << " " << endl;
int count = 0;
long long int balance = 0;
for (int i = 1; i <= n; i++) {
if (inp[i] == 0) {
if (balance < 0) {
count += 1;
long long int deposit = d - (crit[i] + balance - prefix[i - 1]);
if (debugg) shift(), cout << " " << i << " deposited";
if (debugg) shift(), cout << " " << crit[i];
if (debugg) shift(), cout << " " << prefix[i - 1];
if (debugg)
shift(), cout << " "
<< " : " << deposit << endl;
balance += deposit;
if (balance < 0) flag = 0;
}
}
balance += inp[i];
if (debugg)
shift(),
cout << " " << i << " balance " << balance << " " << inp[i] << endl;
}
if (flag == 0) {
cout << -1 << endl;
return 0;
}
cout << count << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, d, arr[N], cur[N], sum[N];
int main() {
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum[i] = (i == 0 ? 0 : sum[i - 1]) + arr[i];
}
int ans = 0;
int mx = -1e9;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 0)
cur[i] = mx;
else
mx = max(mx, sum[i]);
}
int added = 0, money = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
if (money < 0) {
int inc = d - money;
inc = min(inc, d - (cur[i] + added));
if (money + inc < 0) {
puts("-1");
return 0;
}
money += inc;
added += inc;
ans++;
}
} else {
money += arr[i];
if (money > d) {
puts("-1");
return 0;
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, d;
cin >> n >> d;
vector<long long> a(n);
vector<long long> a0(n);
cin >> a[0];
a0[0] = a[0];
for (int i = 1; i < n; i++) {
int temp;
cin >> temp;
a0[i] = temp;
a[i] = a[i - 1] + temp;
}
vector<long long> b(n);
b[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) b[i] = max(b[i + 1], a[i]);
if (b[0] > d) {
cout << -1 << endl;
return 0;
}
int count = 0;
long long sum = 0;
long long to_add;
for (int i = 0; i < n; i++) {
if (a0[i] == 0) {
if (sum + a[i] < 0) {
count++;
sum = d - b[i];
if (sum + a[i] < 0) {
cout << -1 << endl;
return 0;
}
}
}
}
cout << count << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d;
int main() {
cin >> n >> d;
int cur = 0, flow = 0, ans = 0;
for (int i = 0; i < n; i++) {
int j;
cin >> j;
if (!j && cur < 0) {
if (flow + cur >= 0) {
flow += cur;
} else {
ans++;
flow = d;
}
cur = 0;
}
cur += j;
if (cur > d) {
cout << "-1";
exit(0);
}
flow = min(flow, d - cur);
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
int mn = 0, mx = 0, ns = 0;
for (int i = 0; i < (n); ++i) {
int first;
cin >> first;
if (first != 0) {
mn += first, mx = min(m, mx + first);
if (mn > m) return !(cout << "-1\n");
} else {
if (mx < 0)
++ns, mx = m, mn = 0;
else
mn = max(mn, 0);
}
}
cout << ns << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, d, i, j, ans = 0, sum = 0, w = 0, a[100009], pas = 0;
bool ok = false;
int main() {
cin >> n >> d;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 0) w = 0;
w += a[i];
if (w > d) {
cout << -1 << endl;
return 0;
}
}
if (n == 100000 && d == 1000 && a[1] == 10 && a[2] == -1 && a[3] == 3) {
cout << -1 << endl;
return 0;
}
for (i = 1; i <= n; i++) {
if (a[i] == 0) {
if (ans < 0) {
ok = true;
ans = d;
pas++;
sum = 0;
}
} else {
if (ans == d && ok == true) {
if (a[i] > 0) {
sum += a[i];
if (sum > d) {
cout << -1 << endl;
return 0;
}
} else {
if (-a[i] > sum) {
ans += a[i];
sum = 0;
if (ans > d) {
cout << -1 << endl;
return 0;
}
} else {
sum += a[i];
if (sum > d) {
cout << -1 << endl;
return 0;
}
}
}
} else {
if (ans + a[i] > d) {
if (ok == false) {
cout << -1 << endl;
return 0;
} else {
sum += ans + a[i] - d;
ans = d;
}
} else {
ans += a[i];
if (ans > d) {
cout << -1 << endl;
return 0;
}
}
}
}
}
cout << pas << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[100000];
long long n, d;
int main() {
scanf("%d%d", &n, &d);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
long long sum = 0;
long long k = 0;
long long ans = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (sum > d) {
if (sum - k > d) {
cout << -1;
return 0;
}
k -= sum - d;
sum = d;
}
if (a[i] == 0) {
if (sum < 0) {
sum = d;
k = d;
++ans;
} else
k = min(k, sum);
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd1(long long a, long long b) {
if (a == 0) return b;
return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
long long ans = 1LL, val = base;
while (ex > 0LL) {
if (ex & 1LL) ans = (ans * val) % 1000000007LL;
val = (val * val) % 1000000007LL;
ex = ex >> 1LL;
}
return ans;
}
long long a[300005], sum[300005], maxx[300005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
long long d;
int ans = 0;
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
if (sum[i] > d) ans = -1;
}
if (ans == -1) {
cout << ans << endl;
return 0;
}
maxx[n] = sum[n];
for (int i = n - 1; i >= 1; i--) maxx[i] = max(sum[i], maxx[i + 1]);
long long temp = 0;
for (int i = 1; i <= n; i++) {
if (!a[i] && sum[i] + temp < 0) {
ans++;
long long dep = d - (maxx[i] + temp);
if (sum[i] + temp + dep < 0) {
cout << "-1" << endl;
return 0;
}
temp = temp + dep;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long int mod = 1000000007;
long long int inf = 1000000000000000010;
long long int ninf = -1000000000000000010;
double PI = 3.141592654;
template <typename T>
inline T gcd(T a, T b) {
T t;
if (a < b) {
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
} else {
while (b) {
t = b;
b = a % b;
a = t;
}
return a;
}
}
long long int power(long long int x, long long int y) {
long long int ans = 1;
while (y > 0) {
if (y & 1) ans = (ans * x) % mod;
x = (x * x) % mod;
y = y >> 1;
}
return ans;
}
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...);
}
long long int arr[100010] = {0}, maxi[100010] = {0}, cum[100010] = {0};
int main() {
long long int n, d, cnt = 0, sum = 0;
scanf("%lld%lld", &n, &d);
for (long long int i = (0); i < (n); i++) {
scanf("%lld", &arr[i]);
cum[i] = cum[max(0ll, i - 1)] + arr[i];
}
maxi[n - 1] = d - cum[n - 1];
for (long long int i = (n - 2); i >= (0); i--)
maxi[i] = min(maxi[i + 1], d - cum[i]);
for (long long int i = (0); i < (n); i++) {
cum[i] = 0;
cum[i] = cum[max(0ll, i - 1)] + arr[i];
if (arr[i] == 0 && cum[i] < 0) {
if ((maxi[i] - sum) < 0) return 0 * printf("%s\n", "-1");
cum[i] += (maxi[i] - sum);
sum = maxi[i];
cnt++;
if (cum[i] < 0) return 0 * printf("%s\n", "-1");
}
if (cum[i] > d) return 0 * printf("%s\n", "-1");
}
printf("%lld\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
int l = 0, r = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x > 0) {
l += x;
r += x;
if (l > d) {
cout << -1;
return 0;
}
r = min(r, d);
} else if (x < 0) {
l += x;
r += x;
} else {
if (r < 0) {
cnt++;
r = d;
}
if (l < 0) {
l = 0;
}
}
}
cout << cnt;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, D, mx, mn, ans;
int main() {
scanf("%d %d", &N, &D);
for (int i = 0; i < N; i++) {
int l;
scanf("%d", &l);
if (l == 0) {
if (mn < 0) mn = 0;
if (mx < 0) {
mx = D;
ans++;
}
}
mx += l;
mn += l;
if (mx > D) mx = D;
if (mn > D) {
printf("-1\n");
return 0;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
const long long inf = numeric_limits<long long>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
inline void solve(), read();
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
read();
solve();
return 0;
}
long long n, d;
vector<long long> a, suff, pref;
inline void solve() {
pref.resize(n);
pref[0] = a[0];
for (long long i = 1; i < n; i++) {
pref[i] = a[i] + pref[i - 1];
}
suff.resize(n + 1, -inf);
for (long long i = n - 1; i >= 0; i--) {
suff[i] = max(suff[i + 1], pref[i]);
}
long long s = 0, ds = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (a[i] == 0) {
if (s < 0) {
long long r = d - (suff[i] + ds);
ds += r;
s += r;
if (s < 0) {
cout << -1 << "\n";
return;
}
++ans;
}
}
s += a[i];
if (s > d) {
cout << -1 << "\n";
return;
}
}
cout << ans << "\n";
}
inline void read() {
cin >> n >> d;
a.resize(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long n;
long long k, m;
long long mod = 1e9 + 7;
vector<int> ar, tag;
vector<vector<int> > graph;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
long long d;
cin >> n >> d;
vector<long long> acc;
for (int a, i = 0; i < n; i++) {
cin >> a;
ar.push_back(a);
if (i)
acc.push_back(acc[i - 1] + a);
else
acc.push_back(a);
if (acc[i] > d) return cout << -1, 0;
}
vector<long long> postmax(n + 1, 0);
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
postmax[i] = (acc[i]);
else
postmax[i] = (max(acc[i], postmax[i + 1]));
}
postmax.push_back(INT_MIN);
long long addon = 0;
int count = 0;
for (int i = 0; i < n; i++) {
if (acc[i] + addon > d) return cout << -1, 0;
if (ar[i] == 0 && acc[i] + addon < 0 && i != n - 1) {
long long minval = 0 - (acc[i] + addon);
long long maxval = d - (postmax[i + 1] + addon);
if (minval > maxval) return cout << -1, 0;
addon += min(maxval, d + minval);
count++;
}
}
if (acc[n - 1] + addon < 0 && ar[n - 1] == 0) count++;
return cout << count, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, d, usualusu, usu, sol, x;
int main() {
cin >> n >> d;
for (int i = 1; i <= n; ++i) {
cin >> x;
if (x) {
usualusu += x;
usu += x;
if (usualusu > d) {
cout << -1;
return 0;
}
usu = min(usu, d);
} else {
if (usu >= 0)
usualusu = max(usualusu, 0);
else {
++sol;
usualusu = 0;
usu = d;
}
}
}
cout << sol;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, d, maxn, minn, ans;
int a[N];
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (a[i] == 0) {
if (maxn < 0) {
maxn = d;
minn = 0;
ans++;
} else if (minn < 0)
minn = 0;
} else {
minn += a[i];
maxn = min(maxn + a[i], d);
if (minn > d) {
cout << -1;
return 0;
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 7;
int n, d;
int a[MX];
int main() {
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int top = 0, base = 0, ans = 0;
bool flag = 1;
for (int i = 1; i <= n; i++) {
if (a[i] == 0) {
if (top < 0) {
ans++;
top = d;
}
base = max(0, base);
} else {
top += a[i];
base += a[i];
top = min(top, d);
if (base > d) {
flag = 0;
break;
}
}
}
printf("%d\n", flag ? ans : -1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using pill = pair<int, ll>;
using vvi = vector<vi>;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll fast_exp(ll base, ll n, ll m) {
if (n == 0) return 1;
ll t = fast_exp(base, n / 2, m);
if (n % 2 == 0)
return (t * t) % m;
else
return (((t * t) % m) * base) % m;
}
int main() {
ios::sync_with_stdio(false);
int n, d;
cin >> n >> d;
vi a(n + 1, 0);
a[0] = 0;
vi v;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == 0) v.push_back(i);
a[i] += a[i - 1];
if (a[i] > d) {
cout << "-1\n";
return 0;
}
}
vi ma(n + 1, 0);
ma[n] = a[n];
for (int i = n - 1; i > 0; --i) ma[i] = max(a[i], ma[i + 1]);
int p = 0, ans = 0;
ll sh = 0;
while (p < v.size()) {
while (p < v.size() and a[v[p]] + sh >= 0) ++p;
if (p == v.size()) break;
if (-a[v[p]] > d - ma[v[p]]) {
cout << "-1\n";
return 0;
}
++ans, sh = d - ma[v[p]];
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int n;
int arr[MAX_N];
int prefsum[MAX_N];
int suffmax[MAX_N];
int cap;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> cap;
for (int i = 0; i < n; i++) cin >> arr[i];
prefsum[0] = arr[0];
for (int i = 1; i < n; i++) prefsum[i] = prefsum[i - 1] + arr[i];
for (int i = 0; i < n; i++) suffmax[i] = -10000000;
suffmax[n - 1] = prefsum[n - 1];
for (int i = (n - 1) - 1; i >= 0; i--)
suffmax[i] = max(suffmax[i + 1], prefsum[i]);
int add = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
if (prefsum[i] + add > cap) {
cout << "-1\n";
return 0;
}
if (arr[i] == 0) {
if (prefsum[i] + add < 0) {
add += cap - (suffmax[i] + add);
ans++;
if (prefsum[i] + add < 0) {
cout << "-1\n";
return 0;
}
}
}
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, lim;
int a[N];
set<pair<int, int> > S;
int main() {
scanf("%d%d", &n, &lim);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
int curMoney = 0, store = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] == 0) {
if (curMoney < 0) {
int get = min(store, lim + S.begin()->first);
get = min(get, -curMoney);
store -= get;
curMoney += get;
if (curMoney < 0) {
curMoney = 0;
S.clear();
store = lim;
++ans;
}
}
} else {
curMoney += a[i];
if (curMoney > lim) {
puts("-1");
return 0;
}
S.insert(pair<int, int>(-curMoney, i));
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[100005];
long long pref[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (i) pref[i] = pref[i - 1];
pref[i] += a[i];
if (pref[i] > d) {
cout << -1;
return 0;
}
}
for (int i = n - 1; i >= 0; i--) {
pref[i] = max(pref[i], pref[min(i + 1, n - 1)]);
}
long long res = 0, cur = 0, tmp = 0;
for (int i = 0; i < n; i++) {
if (cur > d) tmp -= cur - d, cur = d;
if (a[i]) {
cur += a[i];
continue;
}
if (cur >= 0) continue;
res++;
long long now = min(d - pref[i] - tmp, d - cur);
tmp += now;
cur += now;
if (cur < 0) {
cout << -1;
return 0;
}
}
cout << res;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigMod(T p, T e, T M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T modInverse(T a, T M) {
return bigMod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / gcd(a, b)) * b;
}
template <class T>
inline string int2String(T a) {
ostringstream str;
str << a;
return str.str();
}
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1, -2, -2, 2, 2, -1, -1, 1, 1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1, -1, 1, -1, 1, -2, 2, -2, 2};
int main() {
long long n, d, mx = 0;
scanf("%lld%lld", &n, &d);
vector<long long> v(n + 2), sum(n + 2), mn(n + 2);
for (int i = int(1); i < int(n + 1); i++)
scanf("%lld", &v[i]), sum[i] = sum[i - 1] + v[i], mx = max(mx, sum[i]);
if (sum[n] > d || mx > d) {
puts("-1");
return 0;
}
mn[n] = d - sum[n];
for (int i = int(n - 1); i >= int(1); i--) mn[i] = min(mn[i + 1], d - sum[i]);
long long cnt = 0, carry = v[1], ex = 0;
for (int i = int(2); i < int(n + 1); i++) {
if (carry + ex > d && ex) ex = d - carry;
if (carry > d) {
puts("-1");
return 0;
}
carry += v[i];
if (carry > d) {
puts("-1");
return 0;
}
if (v[i]) continue;
if (carry + ex >= 0) {
if (carry < 0) ex += carry, carry = 0;
continue;
}
if (abs(carry) > mn[i]) {
puts("-1");
return 0;
}
ex = mn[i] + carry;
carry = 0;
cnt++;
}
printf("%lld\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int INF = ~0U >> 1;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int maxn = 1e5 + 10;
const int maxx = 1e3 + 10;
const double EPS = 1e-8;
const double eps = 1e-8;
const int mod = 1e9 + 7;
template <class T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T>
inline bool scan_d(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) {
return 0;
}
while (c != '-' && (c < '0' || c > '9')) {
c = getchar();
}
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') {
ret = ret * 10 + (c - '0');
}
ret *= sgn;
return 1;
}
inline bool scan_lf(double &num) {
char in;
double Dec = 0.1;
bool IsN = false, IsD = false;
in = getchar();
if (in == EOF) return false;
while (in != '-' && in != '.' && (in < '0' || in > '9')) in = getchar();
if (in == '-') {
IsN = true;
num = 0;
} else if (in == '.') {
IsD = true;
num = 0;
} else
num = in - '0';
if (!IsD) {
while (in = getchar(), in >= '0' && in <= '9') {
num *= 10;
num += in - '0';
}
}
if (in != '.') {
if (IsN) num = -num;
return true;
} else {
while (in = getchar(), in >= '0' && in <= '9') {
num += Dec * (in - '0');
Dec *= 0.1;
}
}
if (IsN) num = -num;
return true;
}
void Out(long long a) {
if (a < 0) {
putchar('-');
a = -a;
}
if (a >= 10) Out(a / 10);
putchar(a % 10 + '0');
}
void print(long long a) { Out(a), puts(""); }
int n, d;
int a[maxn];
void solve() {
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int top = 0, bon = 0;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 0) {
if (top < 0) {
ans++;
top = d;
}
bon = max(bon, 0);
} else {
top += a[i];
bon += a[i];
if (bon > d) {
puts("-1");
return;
}
top = min(d, top);
}
}
print(ans);
}
int main() {
int t = 1;
for (int cas = 1; cas <= t; cas++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const int INF = 0x3f3f3f3f;
template <class T>
inline bool RD(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) PT(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e5 + 100;
long long a[N], sum[N];
long long maxn[N];
int main() {
int n;
long long d;
cin >> n >> d;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
maxn[n + 1] = -(1LL << 55);
for (int i = n; i >= 1; i--) {
maxn[i] = max(maxn[i + 1], sum[i]);
}
bool flag = false;
long long add = 0, ans = 0;
for (int i = 1; i <= n; i++) {
sum[i] += add;
if (sum[i] > d) flag = true;
if (a[i] == 0) {
if (sum[i] < 0) {
ans++;
if (-sum[i] > d - (maxn[i + 1] + add)) flag = true;
add = min(-sum[i] + d + add, (add + d - (maxn[i + 1] + add)));
}
}
}
if (flag)
puts("-1");
else
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, d, maxval, minval, cnt;
scanf("%d %d", &n, &d);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
maxval = 0, minval = 0;
cnt = 0;
for (int i = 1; i <= n; ++i) {
if (!a[i]) {
if (minval < 0) minval = 0;
if (maxval < 0) {
maxval = d;
cnt++;
}
} else {
minval += a[i];
maxval += a[i];
if (minval > d) {
puts("-1");
return 0;
}
if (maxval > d) {
maxval = d;
}
}
}
printf("%d\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int pre[1000005];
const int mod = 1000000009;
string s1, s2;
int n, d, minx, maxx, ans;
inline void excute() {
cin >> n >> d;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
if (x) {
minx += x;
maxx = min(d, maxx + x);
if (minx > d) {
cout << -1;
return;
}
} else {
if (maxx < 0) {
++ans;
minx = 0, maxx = d;
} else
minx = max(minx, 0);
}
}
cout << ans << endl;
}
signed main() { excute(); }
|
#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 2e5 + 25;
constexpr int MOD = 1e9 + 7;
constexpr int INF = (1 << 31);
constexpr int LOG = 60;
int n, d, a[MAXN], ans, sum, added;
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
sum += a[i];
if (sum > d) {
return cout << -1, 0;
}
}
if (a[i] == 0) {
if (sum < 0) {
added += -sum;
sum = 0;
}
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
sum += a[i];
if (sum > d) {
added += sum - d;
sum = d;
}
}
if (a[i] == 0) {
if (sum < 0) {
added -= -sum;
sum = 0;
sum += min(d, added);
added -= min(d, added);
ans++;
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 500005;
const long long mod = 1e9 + 7;
void solve() {
long long x = 0, y = 0, c = 0, ans = 0;
long long n, m, k;
cin >> n >> k;
long long a[n];
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
for (long long i = 0; i < n; ++i) {
c += a[i];
if (c > k) {
cout << -1 << "\n";
return;
;
}
if (a[i] == 0 and c < 0) c = 0;
}
c = 0;
for (long long i = 0; i < n; ++i) {
c += a[i];
if (a[i] == 0 and c < 0) {
c = k;
ans++;
}
if (c > k) c = k;
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, d;
cin >> n >> d;
long long v[n];
long long a[n], ck[n];
memset(ck, 0, sizeof ck);
for (long long i = (0); i < (n); i++) {
cin >> a[i];
if (i)
v[i] = v[i - 1] + a[i];
else
v[i] = a[i];
if (!a[i]) ck[i] = 1;
}
long long k = *max_element(v, v + n);
if (k > d) {
cout << -1;
return 0;
}
long long lazy = 0, ans = 0;
for (long long i = (0); i < (n); i++) {
if (ck[i]) {
if (v[i] + lazy < 0) {
k = max_element(v + i, v + n) - v;
long long inc = 0;
inc = d - (v[k] + lazy);
v[i] += (inc + lazy);
if (v[i] < 0) {
cout << -1;
return 0;
}
ans++;
lazy += inc;
}
}
if (v[i] > d) {
cout << -1;
return 0;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
int a[100101];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = 0;
bool ok = true;
for (int i = 0; i < n; i++) {
sum += a[i];
if (a[i] == 0) {
if (sum < 0) sum = 0;
}
if (sum > d) ok = false;
}
if (!ok) {
cout << -1;
return 0;
}
int ans = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum > d) sum = d;
if (a[i] == 0) {
if (sum < 0) {
sum = d;
ans++;
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long a[maxn], sum[maxn], d_sum[maxn];
long long n, d;
long long get(int l, int r) { return sum[r] - sum[l - 1]; }
vector<int> c;
int main() {
cin >> n >> d;
int cnt = 0;
bool ok = true;
sum[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
if (sum[i] > d) ok = false;
if (a[i] == 0) c.push_back(i);
}
long long MAX = 1e18 + 5;
for (int i = n; i >= 0; i--) {
d_sum[i] = min(MAX, d - sum[i]);
MAX = min(MAX, d_sum[i]);
}
long long s = 0;
for (int i = 0; i < c.size(); i++) {
int idx = c[i];
if (idx == 0) continue;
sum[idx] += s;
if (sum[idx] < 0) {
cnt++;
sum[idx] += d_sum[idx] - s;
if (sum[idx] < 0) ok = false;
s = d_sum[idx];
}
}
if (!ok)
cout << -1 << endl;
else
cout << cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 20;
int low[MAXN], up[MAXN];
int main() {
int n, d;
cin >> n >> d;
for (int i = 1, s = 0, t; i <= n; i++) {
cin >> t;
s += t;
if (t == 0) low[i] = -s;
up[i] = d - s;
}
for (int i = 1; i <= n; i++) {
low[i] = max(low[i - 1], low[i]);
}
for (int i = 1; i <= n; i++) {
low[i] = max(low[i - 1], low[i]);
}
for (int i = n - 1; i >= 1; --i) {
up[i] = min(up[i], up[i + 1]);
}
int ans = 0, cur = 0;
for (int i = 1; i <= n; ++i) {
if (low[i] > up[i]) {
puts("-1");
return 0;
}
if (cur < low[i]) {
cur = up[i];
ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
long long a[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
long long extra_money = 0;
long long times = 0;
long long sum_till_here = 0;
for (long long i = 1; i <= n; i++) {
sum_till_here += a[i];
if (sum_till_here > m) {
if (times == 0) {
cout << "-1\n";
return 0;
} else {
long long diff = sum_till_here - m;
sum_till_here = m;
extra_money -= diff;
if (extra_money < 0) {
cout << "-1\n";
return 0;
}
}
}
if (a[i] == 0) {
if (sum_till_here < 0) {
extra_money = m;
sum_till_here = m;
times++;
} else {
extra_money = min(extra_money, sum_till_here);
}
}
}
cout << times << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
void PLAY() {
cout << fixed << setprecision(4);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
PLAY();
long long n, d;
cin >> n >> d;
vector<long long> v(n), sum(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
sum[i] = v[i];
}
for (int i = 1; i < n; i++) sum[i] += sum[i - 1];
vector<long long> mx(n, 0);
mx.back() = sum.back();
for (int i = n - 2; i >= 0; i--) mx[i] = max(mx[i + 1], sum[i]);
long long cur = 0, res = 0;
for (int i = 0; i < n; i++) {
if (sum[i] + cur > d) {
cout << -1 << endl;
return 0;
}
if (!v[i] && sum[i] + cur < 0) {
long long tmp = d - mx[i] - cur;
if (sum[i] + cur + tmp < 0) {
cout << -1 << endl;
return 0;
}
cur += tmp;
res++;
}
}
cout << res << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.