Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define forn(i, n) for (int i = 0; i < int(n); i++)
  4. #define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
  5. #define fore(i, l, r) for (int i = int(l); i < int(r); i++)
  6. #define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
  7. #define all(a) (a).begin(), (a).end()
  8. #define sz(a) int((a).size())
  9. #define pb(a) push_back(a)
  10. #define mp(x, y) make_pair((x), (y))
  11. #define x first
  12. #define y second
  13.  
  14. using namespace std;
  15.  
  16. typedef long long li;
  17. typedef long double ld;
  18. typedef pair<int, int> pt;
  19.  
  20. template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
  21. template<typename X> inline X sqr(const X& a) { return a * a; }
  22.  
  23. template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
  24. template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
  25. template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
  26.  
  27. inline ld gett() { return clock() / ld(CLOCKS_PER_SEC); }
  28.  
  29. const int INF = int(1e9);
  30. const li INF64 = li(1e18);
  31. const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;
  32.  
  33. const int N = 500500;
  34.  
  35. int n, p[N];
  36. char s[N];
  37.  
  38. inline bool read() {
  39.     if (!(cin >> n)) return false;
  40.     forn(i, n) assert(scanf("%d", &p[i]) == 1);
  41.     assert(gets(s));
  42.     assert(gets(s));
  43.     return true;
  44. }
  45.  
  46. li s1[N], s2[N];
  47.  
  48. inline void solve() {
  49.     forn(i, n) s1[i + 1] = s1[i] + (s[i] == 'B' ? p[i] : 0);
  50.     forn(i, n) s2[i + 1] = s2[i] + p[i];
  51.    
  52.     auto sum = [](li* s, int l, int r) { return s[r + 1] - s[l]; };
  53.  
  54.     li ans = 0;
  55.     forn(i, n) {
  56.         ans = max(ans, sum(s2, 0, i) - 2 * sum(s1, 0, i));
  57.         ans = max(ans, sum(s2, i, n - 1) - 2 * sum(s1, i, n - 1));
  58.     }
  59.     ans += sum(s1, 0, n - 1);
  60.     cout << ans << endl;
  61. }
  62.  
  63. int main() {
  64. #ifdef SU1
  65.     assert(freopen("input.txt", "rt", stdin));
  66.     //assert(freopen("output.txt", "wt", stdout));
  67. #endif
  68.    
  69.     cout << setprecision(10) << fixed;
  70.     cerr << setprecision(5) << fixed;
  71.  
  72.     while (read()) {
  73.         ld stime = gett();
  74.         solve();
  75.         cerr << "Time: " << gett() - stime << endl;
  76.         //break;
  77.     }
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement